0

i'm new to programming and have recently started coding in python. i'm working with a textbook that has codes with sample programs in it. the following code is from that book and is supposed to turn a colored picture into a black and white picture by assigning the colors black or white to every pixel depending on their brightness (the sum of their RGB-values).

    from tkinter import *
    def black_white ():
average = 382.5
for x in range (image.width()):
    for y in range (image.height()):
        c = image.get(x, y)
        brightness = int(c[0]) + int(c[1]) + int(c[2])
        if brightness < average:
            image.put("black", (x))
        else:
            image.put("white", (x))

     window = Tk()
    image = PhotoImage(file="1.gif")
    button = Button(master=window, command=black_white,
            font=("Arial", 14),
            text="Bearbeiten")

    label = Label(master=window, image=image)
    label.pack()
    button.pack(fill=X)
    window.mainloop()

however, the code doesn't work, the error message i get is:

    Exception in Tkinter callback
    Traceback (most recent call last):
      File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1550, in __call__
return self.func(*args)
      File "/Users/(anonymous)/Desktop/programmieren/raspberry_lehrbuch/schwarzweiß.pyw", line 8, in black_white
brightness = int(c[0]) + int(c[1]) + int(c[2])
    ValueError: invalid literal for int() with base 10: ' '

i already did a bit of research but couldn't find anything that made it work. help is very much appreciated :) oh and i use a macbook pro 2010 with el capitan

1
  • error means you try to convert empty string into integer. Try int("") and you get the same error message. Check c before you do brightness = int(c[0]) + int(c[1]) + int(c[2]) Commented Dec 15, 2016 at 18:53

1 Answer 1

1

change (x) to (x,y), must be position x, y

from tkinter import *


def black_white():
    average = 382.5
    for x in range(image.width()):
        for y in range(image.height()):
            c = image.get(x, y)
            brightness = int(c[0]) + int(c[1]) + int(c[2])
            if brightness < average:
                image.put("black",  (x, y))
            else:
                image.put("white", (x, y))


window = Tk()
image = PhotoImage(file="1.gif")
button = Button(master=window, command=black_white,
                font=("Arial", 14),
                text="Bearbeiten")

label = Label(master=window, image=image)
label.pack()
button.pack(fill=X)
window.mainloop()

Before clicked:

enter image description here

After Clicked:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.