简体   繁体   中英

Changed/stored pixel value won't brighten output image (using PIL import with grok learning python hw)?

I'm stuck with this grok learning question(python version). I'm supposed to change the pixel value of an image to make it brighter based on the file the user inputs. I've been stuck on this for over a week now going through various iterations of code.

Everytime I've come close, I hit a stop. This is the closest I've gotten where I calculate the input file's pixel value, then use that information to add 50 so that the image becomes brighter.

Here are the instructions specifically;

"If we increase the value of each pixel in the image by the same amount, the image will appear brighter.

Write a program to add 50 to the value of each pixel in a given image.

Your program should ask the user to type in the name of the image file to read, and then create a new output file called output.png which contains the brightened image.

Here's an example where the user selects the given yawn.png.

File name: yawn.png"

Here is my current code which calculates the image's pixels before adding the value of 50;

`

from PIL import Image
file = input("File name: ")
img = Image.open(file)
width, height = img.size
value = int(width * height + 50)
print(value)

`

I've changed it to this because I have use get/put pixel but nothing happens or I get errors with the value put in the img.putpixel;

`

from PIL import Image
file = input("File name: ")
img = Image.open(file)
width, height = img.size
value = img.getpixel(int(width * height))
img.putpixel(int(width * height), value + 50)
img.save("output.png")

`

An example of how the output image should look is this; Brightened cat image

But this is what I keep getting; Incorrect same cat default image

I'm just at a loss. No one in my class has gotten this far as of yet either, so they haven't been able to help. So hopefully if I figure this out, I'll be able to help someone else. Any ideas? Thanks in advance.

Basically you can do this with a lambda function:

from PIL import Image, ImageFilter
im1 = Image.open (input("File name: "))
im2 = im1.point(lambda p: p + 50)
im2.save('output.png')

its is the fastest and easiest method. Here is lambda support page for future use: https://www.w3schools.com/python/python_lambda.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM