Repository: python-pillow/Pillow

Issue:
TypeError on getexif().tobytes()
### What did you do?

Saved a tiff with same exif.

### What did you expect to happen?

saved file

### What actually happened?

```python
Traceback (most recent call last):
  File "/Users/deepio/Desktop/test/ape.py", line 4, in <module>
    im.save("testing.tiff", format=im.format, exif=im.getexif().tobytes())
  File "/Users/deepio/.pyenv/versions/3.9.13/lib/python3.9/site-packages/PIL/Image.py", line 2353, in save
    save_handler(self, fp, filename)
  File "/Users/deepio/.pyenv/versions/3.9.13/lib/python3.9/site-packages/PIL/JpegImagePlugin.py", line 747, in _save
    exif = exif.tobytes()
  File "/Users/deepio/.pyenv/versions/3.9.13/lib/python3.9/site-packages/PIL/Image.py", line 3628, in tobytes
    return b"Exif\x00\x00" + head + ifd.tobytes(offset)
  File "/Users/deepio/.pyenv/versions/3.9.13/lib/python3.9/site-packages/PIL/TiffImagePlugin.py", line 878, in tobytes
    data = ifd.tobytes(offset)
  File "/Users/deepio/.pyenv/versions/3.9.13/lib/python3.9/site-packages/PIL/TiffImagePlugin.py", line 887, in tobytes
    "<table: %d bytes>" % len(data) if len(data) >= 16 else str(values)
TypeError: object of type 'int' has no len()
```

So I traced it:

```python
> /Users/deepio/Desktop/ape.py(11)go()
-> print(im.getexif().tobytes())
(Pdb) b /Users/deepio/.pyenv/versions/3.9.1/lib/python3.9/site-packages/PIL/TiffImagePlugin.py:864
Breakpoint 1 at /Users/deepio/.pyenv/versions/3.9.1/lib/python3.9/site-packages/PIL/TiffImagePlugin.py:864
(Pdb) condition 1 tag==5
New condition set for breakpoint 1.
(Pdb) commands 1
(com) print(f"tag: {tag}\nvalue: {value}")
(com) end
(Pdb) c
tag: 5
value: 0
> /Users/deepio/.pyenv/versions/3.9.1/lib/python3.9/site-packages/PIL/TiffImagePlugin.py(864)tobytes()
-> if tag == STRIPOFFSETS:

(Pdb) is_ifd
False
(Pdb) values = value if isinstance(value, tuple) else (value,)
(Pdb) values
(0,)
(Pdb) self._write_dispatch[typ](self, *values)
0

<same traceback as above>
```

So a possible fix could be to do the same isinstance on data as it is done on values above it.

### What are your OS, Python and Pillow versions?

* OS: recreated on MacOS 12.5.1 / Ubuntu 18.04
* Python: (3.9.1/3.9.13)/3.9.9
* Pillow: 9.3.0

```python
from PIL import Image

im = Image.open("38bfc38a_a51a_c735def5ae62.tiff")
# saving is not required to trigger the same failure.
im.getexif().tobytes()
```


Discussion:
Possibly a type of regression with https://github.com/python-pillow/Pillow/issues/1524 but it's not with saving
Would you be able to upload a copy of the image?
Here we go:
![t56023](https://user-images.githubusercontent.com/28245482/201712811-6a466f72-f221-4d50-b71e-5f44a06623ea.jpg)


Resolving patch:
diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py
--- a/src/PIL/TiffImagePlugin.py
+++ b/src/PIL/TiffImagePlugin.py
@@ -719,6 +719,8 @@ def load_byte(self, data, legacy_api=True):
 
     @_register_writer(1)  # Basic type, except for the legacy API.
     def write_byte(self, data):
+        if isinstance(data, int):
+            data = bytes((data,))
         return data
 
     @_register_loader(2, 1)


Test patch:
diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py
--- a/Tests/test_file_tiff_metadata.py
+++ b/Tests/test_file_tiff_metadata.py
@@ -201,6 +201,22 @@ def test_writing_bytes_to_ascii(tmp_path):
         assert reloaded.tag_v2[271] == "test"
 
 
+def test_writing_int_to_bytes(tmp_path):
+    im = hopper()
+    info = TiffImagePlugin.ImageFileDirectory_v2()
+
+    tag = TiffTags.TAGS_V2[700]
+    assert tag.type == TiffTags.BYTE
+
+    info[700] = 1
+
+    out = str(tmp_path / "temp.tiff")
+    im.save(out, tiffinfo=info)
+
+    with Image.open(out) as reloaded:
+        assert reloaded.tag_v2[700] == b"\x01"
+
+
 def test_undefined_zero(tmp_path):
     # Check that the tag has not been changed since this test was created
     tag = TiffTags.TAGS_V2[45059]