analitics

Pages

Saturday, January 26, 2013

The sandboxed Python - development environments using pip and virtualenv .

Use pip and virtualenv and you can make sandboxed Python development environments.

With this tools such as pip and virtualenv you have total control over the development environment.

Why? Because if your project is developed by a team with mutiple developers then they prefer having exactly replicated environments.

Let's try some simple example commands:

1. Start your environment ( in my case is: my_new_env ).

[free-tutorials@free-tutorials ~]$ python virtualenv.py my_new_env
New python executable in my_new_env/bin/python
Installing setuptools..................done.
Installing pip.............done.

Activate your environment ( in my case is: my_new_env ).

[free-tutorials@free-tutorials ~]$ . my_new_env/bin/activate

Let's see the pip --help command :

(my_new_env)[free-tutorials@free-tutorials ~]$ pip --help
Usage: pip COMMAND [OPTIONS]
 --version                    show program's version number and exit
 -h, --help                   Show help
 -v, --verbose                Give more output
 -q, --quiet                  Give less output
 --log <filename>             Log file where a complete (maximum verbosity)
                              record will be kept
 --proxy <proxy>              Specify a proxy in the form
                              user:passwd@proxy.server:port. Note that the
                              user:password@ is optional and required only if
                              you are behind an authenticated proxy. If you
                              provide user@proxy.server:port then you will be
                              prompted for a password.
 --timeout <seconds>          Set the socket timeout (default 15 seconds)
 --exists-action <exists_action>
                              Default action when a path already exists. Use
                              this option more than one time to specify
                              another action if a certain option is not
                              available. Choices: (s)witch, (i)gnore, (w)ipe,
                              (b)ackup

Commands available:
  bundle: Create pybundles (archives containing multiple packages)
  freeze: Output all currently installed packages (exact versions) to stdout
  help: Show available commands
  install: Install packages
  search: Search PyPI
  uninstall: Uninstall packages
  unzip: Unzip individual packages
  zip: Zip individual packages

Now we will use freeze and install.

I will list all the pip packages used in my virtual environment.

(my_new_env)[free-tutorials@free-tutorials ~]$  pip freeze -l
PyOpenGL==3.0.2
PyOpenGL-accelerate==3.0.2

Put all the output in my_packages.txt file.

(my_new_env)[free-tutorials@free-tutorials ~]$ pip freeze -l > my_packages.txt

Install my packages from my_packages.txt .

(my_new_env)[free-tutorials@free-tutorials ~]$ pip install -r my_packages.txt
Requirement already satisfied (use --upgrade to upgrade): PyOpenGL==3.0.2 in 
./my_new_env/lib/python2.7/site-packages (from -r my_packages.txt (line 1))
Requirement already satisfied (use --upgrade to upgrade): PyOpenGL-accelerate==3.0.2 in 
./my_new_env/lib/python2.7/site-packages (from -r my_packages.txt (line 2))
Cleaning up...

Let's try now to find one python module : opencv .

(my_new_env)[free-tutorials@free-tutorials ~]$ pip search opencv 
remotecv                  - remotecv is an OpenCV server for facial and
                            feature recognition
ctypes-opencv             - ctypes-opencv - A Python wrapper for OpenCV using
                            ctypes
pyopencv                  - PyOpenCV - A Python wrapper for OpenCV 2.x using
                            Boost.Python and NumPy
opencv-cython             - An alternative OpenCV wrapper
CVtypes                   - Python OpenCV wrapper using ctypes
Tippy                     - another Toolbox for Image Processing in PYthon,
                            based on OpenCV

You can see where the version of python you are using installs to by running it the next python code.

>>> import sys
>>> sys.prefix
'/home/free-tutorials/my_new_env'
>>> sys.exec_prefix
'/home/free-tutorials/my_new_env'

To leave your environment just type next command: $ deactivate.

I will come with new tutorials about pip and virtualenv .

See you later.

Sunday, December 16, 2012

Simple way to create png image with an input text.

First you need to have the PIL python module and import this.

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from PIL import ImageFilter

Next will be this function.Basically will make and return one RGBA image data type.

def make_img(image, textColor, backgroundColor):
  img = image.convert("RGBA")
  img.putdata([textColor if value == 0 else backgroundColor 
                   for value in image.getdata()])
  return img

The next step is to set the text , font and the new image.

I use Arggotsc.ttf. You can use any TrueType font.

text = " Inca un script in python! "
font = ImageFont.truetype('Arggotsc.ttf', 55)
image = Image.new("1", font.getsize(text), '#FFF')

Now we can draw, add text, resize, bur the text and finally save the image.

draw = ImageDraw.Draw(image)
draw.text((0, 0), text, font=font)
image = image.resize([i for i in image.size], Image.NEAREST)
imgText = make_img(image, (200, 200, 200), (0, 0, 0, 0))
blur_img = make_img(image, (0, 0, 0), (0, 0, 0, 0))
for i in range(3): 
  blur_img = blur_img.filter(ImageFilter.BLUR)
blur_img.paste(imgText, (0, 0), imgText)
blur_img.save("text-img.png")

The result is:


See you later with another tutorial.