-1

I am trying to invert an image using PIL.ImageOps.invert(image), however I am receiving the following error: AttributeError: 'str' object has no attribute 'mode'

Here is my code:

# allows user to select image file from local
photo = filedialog.askopenfilename(initialdir="/", title="Select file", filetypes=(("png files", "*.png"), ("all files", "*.*")))
print(photo)

# creates canvas to display the image
canvas = Canvas(width=400, height=400)
canvas.pack()

inverted_image = PIL.ImageOps.invert(photo)

# reference to image object so it doesn't blanks it
label = Label(image=inverted_image)
label.image = inverted_image

canvas.create_image(200, 200, image=inverted_image, anchor=CENTER)

What I am trying to do here is to invert the image that was selected by the user and display it. I've seen others with similar problem but I have yet to see one with 'mode' error (maybe I missed it).

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Yu.L
  • 49
  • 2
  • 7

1 Answers1

1

You are passing a string object containing a filename to the function. The value is set here:

photo = filedialog.askopenfilename(initialdir="/", title="Select file", filetypes=(("png files", "*.png"), ("all files", "*.*")))

photo is a string, not an image object. Open the image first, using the PIL.Image.open() function:

img = PIL.Image.open(photo)

then apply operations on img.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343