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

Invalid 8-bit BMP files written when not all palette colours are used #6572

Closed
ValleyBell opened this issue Sep 12, 2022 · 2 comments · Fixed by #6500
Closed

Invalid 8-bit BMP files written when not all palette colours are used #6572

ValleyBell opened this issue Sep 12, 2022 · 2 comments · Fixed by #6500

Comments

@ValleyBell
Copy link

ValleyBell commented Sep 12, 2022

What did you do?

I loaded an image (BMP or PNG) in 8-bit format. (i.e. with a palette of up to 256 colours)
The image did use a palette with less than 256 colours.

Then I edited the image and saved it again, as 8-bit BMP.

EDIT: This may be a similar problem as #6500.

What did you expect to happen?

The saved image should be viewable by Windows' default image viewer.

What actually happened?

The saved image was corrupt and could not be displayed.

Reason: The offset calculation for the bfSize (file offset 0x02) and bfOffBits (file offset 0x0A) assumes that the palette is 256 colours large (0x400 bytes). However there are only 0x200 bytes written for the palette. (4 bytes * 128 colours)

The fields biClrUsed (file offset 0x2E) and biClrImportant (file offset 0x32) are also incorrectly set to 256 instead of 128.

What are your OS, Python and Pillow versions?

  • OS: Windows 10 1909
  • Python: Python 3.7.6
  • Pillow: 9.2.0

I attached an archive with two tests: bmp-tests.zip

  • Test 1: BMP image with 256 colours being used (works fine)
  • Test 2: BMP image with 128 colours being used (broken after resaving)
import PIL.Image

with PIL.Image.open("Test1_AllColors.bmp") as img1:
    img1.save("t1-good.bmp")

with PIL.Image.open("Test2_128Colors.bmp") as img2:
    img2.save("t2-bad.bmp")
@radarhere radarhere changed the title Invalid 8-bit BMP files written when not all palette colours are used. Invalid 8-bit BMP files written when not all palette colours are used Sep 12, 2022
@radarhere
Copy link
Member

radarhere commented Sep 12, 2022

I've added the solution for this to #6500

@ValleyBell
Copy link
Author

I can confirm that the fix from the mentioned PR fixes the issue.
Thanks a lot!

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