diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py
index fa8c852c2..74fb69516 100644
--- a/src/PIL/TiffImagePlugin.py
+++ b/src/PIL/TiffImagePlugin.py
@@ -263,6 +263,20 @@ def _limit_rational(val, max_val):
     return n_d[::-1] if inv else n_d
 
 
+def _limit_signed_rational(val, max_val, min_val):
+    frac = Fraction(val)
+    n_d = frac.numerator, frac.denominator
+
+    if min(n_d) < min_val:
+        n_d = _limit_rational(val, abs(min_val))
+
+    if max(n_d) > max_val:
+        val = Fraction(*n_d)
+        n_d = _limit_rational(val, max_val)
+
+    return n_d
+
+
 ##
 # Wrapper for TIFF IFDs.
 
@@ -520,12 +534,22 @@ class ImageFileDirectory_v2(MutableMapping):
             else:
                 self.tagtype[tag] = TiffTags.UNDEFINED
                 if all(isinstance(v, IFDRational) for v in values):
-                    self.tagtype[tag] = TiffTags.RATIONAL
+                    self.tagtype[tag] = (
+                        TiffTags.RATIONAL
+                        if all(v >= 0 for v in values)
+                        else TiffTags.SIGNED_RATIONAL
+                    )
                 elif all(isinstance(v, int) for v in values):
-                    if all(v < 2 ** 16 for v in values):
+                    if all(0 <= v < 2 ** 16 for v in values):
                         self.tagtype[tag] = TiffTags.SHORT
+                    elif all(-(2 ** 15) < v < 2 ** 15 for v in values):
+                        self.tagtype[tag] = TiffTags.SIGNED_SHORT
                     else:
-                        self.tagtype[tag] = TiffTags.LONG
+                        self.tagtype[tag] = (
+                            TiffTags.LONG
+                            if all(v >= 0 for v in values)
+                            else TiffTags.SIGNED_LONG
+                        )
                 elif all(isinstance(v, float) for v in values):
                     self.tagtype[tag] = TiffTags.DOUBLE
                 else:
@@ -666,7 +690,7 @@ class ImageFileDirectory_v2(MutableMapping):
     @_register_writer(5)
     def write_rational(self, *values):
         return b"".join(
-            self._pack("2L", *_limit_rational(frac, 2 ** 31)) for frac in values
+            self._pack("2L", *_limit_rational(frac, 2 ** 32 - 1)) for frac in values
         )
 
     @_register_loader(7, 1)
@@ -689,7 +713,8 @@ class ImageFileDirectory_v2(MutableMapping):
     @_register_writer(10)
     def write_signed_rational(self, *values):
         return b"".join(
-            self._pack("2L", *_limit_rational(frac, 2 ** 30)) for frac in values
+            self._pack("2l", *_limit_signed_rational(frac, 2 ** 31 - 1, -(2 ** 31)))
+            for frac in values
         )
 
     def _ensure_read(self, fp, size):

exif_transpose errors out on some images
<!--
Thank you for reporting an issue.

Follow these guidelines to ensure your issue is handled properly.

If you have a ...

1.  General question: consider asking the question on Stack Overflow
    with the python-imaging-library tag:

    * https://stackoverflow.com/questions/tagged/python-imaging-library

    Do not ask a question in both places.

    If you think you have found a bug or have an unexplained exception
    then file a bug report here.

2.  Bug report: include a self-contained, copy-pastable example that
    generates the issue if possible. Be concise with code posted.
    Guidelines on how to provide a good bug report:

    * https://stackoverflow.com/help/mcve

    Bug reports which follow these guidelines are easier to diagnose,
    and are often handled much more quickly.

3.  Feature request: do a quick search of existing issues
    to make sure this has not been asked before.

We know asking good questions takes effort, and we appreciate your time.
Thank you.
-->

### What did you do?
Used new `exif_transpose` method.

### What did you expect to happen?
I expected a transposed image to be returned, without error.

### What actually happened?
An error was raised.
```
/Users/okor/thumbor/lib/python2.7/site-packages/PIL/TiffImagePlugin.py:612: UserWarning: Metadata Warning, tag 282 had too many entries: 2, expected 1
  tag, len(values)))
/Users/okor/thumbor/lib/python2.7/site-packages/PIL/TiffImagePlugin.py:612: UserWarning: Metadata Warning, tag 283 had too many entries: 2, expected 1
  tag, len(values)))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/okor/thumbor/lib/python2.7/site-packages/PIL/ImageOps.py", line 549, in exif_transpose
    transposed_image.info["exif"] = exif.tobytes()
  File "/Users/okor/thumbor/lib/python2.7/site-packages/PIL/Image.py", line 3087, in tobytes
    return b"Exif\x00\x00"+head+ifd.tobytes(offset)
  File "/Users/okor/thumbor/lib/python2.7/site-packages/PIL/TiffImagePlugin.py", line 818, in tobytes
    count = len(data)
TypeError: object of type 'int' has no len()
```

Note that this error is not raised for _all_ images. Only some. I'm not sure how the EXIF data differs.

### What are your OS, Python and Pillow versions?

* OS: macOS Mojave 10.14.5
* Python: 2.7.16
* Pillow: 6.0.0

<!--
Please include **code** that reproduces the issue and whenever possible, an **image** that demonstrates the issue. Please upload images to GitHub, not to third-party file hosting sites. If necessary, add the image to a zip or tar archive.

The best reproductions are self-contained scripts with minimal dependencies. If you are using a framework such as Plone, Django, or Buildout, try to replicate the issue just using Pillow.
-->

```python
from PIL import Image, ImageOps

im = Image.open('10_years_of_Wikipedia_by_Guillaume_Paumier.jpg')
im = ImageOps.exif_transpose(im)
```
[10_years_of_Wikipedia_by_Guillaume_Paumier.jpg.zip](https://github.com/python-pillow/Pillow/files/3398388/10_years_of_Wikipedia_by_Guillaume_Paumier.jpg.zip)

---

This error was encountered when attempting to replace `piexif` exif functionality with new Pillow functionality. The PR and conversation is here https://github.com/thumbor/thumbor/pull/1209
EXIF tobytes: ushort format requires 0 <= number <= (0x7fff * 2 + 1)
1. Download this image: https://cdn.everypony.ru/storage/06/18/58/2019/08/19/3e8e6e42ca.jpg

2. Run `Image.open('3e8e6e42ca.jpg').getexif().tobytes()`

3. Not sure if this is a corrupt image or a Pillow bug, but:

```
PIL/TiffImagePlugin.py:605: UserWarning: Metadata Warning, tag 283 had too many entries: 2, expected 1
  % (tag, len(values))

  File "PIL/Image.py", line 3213, in tobytes
    return b"Exif\x00\x00" + head + ifd.tobytes(offset)
  File "PIL/TiffImagePlugin.py", line 824, in tobytes
    data = self._write_dispatch[typ](self, *values)
  File "PIL/TiffImagePlugin.py", line 659, in <lambda>
    b"".join(self._pack(fmt, value) for value in values)
  File "PIL/TiffImagePlugin.py", line 659, in <genexpr>
    b"".join(self._pack(fmt, value) for value in values)
  File "PIL/TiffImagePlugin.py", line 626, in _pack
    return struct.pack(self._endian + fmt, *values)
struct.error: ushort format requires 0 <= number <= (0x7fff * 2 + 1)
```

P. S. Why Tiff?
diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py
index bf8d6e933..f5407d49d 100644
--- a/Tests/test_file_tiff_metadata.py
+++ b/Tests/test_file_tiff_metadata.py
@@ -2,7 +2,7 @@ import io
 import struct
 
 from PIL import Image, TiffImagePlugin, TiffTags
-from PIL.TiffImagePlugin import IFDRational, _limit_rational
+from PIL.TiffImagePlugin import IFDRational
 
 from .helper import PillowTestCase, hopper
 
@@ -139,14 +139,6 @@ class TestFileTiffMetadata(PillowTestCase):
         with Image.open(f) as loaded:
             reloaded = loaded.tag_v2.named()
 
-        for k, v in original.items():
-            if isinstance(v, IFDRational):
-                original[k] = IFDRational(*_limit_rational(v, 2 ** 31))
-            elif isinstance(v, tuple) and isinstance(v[0], IFDRational):
-                original[k] = tuple(
-                    IFDRational(*_limit_rational(elt, 2 ** 31)) for elt in v
-                )
-
         ignored = ["StripByteCounts", "RowsPerStrip", "PageNumber", "StripOffsets"]
 
         for tag, value in reloaded.items():
@@ -225,6 +217,90 @@ class TestFileTiffMetadata(PillowTestCase):
             self.assertEqual(0, reloaded.tag_v2[41988].numerator)
             self.assertEqual(0, reloaded.tag_v2[41988].denominator)
 
+    def test_ifd_unsigned_rational(self):
+        im = hopper()
+        info = TiffImagePlugin.ImageFileDirectory_v2()
+
+        max_long = 2 ** 32 - 1
+
+        # 4 bytes unsigned long
+        numerator = max_long
+
+        info[41493] = TiffImagePlugin.IFDRational(numerator, 1)
+
+        out = self.tempfile("temp.tiff")
+        im.save(out, tiffinfo=info, compression="raw")
+
+        reloaded = Image.open(out)
+        self.assertEqual(max_long, reloaded.tag_v2[41493].numerator)
+        self.assertEqual(1, reloaded.tag_v2[41493].denominator)
+
+        # out of bounds of 4 byte unsigned long
+        numerator = max_long + 1
+
+        info[41493] = TiffImagePlugin.IFDRational(numerator, 1)
+
+        out = self.tempfile("temp.tiff")
+        im.save(out, tiffinfo=info, compression="raw")
+
+        reloaded = Image.open(out)
+        self.assertEqual(max_long, reloaded.tag_v2[41493].numerator)
+        self.assertEqual(1, reloaded.tag_v2[41493].denominator)
+
+    def test_ifd_signed_rational(self):
+        im = hopper()
+        info = TiffImagePlugin.ImageFileDirectory_v2()
+
+        # pair of 4 byte signed longs
+        numerator = 2 ** 31 - 1
+        denominator = -(2 ** 31)
+
+        info[37380] = TiffImagePlugin.IFDRational(numerator, denominator)
+
+        out = self.tempfile("temp.tiff")
+        im.save(out, tiffinfo=info, compression="raw")
+
+        reloaded = Image.open(out)
+        self.assertEqual(numerator, reloaded.tag_v2[37380].numerator)
+        self.assertEqual(denominator, reloaded.tag_v2[37380].denominator)
+
+        numerator = -(2 ** 31)
+        denominator = 2 ** 31 - 1
+
+        info[37380] = TiffImagePlugin.IFDRational(numerator, denominator)
+
+        out = self.tempfile("temp.tiff")
+        im.save(out, tiffinfo=info, compression="raw")
+
+        reloaded = Image.open(out)
+        self.assertEqual(numerator, reloaded.tag_v2[37380].numerator)
+        self.assertEqual(denominator, reloaded.tag_v2[37380].denominator)
+
+        # out of bounds of 4 byte signed long
+        numerator = -(2 ** 31) - 1
+        denominator = 1
+
+        info[37380] = TiffImagePlugin.IFDRational(numerator, denominator)
+
+        out = self.tempfile("temp.tiff")
+        im.save(out, tiffinfo=info, compression="raw")
+
+        reloaded = Image.open(out)
+        self.assertEqual(2 ** 31 - 1, reloaded.tag_v2[37380].numerator)
+        self.assertEqual(-1, reloaded.tag_v2[37380].denominator)
+
+    def test_ifd_signed_long(self):
+        im = hopper()
+        info = TiffImagePlugin.ImageFileDirectory_v2()
+
+        info[37000] = -60000
+
+        out = self.tempfile("temp.tiff")
+        im.save(out, tiffinfo=info, compression="raw")
+
+        reloaded = Image.open(out)
+        self.assertEqual(reloaded.tag_v2[37000], -60000)
+
     def test_empty_values(self):
         data = io.BytesIO(
             b"II*\x00\x08\x00\x00\x00\x03\x00\x1a\x01\x05\x00\x00\x00\x00\x00"

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@f72e866b5e9cd3c54424bd14786532135b4441b7#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_tiff_metadata.py::TestFileTiffMetadata::test_ifd_signed_long Tests/test_file_tiff_metadata.py::TestFileTiffMetadata::test_ifd_signed_rational Tests/test_file_tiff_metadata.py::TestFileTiffMetadata::test_ifd_unsigned_rational Tests/test_file_tiff_metadata.py::TestFileTiffMetadata::test_write_metadata
Tests/test_file_tiff_metadata.py::TestFileTiffMetadata::test_PhotoshopInfo Tests/test_file_tiff_metadata.py::TestFileTiffMetadata::test_empty_metadata Tests/test_file_tiff_metadata.py::TestFileTiffMetadata::test_empty_values Tests/test_file_tiff_metadata.py::TestFileTiffMetadata::test_exif_div_zero Tests/test_file_tiff_metadata.py::TestFileTiffMetadata::test_iccprofile Tests/test_file_tiff_metadata.py::TestFileTiffMetadata::test_iccprofile_binary Tests/test_file_tiff_metadata.py::TestFileTiffMetadata::test_iccprofile_binary_save_png Tests/test_file_tiff_metadata.py::TestFileTiffMetadata::test_iccprofile_save_png Tests/test_file_tiff_metadata.py::TestFileTiffMetadata::test_no_duplicate_50741_tag Tests/test_file_tiff_metadata.py::TestFileTiffMetadata::test_read_metadata Tests/test_file_tiff_metadata.py::TestFileTiffMetadata::test_rt_metadata Tests/test_file_tiff_metadata.py::TestFileTiffMetadata::test_too_many_entries
src/PIL/TiffImagePlugin.py
src/PIL/TiffImagePlugin.py