Skip to content

Open BMP image with black palette in mode 1#9778

Open
radarhere wants to merge 1 commit into
python-pillow:mainfrom
radarhere:bmp
Open

Open BMP image with black palette in mode 1#9778
radarhere wants to merge 1 commit into
python-pillow:mainfrom
radarhere:bmp

Conversation

@radarhere

Copy link
Copy Markdown
Member

Saving a P mode image with only black and white (in that order) and re-opening it

from PIL import Image
im = Image.new("P", (1, 1))
im.putpalette([0, 0, 0, 255, 255, 255])
im.save("out.bmp")

with Image.open("out.bmp") as reloaded:
  print(reloaded.mode)

gives an 1 mode image.

This is because of

# ------- If all colors are gray, white or black, ditch palette
if grayscale:
self._mode = "1" if file_info["colors"] == 2 else "L"

However, 1 mode is only applied if there are exactly two colours. This should be two colours or less. Otherwise, removing white actually changes the image to a more complex mode.

from PIL import Image
im = Image.new("P", (1, 1))
im.putpalette([0, 0, 0])
im.save("out.bmp")

with Image.open("out.bmp") as reloaded:
  print(reloaded.mode)

currently gives mode L. This PR changes it to mode 1 as well.

@radarhere radarhere force-pushed the bmp branch 2 times, most recently from 97d41fa to 46d32ec Compare July 7, 2026 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant