Skip to main content
edited body
Source Link
tsh
  • 495
  • 2
  • 7

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.

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 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')

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
        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.

added 123 characters in body
Source Link
tsh
  • 495
  • 2
  • 7

You can handle RGB channels as a single variable color. So you can avoid some conditions and make everything easyAnd your function receives a color, not a channel, returns processed color.

And, you may change your loophad combined two loops into one for count in range(9) into. 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 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')

You can handle RGB channels as a single variable color. So you can avoid some conditions and make everything easy.

And, you may change your loop for count in range(9) into 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.

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 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')

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 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')
Source Link
tsh
  • 495
  • 2
  • 7

You can handle RGB channels as a single variable color. So you can avoid some conditions and make everything easy.

And, you may change your loop for count in range(9) into 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.

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 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')