Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

Blue Image Filter in python

I am trying to write a function that takes a pixel as a parameter and inverts each color in the pixel then return the new color values as a tuple. Help would be much appreciated Here is my current code:

IMAGE_URL = "https://codehs.com/uploads/c709d869e62686611c1ac849367b3245"
IMAGE_WIDTH = 280
IMAGE_HEIGHT = 200

image = Image(IMAGE_URL)
image.set_position(70, 70)
image.set_size(IMAGE_WIDTH, IMAGE_HEIGHT)
add(image)

#################################################
# Write your function here. Loop through each pixel
# and set each pixel to have a zero blue value.
#################################################
def remove_blue():
    pass

# Give the image time to load
print("Removing Blue Channel ....")
print("Might take a minute....")
timer.set_timeout(remove_blue, 1000)
question from:https://stackoverflow.com/questions/65911324/blue-image-filter-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
from PIL import Image
# open image
image = Image.open('pic.jpeg')
# show the image (optional)
image.show()
# load the image into memory
image_data = image.load()
# obtain sizes
height,width = image.size
# loop over and change blue value to 0
for loop1 in range(height):
    for loop2 in range(width):
        r,g,b = image_data[loop1,loop2]
        image_data[loop1,loop2] = r,g,0

image.save('changed.jpeg')

The corresponding function would be

def remove_blue(image):
   
   image = Image.open('pic.jpeg')
   # show the image (optional)
   image.show()
   # load the image into memory
   image_data = image.load()
   # obtain sizes
   height,width = image.size
   # loop over and change blue value to 0
   for loop1 in range(height):
       for loop2 in range(width):
           r,g,b = image_data[loop1,loop2]
           image_data[loop1,loop2] = r,g,0
 # return image
   image.save('changed.jpeg')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...