简体   繁体   中英

how can i open png and jpg files with python?

i need a program to open png files. I found this on the internet, but this gives me an error.

from PIL import Image

im = Image.open("D:/foto's/fbshare.png")

im.show()

this is the error:

AttributeError: partially initialized module 'PIL.Image' has no attribute 'open' (most likely due to a circular import)

does anybody have the solution to this problem?

The solution is not to name the file the same as any of the modules being imported in that file.

read more in the example here:

https://geektechstuff.com/2020/06/13/attributeerror-partially-initialized-module-has-no-attribute-python/

I use matplotlib.image as mpimg and matplotlib.pyplot as plt :

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image_path = "D:/foto's/fbshare.png"
image = mpimg.imread(image_path)
plt.imshow(image)
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM