PIL

PIL: Get RGB Value from GIF

If you load a GIF file with PIL via Image.open(‘giffile.gif’) and then try to look at its pixels, you would get integers instead of tuples since the GIF pixels refers to one of the 256 colours in the GIF colour palette. The palette would then contain the RGB value of the pixel.

To avoid all this hassle and just get RGB tuple directly:

gif = Image.open('giffile.gif')
rgbimage = GIF.convert ('RGB')
rgbimage.getpixel((0,0))
>>>(231, 10, 54)

Python Challenge Level 14: Italy – Code

Used a NumPy array. Doesn’t work if the diagonal+1s are white of course.

import Image
from numpy import array, zeros, uint8
 
line = Image.open('wire.png')
# third dimension is for colours
a = zeros((100, 100, 3), dtype=uint8)
 
step = array([0, 1])
position = array([0, 0])
count = 0
 
# continue until when the next spot expected to be unfilled is filled
while not max(a[tuple(position)]):
	# paint colour
	a[tuple(position)] = array(line.getpixel((count, 0)))
 
	# if reached a wall
	if max(position + step) > 99 or min(position + step) < 0 or max(a[tuple(position + step)]):
		# change direction
		step = (abs(step) ^ 1) * (-step[0] if step[0] else step[1])
 
	# advance
	position += step
	count += 1
 
# convert to image
Image.fromarray(a).save('swirl.jpeg')

Deploy PyQt Applications on Mac OS X with PyInstaller!

The interweb seem to incline on py2app when it come to deploying applications on mac. I’ve tried to make a single deployable .app file for my application for a long time trying to follow these instructions from ars technica. I’m not a hacker and just want to produce a deployable usable application for others to use. And it seems py2app from MacPorts wasn’t able to surmount the Snow Leopard’s 64-bit compatibility issue.

And then, I was slacking off while studying for my final and out of nowhere I found PyInstaller‘s explicit support for PyQt and its recent support for the mac. And after trying, almost everything works out without much of a kink. Credit goes to ChrisWayg who produced an amazingly complete and up-to-date set of instructions to follow. I’m merely telling how my application did using his instructions (April 2010) and hopefully doing my part to draw more attention to the excellent PyInstaller. Read More…

 Scroll to top