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 ="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)"text-img.png")

The result is:

See you later with another tutorial.

Tuesday, December 11, 2012

Using pip for installing and managing Python packages.

An easy_install replacement is pip.

So pip installs packages and managing Python packages.

Let's try to install packages.

$ pip install PyOpenGL_accelerate
bash: pip: command not found 

Install the pip package.

The recommended way to use pip is within virtualenv, since every virtualenv has pip installed in it automatically.

[free-tutorials@free-tutorials ~]$ 
curl -O
[free-tutorials@free-tutorials ~]$ python my_new_env
New python executable in my_new_env/bin/python
Installing setuptools............................done.
Installing pip.....................done.
[free-tutorials@free-tutorials ~]$ . my_new_env/bin/activate
(my_new_env)[free-tutorials@free-tutorials ~]$ pip --help

Let's try again with PyOpenGL and PyOpenGL_accelerate packages.

(my_new_env)[free-tutorials@free-tutorials ~]$
 pip install PyOpenGL PyOpenGL_accelerate
Downloading/unpacking PyOpenGL
  Downloading PyOpenGL-3.0.2.tar.gz (891kB): 891kB downloaded
  Running egg_info for package PyOpenGL

Now we can test this two packages.First is PyOpenGL.

(my_new_env)[free-tutorials@free-tutorials ~]$ python 
Python 2.7.3 (default, Dec  6 2012, 03:02:26) 
[GCC 4.3.2 20081105 (Red Hat 4.3.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import OpenGL
>>> dir(OpenGL)

...and PyOpenGL_accelerate:

(my_new_env)[free-tutorials@free-tutorials ~]$ python 
Python 2.7.3 (default, Dec  6 2012, 03:02:26) 
[GCC 4.3.2 20081105 (Red Hat 4.3.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import OpenGL_accelerate
>>> dir(OpenGL_accelerate)

This is working only on my_new_env.

That is all for now. I will try to do some examples with this packages.