问题
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!
回答1:
Here is how to do it:
- First split your image into the RGB channels.
- Use
point
to multiply a channel by factor (1.5
in the example, on the r channel). - 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..):
回答2:
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.
回答3:
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.
回答4:
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.
回答5:
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.
来源:https://stackoverflow.com/questions/10885984/multiply-each-pixel-in-an-image-by-a-factor