Repository: python-pillow/Pillow

Issue:
Put binary data in a tiff tag
I want to put binary data in a tiff tag, not just 1 byte, but lots of.

The following code works for 1 byte.
```python
from PIL import Image, TiffImagePlugin

path = "test.tiff"
path_out = "test2.tiff"
im = Image.open(path)

info = TiffImagePlugin.ImageFileDirectory()

info[5000] = b'\x00'
info.tagtype[5000] = 1

im.save(path_out, tiffinfo=info)

im2 = Image.open(path_out)
print(im2.tag_v2[5000])
```

But when I try to put more like this :
```python
info[5000] = b'\x00\x01\x01'
```
Its only take the first one : \x00

And with
```python
info[5000] = (b'\x00', b'\x01')
```
 
got this error :
```
TypeError: write_byte() takes 2 positional arguments but 3 were given
```

Note that I want to put like 3000+ bytes in a tag. is it possible ?



Discussion:
I find that if you just don't specify the tagtype, it works.

```python
from PIL import Image, TiffImagePlugin

path = "Tests/images/hopper.tif"
path_out = "test2.tiff"
im = Image.open(path)

info = TiffImagePlugin.ImageFileDirectory()

info[5000] = b'\x00\x01\x01'
#info.tagtype[5000] = 1

im.save(path_out, tiffinfo=info)

im2 = Image.open(path_out)
print(im2.tag_v2[5000])
```
Well, that was so dumb. Thank you. The type of this is 'undefined', don't know if it could be a problem in the future ...
I've created PR #3672 to fix the problem from your original code.

Resolving patch:
diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py
--- a/src/PIL/TiffImagePlugin.py
+++ b/src/PIL/TiffImagePlugin.py
@@ -819,7 +819,7 @@ def save(self, fp):
                     print("- value:", values)
 
             # count is sum of lengths for string and arbitrary data
-            if typ in [TiffTags.ASCII, TiffTags.UNDEFINED]:
+            if typ in [TiffTags.BYTE, TiffTags.ASCII, TiffTags.UNDEFINED]:
                 count = len(data)
             else:
                 count = len(values)


Test patch:
diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py
--- a/Tests/test_file_libtiff.py
+++ b/Tests/test_file_libtiff.py
@@ -234,11 +234,11 @@ def test_additional_metadata(self):
 
     def test_custom_metadata(self):
         custom = {
-            37000: 4,
-            37001: 4.2,
-            37002: 'custom tag value',
-            37003: u'custom tag value',
-            37004: b'custom tag value'
+            37000: [4, TiffTags.SHORT],
+            37001: [4.2, TiffTags.RATIONAL],
+            37002: ['custom tag value', TiffTags.ASCII],
+            37003: [u'custom tag value', TiffTags.ASCII],
+            37004: [b'custom tag value', TiffTags.BYTE]
         }
 
         libtiff_version = TiffImagePlugin._libtiff_version()
@@ -251,17 +251,33 @@ def test_custom_metadata(self):
         for libtiff in libtiffs:
             TiffImagePlugin.WRITE_LIBTIFF = libtiff
 
-            im = hopper()
+            def check_tags(tiffinfo):
+                im = hopper()
 
-            out = self.tempfile("temp.tif")
-            im.save(out, tiffinfo=custom)
-            TiffImagePlugin.WRITE_LIBTIFF = False
+                out = self.tempfile("temp.tif")
+                im.save(out, tiffinfo=tiffinfo)
 
-            reloaded = Image.open(out)
-            for tag, value in custom.items():
-                if libtiff and isinstance(value, bytes):
-                    value = value.decode()
-                self.assertEqual(reloaded.tag_v2[tag], value)
+                reloaded = Image.open(out)
+                for tag, value in tiffinfo.items():
+                    reloaded_value = reloaded.tag_v2[tag]
+                    if isinstance(reloaded_value, TiffImagePlugin.IFDRational):
+                        reloaded_value = float(reloaded_value)
+
+                    if libtiff and isinstance(value, bytes):
+                        value = value.decode()
+
+                    self.assertEqual(reloaded_value, value)
+
+            # Test with types
+            ifd = TiffImagePlugin.ImageFileDirectory_v2()
+            for tag, tagdata in custom.items():
+                ifd[tag] = tagdata[0]
+                ifd.tagtype[tag] = tagdata[1]
+            check_tags(ifd)
+
+            # Test without types
+            check_tags({tag: tagdata[0] for tag, tagdata in custom.items()})
+        TiffImagePlugin.WRITE_LIBTIFF = False
 
     def test_int_dpi(self):
         # issue #1765