Python Forum
Brightening an image by changing pixel value (PIL image import) grok learning
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Brightening an image by changing pixel value (PIL image import) grok learning
#1
Hello again,
Currently I'm having an issue with an assignment where I must change the value of pixels to brighten an image.

Here is the full question;

"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 answer code;
from PIL import Image
file = input("File name: ")
img = Image.open(file)
for y in range(img.height):
  for x in range(img.width):
    pixel = img.getpixel((x,y))
value = img.getpixel((x,y))
img.putpixel((x,y), value + 50)
img.save("output.png")
I'm not getting any changes whatsoever in my output image. It just looks the same instead of brighter.

The image should look like this;

[Image: Qi5RlCe.png]

Unfortunately, I am getting this; Basically the default or a little darker;

[Image: cBl9s08.png]


Would love some tips as I've exhausted a few code variations with no solution thus far Huh , thanks in advance
Reply
#2
Hello

    pixel = img.getpixel((x,y))
value = img.getpixel((x,y))
What is the purpose of these seemingly same operations, why doubled?

You get all x and y pixels in the inner for loop, but you set the pixel value (+50) outside of that loop. So I would guess it only changes the last pixel it iterates over, when the for loops exit. Instead you should do that inside the loop.
Reply
#3
(Dec-06-2018, 03:21 PM)j.crater Wrote: Hello

    pixel = img.getpixel((x,y))
value = img.getpixel((x,y))
What is the purpose of these seemingly same operations, why doubled?

You get all x and y pixels in the inner for loop, but you set the pixel value (+50) outside of that loop. So I would guess it only changes the last pixel it iterates over, when the for loops exit. Instead you should do that inside the loop.

Hi, thank you for your help~

When removing the line for inner for loop I get error messages, same goes for it I added the 50 and then remove it for the outside of the loop. Vice versa gives me errors as well. Leaving them both gives me no error besides not brightening the image.

I decided to just leave that code alone since it isn't doing anything. I tried something a little different with the code here;

from PIL import Image
file = input("File name: ")
img = Image.open(file)
width, height = img.size
value = img.getpixel(img.size)
img.putpixel(value + 50)
img.save("output.png")
I get this error however;
"Traceback (most recent call last):
File "program.py", line 5, in <module>
value = img.getpixel(img.size)
File "/opt/python-3.6/lib/python3.6/site-packages/PIL/Image.py", line 1227, in getpixel
return self.im.getpixel(xy)
IndexError: image index out of range"

I've never seen it before so I'm not sure what it means? I feel like I'm just making things worse at this point.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Image processing MNO 0 6,521 Apr-27-2022, 06:34 AM
Last Post: MNO
  how to save an image on python perrieS 1 1,601 Dec-13-2020, 11:54 PM
Last Post: Larz60+
  What does image.setMask do? A01 3 2,199 Dec-08-2020, 07:06 AM
Last Post: ndc85430
  Image data cannot be converted to float rakeshd3 1 7,032 Mar-17-2020, 08:01 AM
Last Post: buran
  Convert text from an image to a text file Evil_Patrick 5 4,217 Jul-30-2019, 07:57 PM
Last Post: DeaD_EyE
  Find the centroids of all the stars in a fits image TaikiBessho 2 2,576 Jul-27-2019, 07:18 AM
Last Post: TaikiBessho
  code to retrieve an image gave me an error matthew18 1 1,795 Apr-04-2019, 05:38 PM
Last Post: nilamo
  Changing "import numpy as np" to "from numpy import" ClintWestwood 4 6,315 Nov-28-2018, 06:45 AM
Last Post: nilamo
  Image Puzzle Digitalchemist 6 7,220 Jun-05-2017, 07:56 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020