views:

859

answers:

3

How can I read the colors of an image with python using google app engine?

Example: I like to build a function to determine the most striking colors of an image to set a harmonic background color for it.

+2  A: 

The Images API does not (currently) contain pixel-level functions. To quote the overview document:

Note: In order to use the Images API in your local environment you must first download and install PIL, the Python Imaging Library. PIL is not available on App Engine; it is only used as a stub for the Images API in your local environment. Only the transforms provided in the images API are available on App Engine.

The community has been asking for full PIL support for some time, but it looks like we'll have to wait.

gimel
A: 

If you are willing to put Flash or a Java applet on the page, you might be able to do it on the client. I'm not sure if anything like canvas or SVG supports pixel-level manipulation, but if they do, you might be able to get it to work in some browsers with JavaScript.

The Flash or Java Applet, can be invisible and optional -- you can use JavaScript to detect if the user has the plugin and only support this feature when they do.

Lou Franco
That's assuming that the application is a website, it's possible it would be something else.
Noah McIlraith
+3  A: 

For PNG you can use PyPNG module - lightweight pure-Python PNG decoder/encoder.

import png

point = (10, 20) # coordinates of pixel to read

reader = png.Reader(filename='image.png') # streams are also accepted
w, h, pixels, metadata = reader.read()
pixel_byte_width = 4 if metadata['has_alpha'] else 3
pixel_position = point[0] + point[1] * w
print pixels[
  pixel_position * pixel_byte_width :
  (pixel_position + 1) * pixel_byte_width]
Constantin
Reading of other image types can be achieved by converting the image to PNG within AppEngine.
Noah McIlraith