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

Large BMP files crash (i.e. more than 2**32 pixels) #4274

Closed
nmz787 opened this issue Dec 17, 2019 · 4 comments · Fixed by #4283
Closed

Large BMP files crash (i.e. more than 2**32 pixels) #4274

nmz787 opened this issue Dec 17, 2019 · 4 comments · Fixed by #4283

Comments

@nmz787
Copy link

nmz787 commented Dec 17, 2019

I started asking on StackOverflow about my original problem, and the code is pasted over there:
https://stackoverflow.com/questions/59353721/python-pillow-opening-1-bit-depth-files-with-8-bits

but after moving to a larger machine (256 GB RAM) I moved on from the original issue and onto an Exception in the code.

Basically I'm trying to save a 59GigaPixel single-channel (1 bit depth) BMP file, which is yielding this error:

Traceback (most recent call last):
  File "stitch_bmps.py", line 75, in <module>
    blank.save("59GP.bmp", "BMP")
  File "/home/nmz787/.local/lib/python3.7/site-packages/PIL/Image.py", line 2084, in save
    save_handler(self, fp, filename)
  File "/home/nmz787/.local/lib/python3.7/site-packages/PIL/BmpImagePlugin.py", line 332, in _save
    + o32(offset)  # reserved
  File "/home/nmz787/.local/lib/python3.7/site-packages/PIL/_binary.py", line 91, in o32le
    return pack("<I", i)
struct.error: 'I' format requires 0 <= number <= 4294967295

Here's a small example to reproduce easily (requires at least 61GB of RAM, or RAM+swap):

from PIL import Image
a = Image.new("1", (247246, 247246))
a.save("blank_59GP.bmp", "BMP")

What are your OS, Python and Pillow versions?

  • OS: Linux version 4.9.164-vs2.3.9.8-beng (root@ex64-jessie) (gcc version 4.9.2 (Debian 4.9.2-10+deb8u2) ) Merge pull request from @shuge #1 SMP Wed Mar 20 20:18:21 GMT 2019
  • PIP: 18.1
  • Python: 3.7.3
@radarhere radarhere changed the title Large BMP files (i.e. more than 2**32 pixels) crashes Pillow Large BMP files crash (i.e. more than 2**32 pixels) Dec 17, 2019
@nmz787
Copy link
Author

nmz787 commented Dec 17, 2019

Pillow Version: 6.2.1

@kkopachev
Copy link
Contributor

Looks like BMP max filesize is limited to 4GiB. The second field in BMF file header is filesize in bytes, which is 4 bytes int, which can not exceed 2^32 (4GiB). Since your image is 1bpp image, with given dimensions image size would have to be 7,641,879,368 bytes, which exceeds 4 byte int capacity.

@nmz787
Copy link
Author

nmz787 commented Dec 19, 2019

Thanks for the clarification, can we get this notice added to the code during the save call, so it's absolutely clear what the problem is (user error).

@radarhere
Copy link
Member

Thanks @kkopachev for figuring this one out.

I've created PR #4283 to throw a clearer error.

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

Successfully merging a pull request may close this issue.

3 participants