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

问题描述

我需要一个程序来打开 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 用作 mpimg,将 matplotlib.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()