RSS Feed

OSS Ramblings

http://www.ossramblings.com/taxonomy/term/230

 

python

Making Apache Run Python

I've always been a PHP guy, but recently I had a little Python web scraper utility I wrote that I wanted a nice interface to, and I didn't feel like writing a complete GTK interface for it, or rewriting it in PHP with CURL.

So, I thought, "Hey, there's these people that run Python on their servers instead of PHP, why don't I try that! It shouldn't be any harder than running PHP, right?"

Wrong. It's not hard, but it brings back memories of my first attempt to get Perl scripts to work properly with Apache.

First, you need to install "libapache2-mod-wsgi". This is the module for Apache that lets you run Python scripts from inside your web server. I know, why doesn't it have the word "Python" in the name? Don't worry - just make sure you don't actually try to use the module that DOES have Python in the name. It's the old and outdated way of doing things.

So, use Synaptic to install the module, or at a console enter:

sudo apt-get install libapache2-mod-wsgi


Load Multiple Image Formats Into Cairo Surfaces (Python)

This drove me up the wall trying to figure it out, but loading jpg's or tiff's into a Cairo surface with Python really isn't all that hard.

The trick is to load it into a gtk.gdk.pixbuf first. here's an example:

pixbuf = gtk.gdk.pixbuf_new_from_file(filename)
x = pixbuf.get_width()
y = pixbuf.get_height()
''' create a new cairo surface to place the image on '''
surface = cairo.ImageSurface(0,x,y)
''' create a context to the new surface '''
ct = cairo.Context(surface)
''' create a GDK formatted Cairo context to the new Cairo native context '''
ct2 = gtk.gdk.CairoContext(ct)
''' draw from the pixbuf to the new surface '''
ct2.set_source_pixbuf(pixbuf,0,0)
ct2.paint()
''' surface now contains the image in a Cairo surface '''

Of course you could bypass stamping it onto a Cairo surface entirely and just use the original pixmap for most things, but for doing affine translations I needed an actual surface and not a context to one. Otherwise you have to remember what is accessible through gdk and what is a native Cairo surface.

With a little bit of pipe magic, you can even do image manipulation externally before loading it, and never have to create a temp file:

import subprocess


Working On A Photo Manager

I'm working on a photo manager for Linux with a kinetic scrolling feature in Python. So far so good. The thumbnails are actually being rendered, tilted and reflected by my code. Unfortunately Python can't do perspective transforms, only sheers, so you get a bit of a top-down view.





Python Kinetic GTK Scrolling List for Maemo

It took a lot of sweat and tears, but I finally converted the Kagu kinetic scrolling selection widget to work with GTK.

Check it out.



 
 
 




Image 01 Image 02 Image 03