Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to open DDS image: Unimplemented pixel format #7423

Closed
jmctune opened this issue Sep 26, 2023 · 8 comments · Fixed by #7426
Closed

Unable to open DDS image: Unimplemented pixel format #7423

jmctune opened this issue Sep 26, 2023 · 8 comments · Fixed by #7426

Comments

@jmctune
Copy link

jmctune commented Sep 26, 2023

What did you do?

Attempted to read the attached DDS image.

What did you expect to happen?

Expected it to render in my tkinter GUI.

What actually happened?

Received the following error:

NotImplementedError: Unimplemented pixel format b'\x00\x00\x00\x00'

What are your OS, Python and Pillow versions?

  • OS: Windows 11
  • Python: 3.11 64-bit
  • Pillow: 10.0.1
from PIL import ImageTk

with open ("test.dds", "rb") as f:
  data = f.read()

image = ImageTk.PhotoImage(data=data)

Additional notes

I'm guessing the version of DDS that I am attempting to render is not supported solely based off of the error message. I am passing byte data and not the files themselves, but am unsure of how else to get this to render. Attempted to use Pillow, but no luck.

Attached zip with dds image inside:

@radarhere
Copy link
Member

How was the DDS image file created?

"b'\x00\x00\x00\x00'" is not what I expect for a pixel format. Is there a program you know of that can view the file successfully? Does that program list any information about what the pixel format is? I'm expecting a short string starting with DXT, ATI or BC.

@jmctune
Copy link
Author

jmctune commented Sep 26, 2023

Hello and thanks for commenting so quickly.

This DDS file is used in a video game and dumped directly from its archive. I'm attempting to read and render the image in a GUI.

This image opens in both GIMP and paint.net correctly. In GIMP, I see the following:

image

image

Hopefully this provides the answer you're looking for, but please let me know if I can provide more.

@jmctune
Copy link
Author

jmctune commented Sep 26, 2023

In having no idea what I'm doing, I came across where PIL is reading the fourcc value and updated where it's checking the dds file to each of the supported values (DXT1, DXT3, DXT5, ATI1, ATI2, BC5S, DX10) just in case the original image had left off this data or something.

Thinking that maybe GIMP/paint.net didn't care, but PIL does. GIMP and paint.net render the correct image for all of the different fourcc values, except for DX10 - which doesn't open correctly.

PIL renders an image when I use any of the above values (except for DX10), but it's just noise.

This is a different dds file, but the exact same format:

Expected (in GIMP):
image

PIL (using DXT1 and rendered in my GUI. All formats show some variation of this):
image

My guess is PIL just doesn't support this DDS format.

image

@radarhere
Copy link
Member

I've realised that the spec describing the pixel format flags states that DDPF_FOURCC indicates that "dwFourCC contains valid data" - the image does not have this flag in pfflags, and this explains why fourcc is empty.

pfflags instead lines up with

DDPF_PALETTEINDEXED8 = 0x20

meaning there is palette data in the image. Gimp agrees with this, telling me in the "Image" menu that the "Image" "Mode" is "Indexed..."

DDPF_PALETTEINDEXED8 is not mentioned in the latest spec, but is mentioned in https://learn.microsoft.com/en-us/previous-versions/ms783269(v=vs.85). I guess this file is something that Microsoft has removed official support for.

@radarhere
Copy link
Member

radarhere commented Sep 27, 2023

I guessed that the file structure is the header, followed by 1024 palette bytes, followed by the indexes to the palette, and created PR #7426. For your image,

from PIL import Image
with Image.open("test.dds"):
    im.save("out.png")

now gives
out

Which matches what I see in GIMP for that DDS file (although GIMP has a black background instead of a transparent one).

This DDS file is used in a video game

Presumably this means the image can't be included in our test suite and distributed under the Pillow license. Instead I've manually put together a file with the same structure.

@jmctune
Copy link
Author

jmctune commented Sep 27, 2023

Thanks radar - I really appreciate you digging into this. That is the correct image. GIMP renders it with a black background, while paint.net renders it with a transparent one (transparent should be correct).

Presumably this means the image can't be included in our test suite and distributed under the Pillow license. Instead I've manually put together a file with the same structure.

Yes, this is correct. I do not own the rights to this image.

I just tried to apply your PR as a patch in my virtual environment, but when trying to run the same script above, I am now seeing this error:

ValueError: unknown raw mode for given image mode

I ran the same script you used above:

from PIL import Image
with Image.open("test.dds") as f:
    f.save("out.png")

Does something have to be done differently to verify this? I double-checked that the patch did indeed apply correctly:

image

Without the patch, I see the same error as before (Unimplemented pixel format..).

@radarhere
Copy link
Member

My PR takes #7307 into consideration, which hasn't been released yet.

Just change self._mode = "P" to self.mode = "P", removing the underscore.

@jmctune
Copy link
Author

jmctune commented Sep 27, 2023

Worked like a charm! Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants