analitics

Pages

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.

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 https://raw.github.com/pypa/virtualenv/master/virtualenv.py
...
[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.
[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 setup.py 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.

Sunday, October 14, 2012

About new release - Python 3.3.0

The new Python 3.3.0 was released on September 29th, 2012.

It's been two weeks since it was launched last version of python and I don't have found complaints about this release.

We can read more about updates and changes made by developers here.

What I think it's more significantly to this version:

  • The new "faulthandler" module that helps diagnosing crashes
  • The new "unittest.mock" module
  • The new "ipaddress" module
  • The "sys.implementation" attribute
  • A policy framework for the email package, with a provisional (see PEP 411) policy that adds much improved unicode support for email header parsing
  • A "collections.ChainMap" class for linking mappings to a single unit
  • Wrappers for many more POSIX functions in the "os" and "signal" modules, as well as other useful functions such as "sendfile()"
  • Hash randomization, introduced in earlier bugfix releases, is now switched on by default
  • A C implementation of the "decimal" module, with up to 120x speedup for decimal-heavy applications
  • The import system (__import__) is based on importlib by default
  • The new "lzma" module with LZMA/XZ support
  • PEP 397, a Python launcher for Windows
  • PEP 405, virtual environment support in core

Let's see how to working the new Python 3.3.0

First download it , unzip and install it. See next:

/Python-3.3.0 $ ./configure 
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking for --enable-universalsdk... no
checking for --with-universal-archs... 32-bit
checking MACHDEP... linux
checking for --without-gcc... 

The next step is ...

$ make all

And finally ...

# su 
# ./python setup.py build
running build
running build_ext
INFO: Can't locate Tcl/Tk libs and/or headers

Python build finished, but the necessary bits to build these modules were not found:
_bz2               _dbm               _gdbm           
_lzma              _sqlite3           _ssl            
_tkinter           readline                           
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

I will use it without this modules, until I find a way to fix it.

$ ./python 
Python 3.3.0 (default, Oct 14 2012, 21:42:00) 
[GCC 4.4.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import ipaddress

Tuesday, October 2, 2012

Tuples can put you in difficulty.

They are two examples of sequence data types in python.

Today I will tell about tuples.

A tuple consists of a number of values separated by commas.

The biggest problem is when we do not know the number of these values ​​or type.

Let me illustrate with a function that returns a tuple.

This is a known tuple but we will use like an unknown tuple.

Let's see the python code and some errors:

>>> import sys
>>> if hasattr(sys, 'version_info'):
...     print sys.version_info()
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: 'tuple' object is not callable

Is very correct to got this error.

And the result using the print function:

>>> if hasattr(sys, 'version_info'):
...     print "%s" % str(sys.version_info)
... 
(2, 6, 4, 'final', 0) 

>>> if hasattr(sys, 'version_info'):
...     sys.stderr.write("%s" % str(sys.version_info))
... 
(2, 6, 4, 'final', 0)>>> 

or if you can use the repr function.


>>> if hasattr(sys, 'version_info'):
...     sys.stderr.write("%s" % repr(sys.version_info))
... 
(2, 6, 4, 'final', 0)>>> 

The official Python documentation says __repr__ is used to compute the “official” string representation of an object and __str__ is used to compute the “informal” string representation of an object.

In my opinion , the correct way it's to use repr. For example:

>>> import datetime
>>> today = datetime.datetime.now()
>>> str(today)
'2012-10-02 22:45:57.634977'
>>> repr(today)
'datetime.datetime(2012, 10, 2, 22, 45, 57, 634977)'

As we see all values from tuples it's show.