5 Replies - 3411 Views - Last Post: 06 October 2012 - 12:35 PM Rate Topic: -----

#1 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

converting picture to black and white in python

Posted 05 October 2012 - 10:46 AM

I have an assignment and I have to switch the picture to black and white.

Here are the assignment instructions:
Write a function called blackAndWhite that has as input argument the file name of an image file (example: bluefish.gif), then transforms the image in black and white and displays it. In order to create a black and white image, do:

1. Prepare the image file: (i.e open the image file and convert it into RGB representation)

2. Display the initial image

3. Loop over each pixel in the image (hint: you'll need a nested for loop) and compute the average of r, g, b. Next, update the pixel value with average.

4. Save the new image in a file called: filename”_BW_mod.gif”

5. Display the black and white image on screen


#below is what I have so far in my code. I think I generally understand steps 1,2,4 and 5 but not really #3

def blackandwhite(filename)
	myImage = Image.open(filename)
	myImage = myImage.convert('RGB')
	myImage.show()

		# in here I am trying to figure out the nested loop and how to start it
		

	newfilename = filename[:-4]
	newfilename = newfilename + "_BW_mod.gif"
	myImage.save(newfilename)
	myImage.show(newfilename)


any suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: converting picture to black and white in python

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: converting picture to black and white in python

Posted 05 October 2012 - 12:24 PM

Hehe well, first off, your teacher is wrong! Black and white literally means black and white. This assignment is to convert the image to grayscale :-P

So, do you know how to loop over the image and look at 1 pixel at a time? Does your class use a textbook? If so, have you looked in it to see how to examine 1 pixel at a time?

Have you tried checking to see what functions are available in the module? If you go into IDLE and type help(Image) you should get a listing that might be helpful. If NONE of this helps, could you tell us where this Image module came from? I'd need to download it to see how it works :-P

EDIT:
Or is it possible that this is as simple as
for column in myImage:#could be row, I dunno
    for pixel in column:
 

This post has been edited by atraub: 05 October 2012 - 12:26 PM

Was This Post Helpful? 0
  • +
  • -

#3 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: converting picture to black and white in python

Posted 05 October 2012 - 12:41 PM

To get the pixel at a co-ordinates you need to index it, to index all the values you're going to need the width and height of the image so:
img = Image.open(myImage)
width, height = img.size

for x in range(width):
  for y in range(height):
    p = img.getPixel([x, y])
    # Process it here


I believe that's correct from the documentation. It's been a while since I used it, and I can't install it right now.

@atraub The Image module comes from PIL as I found out in another thread.

This post has been edited by Simown: 05 October 2012 - 12:41 PM

Was This Post Helpful? 0
  • +
  • -

#4 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Re: converting picture to black and white in python

Posted 06 October 2012 - 09:52 AM

I dont really understand the last post.
Was This Post Helpful? 0
  • +
  • -

#5 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: converting picture to black and white in python

Posted 06 October 2012 - 10:58 AM

Which particular bit don't you get?

I'll write it again with a few comments:
# Now, img contains myImage that I opened
img = Image.open(myImage)
# This is a Python trick, two variables are assigned at the same time
# img.size returns a value such as (100, 200) for the width and height of the image
# So width would be assigned to 100, and height 200. In pixels
width, height = img.size
# x in range(width) is a function that goes in this case from 0 to the value of width, so in my example
# 0..100 
for x in range(width):
# Now y goes from 0...height 0..200 in my example
  for y in range(height):
  # Now we are looping through all the y's for every x value so when x is 0, y= 1 then 2, then 3 .... then 99
  # Next x is 1, y= 1 then 2 then 3 ... then 99
  # As you can see this produces every possible pair of x's and y's which cover every pixel in your image
  # The getPixel() function just returns the pixel and the [x, y] that we are looping through and assigns it to "p"
    p = img.getPixel([x, y])



Hope that's a bit clearer :)

This post has been edited by Simown: 06 October 2012 - 10:59 AM

Was This Post Helpful? 0
  • +
  • -

#6 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Re: converting picture to black and white in python

Posted 06 October 2012 - 12:35 PM

Thank you! I got it working!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1