I am getting an Atribute error that the object image does not have atibute open when I import using PIL

Question:

I want to rotate an image but I can’t even open it. The answer I found does not work for me. I tried doing import PIL.Image but that doesn’t work either. Any answers would be very helpful

from tkinter import *
from time import *
from math import *
from random import * 
from PIL import *



imageCar = Image.open("C:\Users\Antonio\Desktop\Python Programs\GameFinal\car.png")
Asked By: Tontonio3

||

Answers:

It’s considered bad practice to use * when importing from a package, exactly because it just dumps everything from that package into your code. If two different packages have something with the same name (like in your code tkinter.Image and PIL.Image), you get some strange errors.

It’s unfortunate that the documentation for tkinter uses from tkinter import * as an example.

What you want is to selectively import what you need:

# Only import what you need. When imported like this, you
# use its name directly
from tkinter import TK
from tkinter import ttk
root = Tk()

If I’m calling an imported function just a couple time throughout my code, I usually import it like so as to not pollute my namespace:

import tkinter
from tkinter import ttk

# Prefix it with the package name you used to import
root = tkinter.Tk()

A pretty good tutorial on importing is

https://www.digitalocean.com/community/tutorials/how-to-import-modules-in-python-3

Answered By: Leonardo
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.