Diego Manenti Martins

Installing mod_wsgi against Python 2.6 on Mac OSX 10.7 (Lion)

23/04/2012

If you want to install mod_wsgi on Mac OSX against Python 2.6.7 (default install) the switch --with-python=/usr/bin/python2.6 is not enough.

$ ./configure --with-python=/usr/bin/python2.6
checking for apxs2... no
checking for apxs... /usr/sbin/apxs
checking Apache version... 2.2.21
configure: creating ./config.status
config.status: creating Makefile
$ make
/usr/sbin/apxs -c -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6
-DENABLE_DTRACE -DMACOSX -DNDEBUG -DNDEBUG -DENABLE_DTRACE  
-Wc,'-arch x86_64' -Wc,'-arch i386' mod_wsgi.c -Wl,-F/System/Library/Frameworks -framework Python
-u _PyMac_Error /System/Library/Frameworks/  -arch x86_64 -arch i386 -ldl  -framework CoreFoundation
/usr/share/apr-1/build-1/libtool --silent --mode=compile gcc    -DDARWIN
-DSIGPROCMASK_SETS_THREAD_MASK -I/usr/local/include -I/usr/include/apache2 
-I/usr/include/apr-1   -I/usr/include/apr-1  -arch x86_64 -arch i386
-I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6
-DENABLE_DTRACE -DMACOSX -DNDEBUG -DNDEBUG -DENABLE_DTRACE  -c -o mod_wsgi.lo mod_wsgi.c && touch mod_wsgi.slo
/usr/share/apr-1/build-1/libtool --silent --mode=link gcc -o mod_wsgi.la  -rpath /usr/libexec/apache2
-module -avoid-version    mod_wsgi.lo -Wl,-F/System/Library/Frameworks -framework Python
-u _PyMac_Error /System/Library/Frameworks/ -arch x86_64 -arch i386 -ldl -framework CoreFoundation
warning: no debug symbols in executable (-arch x86_64)
warning: no debug symbols in executable (-arch i386)

As we can check with otool it was built against Python 2.7:

$ otool -L .libs/mod_wsgi.so
.libs/mod_wsgi.so:
/System/Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.1)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation
(compatibility version 150.0.0, current version 635.19.0)

To build it for Python 2.6 you have to pass also the command --disable-framework.

$ ./configure --with-python=/usr/bin/python2.6 --disable-framework
checking for apxs2... no
checking for apxs... /usr/sbin/apxs
checking Apache version... 2.2.21
configure: creating ./config.status
config.status: creating Makefile
$ make
/usr/sbin/apxs -c -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6
-DENABLE_DTRACE -DMACOSX -DNDEBUG -DNDEBUG -DENABLE_DTRACE  -Wc,'-arch x86_64' -Wc,'-arch i386' mod_wsgi.c
-L/System/Library/Frameworks/Python.framework/Versions/2.6/lib
-L/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/config 
-arch x86_64 -arch i386 -lpython2.6 -ldl  -framework CoreFoundation 
/usr/share/apr-1/build-1/libtool --silent --mode=compile gcc    -DDARWIN -DSIGPROCMASK_SETS_THREAD_MASK
-I/usr/local/include -I/usr/include/apache2  -I/usr/include/apr-1   -I/usr/include/apr-1  -arch x86_64
-arch i386 -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6
-DENABLE_DTRACE -DMACOSX -DNDEBUG -DNDEBUG -DENABLE_DTRACE  -c -o mod_wsgi.lo
mod_wsgi.c && touch mod_wsgi.slo
/usr/share/apr-1/build-1/libtool --silent --mode=link gcc -o mod_wsgi.la  -rpath /usr/libexec/apache2
-module -avoid-version    mod_wsgi.lo -L/System/Library/Frameworks/Python.framework/Versions/2.6/lib
-L/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/config -arch x86_64
-arch i386 -lpython2.6 -ldl -framework CoreFoundation
warning: no debug symbols in executable (-arch x86_64)
warning: no debug symbols in executable (-arch i386)

Now it's compiled for Python 2.6:

$ otool -L .libs/mod_wsgi.so
.libs/mod_wsgi.so:
/System/Library/Frameworks/Python.framework/Versions/2.6/Python (compatibility version 2.6.0, current version 2.6.7)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation
(compatibility version 150.0.0, current version 635.19.0)

Tags: python, mod_wsgi, mac


Speeding up image resizing with Python and PIL

13/04/2012

Resizing images some times can take a lot of time when dealing with large files. If you are using PIL (although I think this should work with other image libraries too), you can do a "two passes" resize, first with the faster algorithm and the second with the best quality algorithm, increases significantly the operation speed.

For example, lets convert a 30MB image, resizing it to 800x600 pixels. First we convert it to the double of the target size using the fastest algorithm, then we convert the result to the target size using the ANTIALIAS filter.

>>> import Image
>>> import datetime
>>> im = Image.open('bigimage.png')
>>> start=datetime.datetime.now();\
... im.thumbnail((1600, 1200));\
... im.thumbnail((800, 600), Image.ANTIALIAS);\
... print '%d seconds' % (datetime.datetime.now() - start).seconds
6 seconds

Convert the image directly to the target size increases the time.

>>> im = Image.open('bigimage.png')
>>> start=datetime.datetime.now();\
... im.thumbnail((800, 600), Image.ANTIALIAS);\
... print '%d seconds' % (datetime.datetime.now() - start).seconds
28 seconds

It takes about 4x more time! Six seconds is not a good time yet if you want to resize it on a web application but it's far better than 28 seconds.

Tags: python, PIL


PIL e libjpeg no Snow Leopard (Mac OS X 10.6)

18/12/2010

Tentando instalar a Python Imaging Library (PIL) no Mac OS X 10.6 obtia o seguinte erro:

Symbol not found: _jpeg_resync_to_restart

Depois de pesquisar um pouco e não achar nada que resolvesse o problema, resolvi tentar compilar a libjpeg para x86_64.

$ CC="gcc -arch x86_64" ./configure --enable-static --enable-shared
$ make
$ sudo make install

Depois é só instalar a PIL:

$ python setup.py build

Verifique se o suporte a JPEG foi habilitado, então:

$ sudo python setup.py install

Utilizei a versão padrão do python no sistema (2.6), libjpeg 8b e PIL 1.1.7.

Agora tudo funcionando:

$ python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL
>>> import Image
>>> import _imaging
>>>

Tags: python, PIL, mac


DOSBox

12/04/2010

Descobri o DOSBox quando precisei rodar um programa em modo DOS no Windows Vista e descobri que este não suporta console DOS fullscreen.

Funciona bem, inclusive com o conversor USB/Serial, já que o programa que eu precisava rodar usa a porta serial para enviar dados para um CNC.

Para quem, em 2010, ainda precisa usar softwares mal feitos em DOS é uma mão na roda.

http://www.dosbox.com

Tags: DOSBox


Arduino e LCD

12/04/2010

Depois de botar pra funcionar meu arduino feito em casa abri o exemplo da biblioteca LiquidCrystal, que vem na instalação padrão do arduino.

A utilização da biblioteca é bem simples, basta indicar os pinos RS, RW, EN e os pinos de dados. Pode ser utilizado o controle em 4 bits e 8 bits.

Liguei os pinos do LCD no arduino, configurado para controle com 4 bits, baixei o programa e … não funionou! Azarado.

Verifiquei as ligações, tudo certo. Resetei a placa algumas vezes e o tal do 'hello, world!' apareceu.

Olhando o código da LiquidCrystal, vi que faltavam alguns comandos de inicialização para o controlador do display (HD44780). Deixei uma mensagem no forum Bugs & Sugestions mas não teve nenhuma resposta.

Resolvi os problemas de inicialização do LCD e coloquei o código em http://code.google.com/p/arduinolcd/. Para utilizar baixe o arquivo ZIP, descompacte e copie a pasta 'HD47660′ para a pasta 'hardware/libraries/' da instalação do Arduino.

Depois de instalada a biblioteca, vamos aos exemplos.

Abra o exempl HelloWorld em 'File>Sketchbook>Examples>Library>HD44780>HelloWorld', baixe para a placa, …

HD44780

… agora sim. :)

O exemplo SerialDisplay também funcionou. Demorei um pouco pra testar pois não tinha achado ainda um cristal de 16 MHz e queria também testar a porta serial. Como moro em Santa Catarina e longe da capital, não é muito fácil encontrar componentes eletrônicos por aqui. Não estava disposto a pagar mais pelo frete que pelo componente e não tinha nenhuma compra em vista para incluir alguns cristais. Achei em uma placa MiniDrangon que tenho aqui e agora posso testar as bibliotecas corretamente (as que utilizam temporização e porta serial).

Estou esperando chegar os fios para wire wrap para montar a placa ethernet e ver o que dá pra fazer com o Arduino na rede.

Tags: arduino, embedded, lcd