简体   繁体   中英

Multiply each pixel in an image by a factor

I have an image that is created by using a bayer filter and the colors are slightly off. I need to multiply RG and B of each pixel by a certain factor ( a different factor for R, G and B each) to get the correct color. I am using the python imaging library and of course writing in python. is there any way to do this efficiently?

Thanks!

Here is how to do it:

  1. First split your image into the RGB channels.
  2. Use point to multiply a channel by factor ( 1.5 in the example, on the r channel).
  3. Merge the channels back.

Heres the code:

import Image
im = Image.open('1_tree.jpg')
im = im.convert('RGB')
r, g, b = im.split()
r = r.point(lambda i: i * 1.5)
out = Image.merge('RGB', (r, g, b))
out.show()

original:

在此处输入图片说明

With the red channel multiplied by 1.5 (its a bit redder..):

在此处输入图片说明

You can do this pretty efficiently and simply using a simple transform matrix with the convert() function:

#!/usr/bin/env python3 

from PIL import Image 

# Open image 
im = Image.open('tree.jpg') 

# Make transform matrix, to multiply R by 1.5, leaving G and B unchanged
Matrix = ( 1.5, 0,  0, 0, 
           0,   1,  0, 0, 
           0,   0,  1, 0) 

# Apply transform and save 
im = im.convert("RGB", Matrix) 
im.save('result.png') 

Input image:

在此处输入图片说明

Result image:

在此处输入图片说明

Keywords : Python, PIL, Pillow, color matrix, colour matrix, transform, multiply channel, scale channel, separate, separately, individual channels, bands, components, individually, split channels, merge channels, image, image processing.

As a basic optimization, it may save a little time if you create 3 lookup tables, one each for R, G, and B, to map the input value (0-255) to the output value (0-255). Looking up an array entry is probably faster than multiplying by a decimal value and rounding the result to an integer. Not sure how much faster.

Of course, this assumes that the values should always map the same.

From documentation:

from PIL import Image
Image.eval(image, function) => image

Applies the function (which should take one argument) to each pixel in the given image. If the image has more than one band, the same function is applied to each band. Note that the function is evaluated once for each possible pixel value , so you cannot use random components or other generators.

You can use .split to split the image into a separate image for each of the three channels, .eval as suggested by astynax , and Image.merge to put them back together.

If any of your multiplication factors are greater than 1, be sure to clamp the output to 255.

如果类型是 numpy.ndarray 只是 img = np.uint8(img*factor)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM