You can handle RGB channels as a single variable color
. And your function receives a color, not a channel, returns processed color.
And, you had combined two loops into one for count in range(9)
. They should be for factor in [0.1, 0.5, 0.9]
and for channel in range(3)
. count
is meaningless in the loop. But factor
and channel
are useful. So you do not need to calculate factor
and channel
variable from count
later.
from PIL import Image
def convert_color(factor, channel):
def convert(color):
return (
*color[:channel],
int(color[channel] * factor),
*color[channel + 1:]
)
return convert
pic = Image.open('image.png').convert('RGB')
processed = []
for channel in range(3): # which channel
for factor in [0.1, 0.5, 0.9]: # factor to multiple
for channel in range(3): # which channel
new_pic = Image.new('RGB', pic.size)
new_pic.putdata(list(map(
convert_color(factor, channel),
pic.getdata()
)))
processed.append(new_pic)
new_pic.save(f'image{len(processed)}.png')
Just one more thing (not related to duplicated codes): getpixel
and setpixel
may slow. Use getdata
, putdata
as above code would be a better choice.