如何用 python 打开 png 和 jpg 文件?

how can i open png and jpg files with python?

我需要一个程序来打开 png 文件。我在网上找到了这个,但这给了我一个错误。

from PIL import Image

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

im.show()

这是错误:

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

有人解决这个问题吗?

解决方案是不要将文件命名为与该文件中导入的任何模块相同的名称。

在此处的示例中阅读更多内容:

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

我使用 matplotlib.image 作为 mpimgmatplotlib.pyplot 作为 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()