python - Using the Image.point() method in PIL to manipulate pixel data -


I am using a lookup table to colorize a black and white image that defines color relationships is. The lookup table is a 256-element list of RGB Tuvles:

  & gt; & Gt; & Gt; Lane (color) 256 & gt; & Gt; & Gt; Color [0] (255, 237, 237) & gt; & Gt; & Gt; Color [127] (50, 196, 33) & gt; & Gt; & Gt;  

My first version is used for getpixel () and putpixel () methods:

  For Range (Y) in Y: Range (Y) for Y: Pixel = IMGGet pixel ((x, y)) IMG.put pixel ((x, y), color [pix [0]])  

This was a slow slower, a profile report, indicating the putpixel and getpixel methods as offenders Is a small investigation (i.e., read the document) and me " Note that this method is relatively slow. " re: putpixel ( real runtime : 53s in putpixel 1024x1024 image for 50s getpixel )

Based on the suggestion in the docs, I use im.load () and instead use direct pixel Done:

  for pixels = img.load () in range (w): for range y (h): pixel = pixels [x, y] pixels [x, y ] = Color [pix [0]]  

The processing speed is faster than an order, but still < Em> Slow: About 3.5 to process a 1024x1024 image.

A more intense study of PIL documents indicates that Image.point () is absolutely intended for this purpose:

im.point (table) => image

im.point (function) => image

Returns a copy of the image, where each pixel is mapped through the given table. The table should include 256 values ​​per band in the image. If a function is used instead, then one argument should say that the function is called once for every possible pixel value, and as a result the table is applied to all the bands of the image.

I have spent some time hacking with the interface, but it seems quite right. Forgive my ignorance, but PIL's documents are kurt and I do not have much image processing experience. I'm going round to some extent and have extended some examples, but there is nothing that uses "click" for me, so in the end, my question:

  • Code> Image.point () This is the right tool for the job?
  • What format / structure is Image.point () table expected?
  • Can one example scam from implementation? I have tried so far that every walk has ended with a straight black image

Is ImagePoint () the right tool for this job?

Yes, Image.point () This job is perfect for

What format / structure is ImagePoint () Expect the table?

You should level the list instead of [code (12, 140, 10), (10, 100, 200) ...] Usage:

  [12, 140, 10, 10, 100, 200, ...]  

Here's a quick example that I've tried now:

  im = im.point (category (256, 0, -1) * 3)  

And by the way, if you have more on colors Control is required and you feel that ImagePoint is not for you. You can see the Image.getdata and Image.putdata By adding yoga, you can change faster than both colors, load and putpixel . It is slower than ImagePoint .

Image.getdata gives you a list of all the pixels, modifies them and Image.putdata . It's easy but try to use the ImagePoint first.


EDIT

I have made a mistake for the first time explanation, I will explain it correctly now:

color table In fact this is

  [0, 1, 2, 3, 4, 5, ... 255, 0, 1, 2, 3, .... 255, 0, 1, 2, 3, ... 255]  

To change each band range color (0, 0, 0) to the other (10, 100, 10) To be like this:

  [10, 1, 2, 3, 4, 5,. ..255, 100, 1, 2, 3, .... 255, 10, 1, 2, 3, ... 255]  

To change your color list correctly Try this in the format for:

  table = sum (zip (* color), ())  

I think my first example is for you The format should display.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -