Repository: python-pillow/Pillow

Issue:
8.3.0: `convert` on a P image with transparency now returns a RGB image (used to be RGBA) 
<!--
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?

I converted an image in P mode with transparency on the new 8.3.0 release.


### What did you expect to happen?

I expected the output to be RGBA.

### What actually happened?

it is now in RGB.

### What are your OS, Python and Pillow versions?

* OS: Ubuntu 18.04
* Python: 3.7
* Pillow: 8.3.0 or 8.3.1


Please find the code and image to reproduce the issue on https://gist.github.com/benjaminrigaud/d98a4b54fb409bc8f745d169df0196fc
<!--
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
def test_convert_p():
    with open("indexed_with_transparency.png", "rb") as f:
        data = BytesIO(f.read())

    img = Image.open(data)

    assert img.mode == "P"
    assert img.palette.mode == "RGB"
    assert img.info.get("transparency") == 0

    img2 = img.convert()

    assert img2.mode == "RGBA"
```



Discussion:
This would be due to https://github.com/python-pillow/Pillow/pull/5552/commits/804183c2489242251fdd9858b6e781ceefb08c8b. It changed palettes for P images with `info["transparency"]` to RGB instead of RGBA. Separate to that, converting from a P image with no mode argument uses the palette mode if there is one.

I've created PR #5594 to fix this.
I am facing a similar issue when converting a P image into LA: values of the transparency channel are all 255 with `pillow>=8.3`:

```py
from PIL import Image
import numpy as np

# same image that OP mentioned https://gist.github.com/benjaminrigaud/d98a4b54fb409bc8f745d169df0196fc
path = the_path_here

with Image.open(path) as img:
    img = img.convert("LA")
np.unique(np.asarray(img)[:, :, -1])
```

This will print `array([255], dtype=uint8)` on 8.3.1 and `array([  0, 255], dtype=uint8)` on 8.2

Using another P image like https://github.com/pytorch/vision/blob/master/test/assets/fakedata/logos/palette_pytorch.png, the results are even more drastic:

`array([255], dtype=uint8)` vs `array([  0,  16,  64,  80,  96, 128, 160, 208, 224, 240, 255], dtype=uint8)`

Resolving patch:
diff --git a/src/PIL/Image.py b/src/PIL/Image.py
--- a/src/PIL/Image.py
+++ b/src/PIL/Image.py
@@ -1005,7 +1005,7 @@ def convert_transparency(m, v):
                             trns_im = trns_im.convert("RGB")
                         trns = trns_im.getpixel((0, 0))
 
-            elif self.mode == "P" and mode == "RGBA":
+            elif self.mode == "P" and mode in ("LA", "PA", "RGBA"):
                 t = self.info["transparency"]
                 delete_trns = True
 


Test patch:
diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py
--- a/Tests/test_image_convert.py
+++ b/Tests/test_image_convert.py
@@ -100,18 +100,22 @@ def test_trns_p(tmp_path):
 # ref https://github.com/python-pillow/Pillow/issues/664
 
 
-def test_trns_p_rgba():
+@pytest.mark.parametrize("mode", ("LA", "PA", "RGBA"))
+def test_trns_p_transparency(mode):
     # Arrange
     im = hopper("P")
     im.info["transparency"] = 128
 
     # Act
-    im_rgba = im.convert("RGBA")
+    converted_im = im.convert(mode)
 
     # Assert
-    assert "transparency" not in im_rgba.info
-    # https://github.com/python-pillow/Pillow/issues/2702
-    assert im_rgba.palette is None
+    assert "transparency" not in converted_im.info
+    if mode == "PA":
+        assert converted_im.palette is not None
+    else:
+        # https://github.com/python-pillow/Pillow/issues/2702
+        assert converted_im.palette is None
 
 
 def test_trns_l(tmp_path):