Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

get pixel colors of an image

I wrote a hack to get the color of a pixel.
Now pys60 1.2 has support for getpixel (though undocumented).
Cyke64 has shown me in an example.
   1  
   2  >>> from graphics import Image
   3  >>> im = Image.new((10,10))   # create a new 10x10 image
   4  >>> im.clear(0xff0000)        # make it all red
   5  >>> im.getpixel((0,0))   # top-left is red
   6  [(255, 0, 0)]
   7  >>> im.getpixel([(0,0), (10,10)])  # you can get multiple points
   8  [(255, 0, 0), (255, 0, 0)]
   9  >>> 

Get pixel colors of an image

Pys60 now has a decent Canvas and Image class.
We can draw many shapes and color them.
However, one important feature is missing, ie. getpixel().
So, you can only 'write' but not 'read' from graphics.

To get around this problem, I write a small library that
add 'getpixel' to the Image class.
Now, you can call im.getpixel(x,y) and get an (R,G,B) tuple.
   1  
   2  # some setup
   3  from graphics import *
   4  im = screenshot()  # sample image
   5  
   6  # http://larndham.net/service/pys60/getpixel.py
   7  import getpixel
   8  getpixel.enable(im)  # magically give Image.getpixel()
   9  r, g, b = im.getpixel(0,0)  # top left corner
  10  print 'Red: %s, Green:%s, Blue:%s' % (r,g,b)

Now you can do some easy image processing with getpixel.

Implementation note
===================
- This is a pure python module
- It saves an image as an uncompressed PNG file
- It reads pixel data from the file and attach it to the image.
- You need to call enable() every time you change the image.
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS