Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Saturday, July 12, 2014

Dealing with different python versions


One can install multiple python versions in unix based operating sytems such as Mac and Linux

To work on a specific python version one can simply use the commands - python'version' in the terminals

Examples:

python2.6 mycode.py would run python 2.6
python3 mycode.py would run python version3.*


Similarly for specific library installation using pip on can directly call 

pip2.6 or pip-2.6 (respectively depending on using pip version 1.5 or lower)


The syntax for using easy_install is also similar:

sudo easy_install2.6 package
sudo easy_install package (would install the package for the default python)


Also to change the version of python one can simply create an alias for the command 'python' to the version of choice in the bashrc file

open the bashrc file with your favorite editor and place the following line in the end- 
eg- vim ~/.bashrc

alias "python" "python'version' "

example: alias "python" "python2.6"


If you dont have a bashrc file in your home folder, create one and paste the code


Also never delete your original python in Linux/Mac OS because a lot of the OS software dependencies are linked to python and deleting can simply make your OS stop working


PIP installation - dealing with multiple python versions

Recently, I was facing this problem  of having multiple versions of python installed and pip installs the install library to the wrong python version

The solution to this is very simple:

Suppose you have different python versions, say:
1) Python 2.6
2) Python 2.7
3) Python 3.1

Pip supports the format pip-{version}

So to install a package for say python2.7 one can simply call

pip-2.7 install packagename

Similary for 3.1

pip-3.1 install packagename


Also for one having pip version 1.5 or higher the format would be pip{version}
that is the above commands will become

pip2.7 install packagename
pip3.1 install packagename

^notice that the '-' is missing in the above commands.

Thursday, January 20, 2011

Matlab basics-2 : Format conversions in Matlab

Suppose we have an image

aa=imread("image.jpg");

Conversion to grayscale:

we can convert it to gray scale using the following operation

grayaa=rgb2gray(aa);

the matrix grayaa is the gray scale version of the image aa.

rgb2gray converts RGB values to grayscale values by forming a weighted sum of the R, G, and B components:

0.2989 * R + 0.5870 * G + 0.1140 * B



Converting the image into black and white:

It can be done in various ways depending on the threshold values.

bw1=im2bw(aa); %% Converts the image into bw using a default value

bw2=im2bw(aa,i); %%here i lies between 0 and 1. 'i' having a value of 0.3 means a threshold value of nearly 77 ( 0.3*256).

An important observation here is that the input image aa can be a grayscale image, an intensity image or even a RGB image.

If we give a RGB image as an input, MATLAB converts the RGB to grayscale first and then into black and white by itself.