简体   繁体   English

更改/存储的像素值不会使输出图像变亮(使用 PIL 导入和 grok learning python hw)?

[英]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).我坚持这个 grok 学习问题(python 版本)。 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.这是我计算输入文件像素值的最接近位置,然后使用该信息添加 50,使图像变得更亮。

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.编写一个程序,将给定图像中每个像素的值加 50。

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.您的程序应该要求用户输入要读取的图像文件的名称,然后创建一个名为 output.png 的新输出文件,其中包含亮化后的图像。

Here's an example where the user selects the given yawn.png.下面是用户选择给定的 yawn.png 的示例。

File name: yawn.png"文件名称:打哈欠.png”

Here is my current code which calculates the image's pixels before adding the value of 50;这是我当前的代码,它在添加值 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;我已将其更改为此,因为我使用了获取/放置像素但没有任何反应,或者我在 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:基本上你可以用 lambda 函数来做到这一点:

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这是供将来使用的 lambda 支持页面: https ://www.w3schools.com/python/python_lambda.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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