Repository: python-pillow/Pillow (Python)

Issue:
Exceptions when trying to convert BMP to TIFF
https://stuff.realpath.org/test.bmp is a very simple BMP image (unfortunately GitHub doesn't allow to attach it here).

### First exception

When I try to convert it to a TIFF:

```python
with Image.open("test.bmp") as im:
    im.save("test.tiff", format="TIFF")
```

I get the following exception:

```
Traceback (most recent call last):
  File "convert.py", line 4, in <module>
    im.save("test.tiff", format="TIFF")
  File "venv\lib\site-packages\PIL\Image.py", line 2300, in save
    save_handler(self, fp, filename)
  File "venv\lib\site-packages\PIL\TiffImagePlugin.py", line 1810, in _save
    e = Image._getencoder(im.mode, "libtiff", a, encoderconfig)
  File "venv\lib\site-packages\PIL\Image.py", line 458, in _getencoder
    return encoder(mode, *args + extra)
TypeError: argument 3 must be str, not int
```

However when disabling compression it works:

```python
with Image.open("test.bmp") as im:
    im.save("test.tiff", format="TIFF", compression=None)
```

This seems to be an older issue because I found a StackOverflow question from 2019 mentioning this exact problem: https://stackoverflow.com/q/54181211

### Second exception
There also seems to be a **regression** in 9.1.0 compared to 9.0.1. In 9.0.1 using `tiff_lzw` compression worked without problems:

```python
with Image.open("test.bmp") as im:
    im.save("test.tiff", format="TIFF", compression="tiff_lzw")
```

However in 9.1.0 I get the following exception:

```
Traceback (most recent call last):
  File "convert.py", line 4, in <module>
    im.save("test.tiff", format="TIFF", compression="tiff_lzw")
  File "venv\lib\site-packages\PIL\Image.py", line 2300, in save
    save_handler(self, fp, filename)
  File "venv\lib\site-packages\PIL\TiffImagePlugin.py", line 1810, in _save
    e = Image._getencoder(im.mode, "libtiff", a, encoderconfig)
  File "venv\lib\site-packages\PIL\Image.py", line 458, in _getencoder
    return encoder(mode, *args + extra)
ValueError: Requiring 768 items for Colormap
```

### OS, Python and Pillow versions:

* OS: Windows 10
* Python: 3.9 x64
* Pillow: 9.1.0





Discussion:
I've created #6231 to fix the first part.
I've created PR #6232 to fix the second part.



Patch:
diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py
index 000429991d6..6966467d66e 100644
--- a/src/PIL/TiffImagePlugin.py
+++ b/src/PIL/TiffImagePlugin.py
@@ -1676,7 +1676,12 @@ def _save(im, fp, filename):
 
     if im.mode in ["P", "PA"]:
         lut = im.im.getpalette("RGB", "RGB;L")
-        ifd[COLORMAP] = tuple(v * 256 for v in lut)
+        colormap = []
+        colors = len(lut) // 3
+        for i in range(3):
+            colormap += [v * 256 for v in lut[colors * i : colors * (i + 1)]]
+            colormap += [0] * (256 - colors)
+        ifd[COLORMAP] = colormap
     # data orientation
     stride = len(bits) * ((im.size[0] * bits[0] + 7) // 8)
     # aim for given strip size (64 KB by default) when using libtiff writer


Test patch:
diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py
index d83c584b5da..a43548ae0f3 100644
--- a/Tests/test_file_libtiff.py
+++ b/Tests/test_file_libtiff.py
@@ -497,8 +497,8 @@ def test_cmyk_save(self, tmp_path):
         im.save(out, compression="tiff_adobe_deflate")
         assert_image_equal_tofile(im, out)
 
-    def test_palette_save(self, tmp_path):
-        im = hopper("P")
+    @pytest.mark.parametrize("im", (hopper("P"), Image.new("P", (1, 1), "#000")))
+    def test_palette_save(self, im, tmp_path):
         out = str(tmp_path / "temp.tif")
 
         TiffImagePlugin.WRITE_LIBTIFF = True