So I have the following code, that takes an image and modifies the RGB channels one at a time. It then creates a list of 9 images with the various modified channels.
The Red channel is modified by 0.1, then by 0.5, then by 0.9. This is then applied to the Green channel then the Blue Channel.
Resulting in a list of 9 images with the same modifications, but to different channels. The code works, but I just feel like there is a lot of repetition, and this really bugs me.
#%%
from PIL import Image
# Set the file and open it
file = "Final/charlie.png"
#Create an empty list to append the images to
images = []
#Image processing function, depending on the counter
def image_processing0(a):
if count == 0 or count == 3 or count == 6:
c = int(a * 0.1)
return c
elif count == 1 or count == 4 or count == 7:
c = int(a * 0.5)
return c
elif count == 2 or count == 5 or count == 8:
c = int(a * 0.9)
return c
#Set the count to 0
count = 0
#Create PixelMap
while count <9:
#Creat Pic at the start of each loop
pic = Image.open(file)
#Convert the image to RGB, so we dont have to deal with the alpha channel
pic = pic.convert('RGB')
#Get the RGB value for every pixel
for x in range(pic.size[0]):
for y in range(pic.size[1]):
#Split the RGB value into variables r, g and b
r,g,b = pic.getpixel((x,y))
#Check the count, use logic to appy the processing to the correct channel
if count <3:
r = image_processing0(r)
pic.putpixel((x,y),(r,g,b))
elif count < 6:
g = image_processing0(g)
pic.putpixel((x,y),(r,g,b))
elif count < 9:
b = image_processing0(b)
pic.putpixel((x,y),(r,g,b))
#Add the images to the list
images.append(pic)
#Increment the counter
count+=1
Both my logic statement, and my function seem a bit repetitive:
#---------------------------------------------------
#Image processing function, depending on the counter
def image_processing0(a):
if count == 0 or count == 3 or count == 6:
c = int(a * 0.1)
return c
elif count == 1 or count == 4 or count == 7:
c = int(a * 0.5)
return c
elif count == 2 or count == 5 or count == 8:
c = int(a * 0.9)
return c
#---------------------------------------------------
#Check the count, use logic to apply the processing to the correct channel
if count <3:
r = image_processing0(r)
pic.putpixel((x,y),(r,g,b))
elif count < 6:
g = image_processing0(g)
pic.putpixel((x,y),(r,g,b))
elif count < 9:
b = image_processing0(b)
pic.putpixel((x,y),(r,g,b))
#---------------------------------------------------
I tried to change my logic to the following, but soon realized that when I put the pixel back, i had no way to specify exactly what channel I was placing back in the pic.putpixel((x,y),(r,g,b)) statement.
if count <3:
channel = r
elif count < 6:
channel = g
elif count < 9:
channel = b
channel = image_processing0(channel)
pic.putpixel((x,y),(r,g,b)) #No way to define which channel is being changed