Top Picks

January 7, 2013


Using Python to get pixel value of a jpeg(SL4A android 4.0)


I am working on a project on my Samsung Galaxy Tab with Android 4.0 Ice-cream sandwich.
Using python through SL4A I am trying to get the pixel RGB value of a jpeg(or png) image of a give co-ordinate (x,y).



The Android scripting facades do not include anything that can do this. And, as far as I know, nobody has done a port of PIL, or any other popular image libraries, to SL4A.

However, Kivy’s Python for Android distribution comes with PIL. If you can use that, great. If not, maybe you can look at how they get

to build, and build it for S4LA.

If either of those works for you, this is easy:

jpg ='foo.jpg') pixels = jpg.load() r, g, b = pixels[x, y]

Alternatively, there are a handful of pure-Python JPEG libraries out there, such as TonyJpegDecoder. Try searching PyPI, ActiveState, and Google for more. None of these are as complete, full-featured, performant, or simple as PIL, the Python bindings for libjpeg, etc., but you should be able to handle what you need. For example (untested):

with open('foo.jpg', 'rb') as f: jpegdata = decoder = TonyJpegDecoder.TonyJpegDecoder() bmp = decoder.DecompressImage(jpegdata) bmpstr = ''.join(map(cur(bmp))) bmprgb = bgr2rgb(bmpstr) pos = y * decoder.Width + x rgb = map(ord, bmprgb[pos*3:(pos+1)*3])
Professional Hosting fro Just Host

Filed under: Android Coding FAQ



No Comments

Leave a reply