简体   繁体   中英

In Python I can't open some png images properly using PIL

I have two png images and if i try to open them (here is the code)

from PIL import Image 

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

The first line opens the image normally as png, while the second line one automatically opens the image as jpeg

Here is the output:

"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

I just want to open both files as png, in order to use RGBA to make transparent background.

Someone could help me please?

The extension of a file is not necessarily a proper indicator of its content - it is just a Windows concept. Most files have a recognisable "magic number" , or "signature" , at the start.

So, try dumping the contents your "unhappy" PNG file in hex, or opening it in an online hex editor like hexedit if you don't have a locally-installed hex tool available.

For PNG, you should see a valid PNG signature like this:

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

For JPEG, you should see a valid JPEG signature , like this:

ff d8 ff ...

Use this display(x) and display(o) If not working check the directories.

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