-1

I am trying to convert some of my pictures to black and white. I have this so far

import image 

def black_and_white(pic): 

    for y in range(pic.getHeight()): 
        for x in range(pic.getWidth()): 
            p = pic.getPixel(x,y) 

            r = p.getRed() 
            g = p.getGreen()
            b = p.getBlue()

            if x > 0.128:
                x = .255 * r + .255 * g +.255 * b
            else: 
                x = .0 * r + .0 * g +.0 * b

            x = int(x) ## to convert it to an integer 
            newp = image.Pixel(x, x, x) ## to convert to a new pixel 
            pic.setPixel(x, y, newp) 
    return pic 

def main(): 

    bell = image.Image("luther.jpg") 

    width = bell.getWidth() 

    height = bell.getHeight()

    win = image.ImageWin(width, height) 

    bell.draw(win) 

    gs_bell = grayscale(bell)

    gs_bell.draw(win) 


main() ## starts execution 

If anyone could give me some advice, I would greatly appreciate it!

I am truly sorry for the lack of clarity. Here is the image I am getting. enter image description here

  • What do you want help with? Where is the code you posted failing? – Simon Crane Apr 08 '20 at 15:58
  • Please read [ask]. Rather than have us try to figure out what your question is, state it clearly in your post. The more information you give the better: are we dealing with a syntax error? Unexpected output? – Cris Luengo Apr 08 '20 at 19:22
  • Do you truly want a black and white image, with only 2 color values (black and white)? Or do you want a gray scale image with shades of gray? – Rusty Widebottom Apr 08 '20 at 23:17
  • Rusty, yes, it is a black and white image with only 2 color values. Cris, sorry for not stating my problem clearly. I read over the 'how to ask' and will make sure I am more clear with my for future posts. Thank you for giving me that link. Simon, my code isn't failing per say, it just isn't giving me a black and white photo. Thank you for checking this out for me! – Jack Lance Apr 09 '20 at 15:35

2 Answers2

0

Your code has numerous bugs. You're using x as a pixel value, when it is actually a pixel coordinate.

I would change this:

            if x > 0.128:
                x = .255 * r + .255 * g +.255 * b
            else: 
                x = .0 * r + .0 * g +.0 * b

            x = int(x) ## to convert it to an integer 
            newp = image.Pixel(x, x, x) ## to convert to a new pixel

to this:

            v = 0.2989 * r + 0.5870 * g + 0.1140 * b
            if v > 128.0:
                v = 255
            else: 
                v = 0

            v = int(v) ## to convert it to an integer 
            newp = image.Pixel(v, v, v) ## to convert to a new pixel

The RGB weighting values come from this article.

Rusty Widebottom
  • 985
  • 2
  • 5
  • 14
  • Thank you for your response, but I am still receiving the same out put /Users/JackLance/Desktop/Screen Shot 2020-04-09 at 2.47.02 PM.png – Jack Lance Apr 09 '20 at 19:53
  • Your code listing contains many bugs. Another bug is that you're never actually calling `black_and_white()`. You do call something called `grayscale()`. Also, your program contains no lines to write out the modified image. How about you fix some of those bugs, and post an updated version of your program in the question? – Rusty Widebottom Apr 09 '20 at 21:04
0

Here's a complete working example, using PIL (because I don't know where your import image is coming from, you didn't say).

import sys
import argparse
import PIL.Image # https://pillow.readthedocs.io/en/3.1.x/reference/Image.html
import os

def black_and_white(pic): 
    for y in range(pic.size[1]): 
        for x in range(pic.size[0]): 
            r, g, b = pic.getpixel((x, y)) 

            v = 0.2989 * r + 0.5870 * g + 0.1140 * b
            if v > 128.0:
                v = 255
            else: 
                v = 0

            v = int(v) ## to convert it to an integer 
            pic.putpixel((x, y), (v, v, v)) 


def main(options):
    try:
        image = PIL.Image.open(options.filename)
    except:
        print('ERROR: Could not open %s' % (options.filename))
    else:
        black_and_white(image)
        basename = os.path.splitext(options.filename)[0]
        image.save(basename + '_solution' + '.jpg', 'JPEG')

    return 0 

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument(
        'filename',
        help='Image file.')
    options = parser.parse_args()
    sys.exit(main(options))

This works for me: enter image description here

Rusty Widebottom
  • 985
  • 2
  • 5
  • 14