python - 在 Python 中,我无法使用 PIL 正确打开一些 png 图像

标签 python python-3.x python-imaging-library png jpeg

我有两个 png 图像,如果我尝试打开它们(这是代码)

from PIL import Image 

x = Image.open('png/x-tictactoe.png')
o = Image.open('png/o-tictactoe.png')
print(x)
print(o)

第一行正常打开图像为png,第二行自动打开图像为jpeg

这是输出:

"C:\Users\Delfino De Chicchis\PycharmProjects\TicTacToe\venv\Scripts\python.exe" "C:/Users/Delfino De Chicchis/PycharmProjects/TicTacToe/test.py"
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=860x898 at 0x27FA46330A0>
<PIL.JpegImagePlugin.JpegImageFile image mode=L size=880x1004 at 0x27FA67E8C10>

Process finished with exit code 0

我只想将这两个文件都以 png 格式打开,以便使用 RGBA 制作透明背景。

有人可以帮助我吗?

最佳答案

文件的扩展名不一定是其内容的正确指示符 - 它只是一个 Windows 概念。大多数文件都有 recognisable “魔数(Magic Number)”,或“签名”,位于开头。

因此,请尝试将您的“不开心” PNG 文件转储为十六进制,或者在在线十六进制编辑器(例如 hexedit)中打开它。如果您没有本地安装的可用十六进制工具。

对于 PNG,您应该看到有效的 PNG signature像这样:

89 50 4e 47 0d 0a 1a 0a ... (in ASCII 50="P", 4e="N", 47="G")

对于 JPEG,您应该看到有效的 JPEG signature ,像这样:

ff d8 ff ...

关于python - 在 Python 中,我无法使用 PIL 正确打开一些 png 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66372957/

相关文章:

python - 如何从 numpy 数组中选择两个边界之间的点?

python - 列表中元组值之间的关系根据它们的位置

python - 调整PIL图像大小: ValueError: Unknown resampling filter

python - 在 CentOS 6.6 上安装 Pillow 时出错

Python/NumPy : implementing a running sum (but not quite)

python - matplotlib/Python 中条形图的单独标记条

python - 如何将对象存储在动态生成的变量中?

python - 如何使用for循环 append 从python中的函数生成的数据框

python - 从多索引数据框中删除特定行

python - 图像尺寸突然缩小