algorithm - Image Gurus: Optimize my Python PNG transparency function -


I have to change all the white (ISH) pixels in a PNG image with alpha transparency.

I am using Python in APENGen and there is no access to such libraries like PIL, ImageMagk etc. AppAngine has an image library, but it is mainly placed in image resizing.

To open a small and managed little function that I need:

The pseudo-code for the main loop would be something like this:

  Pixel for each: If the pixel looks "quite white": otherwise set the pixel value to make transparent: keep the existing pixel values ​​and ("assuming 8bit values") will be "quite white" :  
  Where every R, ​​G, B value is greater than "240" and every R, ​​G, B value is This is the first of all "20" within  

This is the first time I have given time to work with raw pixel data like this, and though it works, it performs very poorly It seems that in this way there should be a more efficient way of processing any data on every pixel? (Matrix?)

I was hoping that anyone with more experience in dealing with these things could be able to point out some of my more obvious mistakes / improvements in my algorithm.

Thank you!

It still goes to every pixel, but can be fast:

  New_pixels = [] for row in pixels: new_row = array ('b', line) i = 0 while i  

This slicen generator , Which will copy the full line of pixels for every pixel (less than one pixel each time).

It also allocates pre-output lines by simply copying the input row, and then only writes the alpha value of pixels which have changed.

Even a new set of fast pixels will not be allocated, and pixels directly in the source image (assuming you do not need a source picture for anything else).


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -