Bottom Page

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;

İmage


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

İmage



Would love some tips as I've exhausted a few code variations with no solution thus far Huh , thanks in advance
Quote
#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.
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Changing "import numpy as np" to "from numpy import" ClintWestwood 4 91 Nov-28-2018, 06:45 AM
Last Post: nilamo
  self learning itertools for complex structures enrikS 5 570 May-18-2018, 05:53 AM
Last Post: enrikS
  Method on instance changing its representation Samsar 1 517 Dec-20-2017, 07:39 AM
Last Post: Terafy
  Changing object values to see how air resistance works Tomsom 0 347 Nov-27-2017, 05:10 PM
Last Post: Tomsom
  Learning and Looping AnjyilLee 6 1,060 Sep-23-2017, 07:43 AM
Last Post: Sagar
  Machine Learning for Applications in Architecture JinLee 0 696 Sep-20-2017, 12:54 AM
Last Post: JinLee
  Machine Learning Antivirus [Urgent] Echoo0o 4 1,172 Jul-28-2017, 01:57 PM
Last Post: nilamo
  Image Puzzle Digitalchemist 6 2,089 Jun-05-2017, 07:56 PM
Last Post: micseydel
  Learning Python from Jython NewtoPy 1 846 Jan-20-2017, 02:20 AM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 2 Guest(s)