1

I'm confused on how to convert an image to grayscale.

This is the image I'm working with: https://i.stack.imgur.com/kTvyR.png and we have a .py file as well to use for this problem which is https://pastebin.com/VNNRacBx (It was premade).

import cImage as image

img = image.Image("testimage.gif")
win = image.ImageWin(img.getWidth(), img.getHeight())
img.draw(win)
img.setDelay(1,15)  

for row in range(img.getHeight()):
    for col in range(img.getWidth()):
        p = img.getPixel(col, row)

        newred = 255 - p.getRed()
        newgreen = 255 - p.getGreen()
        newblue = 255 - p.getBlue()

        newpixel = image.Pixel(newred, newgreen, newblue)

        img.setPixel(col, row, newpixel)

img.draw(win)
win.exitonclick()

That's what I have so far, a question I have is how would I convert it into a gray scale? In the workbook it says I can create a gray scale pixel by averaging the red, green and blue intensities and then using that value for all of em but not sure how exactly that's done.

And whenever I run it, why would I get an error with the img.SetDelay(1,15)? I can't quite see what's wrong with it.

This is absolute beginner Python class I'm taking this Summer semester, I tried looking up tutorials and YouTube videos on this but all of them are much more complex than what I'm dealing with and I don't understand what they're doing at all. Would I do something like

grayscaleWeighted(image, redWeight, greenWeight, blueWeight) ?

And another question, is there any websites recommended to help me learn some of this? So far I've done a bit of Python Modules, Functions, Turtle Graphics and Selection. Any help is appreciated, cheers.

2 Answers2

0

The python imaging library(PIL) has a greater variety of tools, to convert an image to grayscale, you can do:

from PIL import Image 
image_file = Image.open("testimage.gif")
image_file = image_file.convert('L') # convert image to grayscale
image_file.save('testimage.gif')

Download PIL/pillow(PIL fork) by:

pip install Pillow

http://pillow.readthedocs.io/en/4.1.x/reference/Image.html#PIL.Image.Image

Coming from cImage source codes, you can also do this instead of using PIL:

from cImage import *

def grayPixel(p):
    avg = (p.getRed() + p.getGreen() + p.getBlue()) // 3
    return Pixel(avg,avg,avg)

def makeGrayScale(imageFile):
    myimagewindow = ImageWin("Image Processing",600,200)
    oldimage = Image(imageFile)            
    oldimage.draw(myimagewindow)

    width = oldimage.getWidth()
    height = oldimage.getHeight()
    newim = EmptyImage(width,height)

    for row in range(height):
        for col in range(width):
            originalPixel = oldimage.getPixel(col,row)
            newPixel = grayPixel(originalPixel)
            newim.setPixel(col,row,newPixel)

    newim.setPosition(width+1,0)
    newim.draw(myimagewindow)
    myimagewindow.exitOnClick()

makeGrayScale('testimage.gif')
Taku
  • 31,927
  • 11
  • 74
  • 85
  • I've seen a lot of examples of PIL but the teacher said we can't use it for this question as we haven't even discussed it yet. The teacher said we have to do it similar to examples he provided which is basically similar to what I posted. – Zanders2001 May 10 '17 at 07:17
0

You can check here. To convert RGB image to grayscale instead of implementing your own method you can you do

from PIL import Image
img = Image.open('image.png').convert('LA')
img.save('greyscale.png')

It would be great if you can ask specifically the topics on which you need help. Till then the best place to learn about any of these would be the official docs of respective topics. From there you can reach out to their forums for help or other discussions.

Community
  • 1
  • 1
radbrawler
  • 2,391
  • 2
  • 15
  • 22
  • I've seen a lot of examples of PIL but the teacher said we can't use it for this question as we haven't even discussed it yet. The teacher said we have to do it similar to examples he provided which is basically similar to what I posted. And it's mostly just Turtle Graphics that I don't understand. I feel like I have to look it up online in order to do anything, even now I can't remember how to do a basic square / lines. – Zanders2001 May 10 '17 at 07:18