diff --git a/src/PIL/BmpImagePlugin.py b/src/PIL/BmpImagePlugin.py
index cdef77ced..85e2350c5 100644
--- a/src/PIL/BmpImagePlugin.py
+++ b/src/PIL/BmpImagePlugin.py
@@ -321,12 +321,15 @@ def _save(im, fp, filename, bitmap_header=True):
     # bitmap header
     if bitmap_header:
         offset = 14 + header + colors * 4
+        file_size = offset + image
+        if file_size > 2 ** 32 - 1:
+            raise ValueError("File size is too large for the BMP format")
         fp.write(
-            b"BM"
-            + o32(offset + image)  # file type (magic)
-            + o32(0)  # file size
-            + o32(offset)  # reserved
-        )  # image data offset
+            b"BM"  # file type (magic)
+            + o32(file_size)  # file size
+            + o32(0)  # reserved
+            + o32(offset)  # image data offset
+        )
 
     # bitmap info header
     fp.write(

Large BMP files crash (i.e. more than 2**32 pixels)
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):
```python
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) ) #1 SMP Wed Mar 20 20:18:21 GMT 2019
* PIP: 18.1
* Python: 3.7.3
diff --git a/Tests/test_file_bmp.py b/Tests/test_file_bmp.py
index 76cd98aba..338f52cd7 100644
--- a/Tests/test_file_bmp.py
+++ b/Tests/test_file_bmp.py
@@ -41,6 +41,13 @@ class TestFileBmp(PillowTestCase):
             self.assertEqual(im.size, reloaded.size)
             self.assertEqual(reloaded.format, "BMP")
 
+    def test_save_too_large(self):
+        outfile = self.tempfile("temp.bmp")
+        with Image.new("RGB", (1, 1)) as im:
+            im._size = (37838, 37838)
+            with self.assertRaises(ValueError):
+                im.save(outfile)
+
     def test_dpi(self):
         dpi = (72, 72)
 

pip install -e .[dev]
parse_log_pytest
requirements.txt
apt-get update apt-get install -y gcc libjpeg-dev zlib1g-dev libtiff5-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev libharfbuzz-dev libfribidi-dev libxcb1-dev
3.9
requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
alabaster==0.7.16
babel==2.17.0
black==25.1.0
blessed==1.20.0
build==1.2.2.post1
certifi==2025.1.31
charset-normalizer==3.4.1
check-manifest==0.50
click==8.1.8
coverage==7.8.0
coveralls==4.0.1
docopt==0.6.2
docutils==0.21.2
exceptiongroup==1.2.2
idna==3.10
imagesize==1.4.1
importlib_metadata==8.6.1
iniconfig==2.1.0
jarn.viewdoc==2.7
Jinja2==3.1.6
MarkupSafe==3.0.2
mypy-extensions==1.0.0
olefile==0.47
packaging==24.2
pathspec==0.12.1
-e git+https://github.com/python-pillow/Pillow.git@209faf1b62fa7ed6c9d8b2fc7bd46e1c7e54e0bd#egg=Pillow
platformdirs==4.3.7
pluggy==1.5.0
pycodestyle==2.13.0
pyflakes==3.3.1
Pygments==2.19.1
pyproject_hooks==1.2.0
pyroma==4.2
pytest==8.3.5
pytest-cov==6.0.0
requests==2.32.3
six==1.17.0
snowballstemmer==2.2.0
Sphinx==7.4.7
sphinx-rtd-theme==3.0.2
sphinxcontrib-applehelp==2.0.0
sphinxcontrib-devhelp==2.0.0
sphinxcontrib-htmlhelp==2.1.0
sphinxcontrib-jquery==4.1
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==2.0.0
sphinxcontrib-serializinghtml==2.0.0
tomli==2.2.1
trove-classifiers==2025.3.19.19
typing_extensions==4.13.0
urllib3==2.3.0
wcwidth==0.2.13
zipp==3.21.0

name: Pillow
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tzdata=2025a=h04d1e81_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - alabaster==0.7.16
      - babel==2.17.0
      - black==25.1.0
      - blessed==1.20.0
      - build==1.2.2.post1
      - certifi==2025.1.31
      - charset-normalizer==3.4.1
      - check-manifest==0.50
      - click==8.1.8
      - coverage==7.8.0
      - coveralls==4.0.1
      - docopt==0.6.2
      - docutils==0.21.2
      - exceptiongroup==1.2.2
      - idna==3.10
      - imagesize==1.4.1
      - importlib-metadata==8.6.1
      - iniconfig==2.1.0
      - jarn-viewdoc==2.7
      - jinja2==3.1.6
      - markupsafe==3.0.2
      - mypy-extensions==1.0.0
      - olefile==0.47
      - packaging==24.2
      - pathspec==0.12.1
      - platformdirs==4.3.7
      - pluggy==1.5.0
      - pycodestyle==2.13.0
      - pyflakes==3.3.1
      - pygments==2.19.1
      - pyproject-hooks==1.2.0
      - pyroma==4.2
      - pytest==8.3.5
      - pytest-cov==6.0.0
      - requests==2.32.3
      - six==1.17.0
      - snowballstemmer==2.2.0
      - sphinx==7.4.7
      - sphinx-rtd-theme==3.0.2
      - sphinxcontrib-applehelp==2.0.0
      - sphinxcontrib-devhelp==2.0.0
      - sphinxcontrib-htmlhelp==2.1.0
      - sphinxcontrib-jquery==4.1
      - sphinxcontrib-jsmath==1.0.1
      - sphinxcontrib-qthelp==2.0.0
      - sphinxcontrib-serializinghtml==2.0.0
      - tomli==2.2.1
      - trove-classifiers==2025.3.19.19
      - typing-extensions==4.13.0
      - urllib3==2.3.0
      - wcwidth==0.2.13
      - zipp==3.21.0
prefix: /opt/conda/envs/Pillow

Tests/test_file_bmp.py::TestFileBmp::test_save_too_large
Tests/test_file_bmp.py::TestFileBmp::test_dpi Tests/test_file_bmp.py::TestFileBmp::test_invalid_file Tests/test_file_bmp.py::TestFileBmp::test_load_dib Tests/test_file_bmp.py::TestFileBmp::test_load_dpi_rounding Tests/test_file_bmp.py::TestFileBmp::test_rgba_bitfields Tests/test_file_bmp.py::TestFileBmp::test_sanity Tests/test_file_bmp.py::TestFileBmp::test_save_bmp_with_dpi Tests/test_file_bmp.py::TestFileBmp::test_save_dib Tests/test_file_bmp.py::TestFileBmp::test_save_dpi_rounding Tests/test_file_bmp.py::TestFileBmp::test_save_to_bytes
src/PIL/BmpImagePlugin.py
src/PIL/BmpImagePlugin.py