radarhere: Rebased.
diff --git a/src/PIL/Image.py b/src/PIL/Image.py
index f13a98276..e31d9002d 100644
--- a/src/PIL/Image.py
+++ b/src/PIL/Image.py
@@ -900,12 +900,28 @@ class Image(object):
         if not mode or (mode == self.mode and not matrix):
             return self.copy()
 
+        has_transparency = self.info.get('transparency') is not None
         if matrix:
             # matrix conversion
             if mode not in ("L", "RGB"):
                 raise ValueError("illegal conversion")
             im = self.im.convert_matrix(mode, matrix)
-            return self._new(im)
+            new = self._new(im)
+            if has_transparency and self.im.bands == 3:
+                transparency = new.info['transparency']
+
+                def convert_transparency(m, v):
+                    v = m[0]*v[0] + m[1]*v[1] + m[2]*v[2] + m[3]*0.5
+                    return max(0, min(255, int(v)))
+                if mode == "L":
+                    transparency = convert_transparency(matrix, transparency)
+                elif len(mode) == 3:
+                    transparency = tuple([
+                        convert_transparency(matrix[i*4:i*4+4], transparency)
+                        for i in range(0, len(transparency))
+                    ])
+                new.info['transparency'] = transparency
+            return new
 
         if mode == "P" and self.mode == "RGBA":
             return self.quantize(colors)
@@ -913,8 +929,7 @@ class Image(object):
         trns = None
         delete_trns = False
         # transparency handling
-        if "transparency" in self.info and \
-                self.info['transparency'] is not None:
+        if has_transparency:
             if self.mode in ('L', 'RGB') and mode == 'RGBA':
                 # Use transparent conversion to promote from transparent
                 # color to an alpha channel.

Save fails after converting 16-bit RGB to grayscale
### What did you do?
Downloaded test-PNG from "pngsuite" (see below), converted to grayscale using the matrix-option, saved the result.

### What did you expect to happen?
that the script would succeed like with almost all other PNGs from "pngsuite"

### What actually happened?
Save fails with

```
Traceback (most recent call last):
  File "C:/base2_nl/py-grayscale/minimalExampleBug.py", line 5, in <module>
    im.save('tbbn2c16-out.png')
  File "C:\Python36\lib\site-packages\PIL\Image.py", line 1935, in save
    save_handler(self, fp, filename)
  File "C:\Python36\lib\site-packages\PIL\PngImagePlugin.py", line 790, in _save
    transparency = max(0, min(65535, transparency))
TypeError: '<' not supported between instances of 'tuple' and 'int'
```

### What versions of Pillow and Python are you using?
Pillow 5.1.0
Python 3.6.5

### Minimal example to reproduce the error
```python
from PIL import Image

im = Image.open('tbbn2c16.png') # from here: http://www.schaik.com/pngsuite/tbbn2c16.png
im = im.convert("L",  (0.22, 0.72, 0.06, 0))
im.save('tbbn2c16-out.png') # fails
```

BTW in this case transparency is a tuple with the following value before the exception:
```python
transparency  = (65535,65535,65535)
```

Probably line 790 would have produced the expected result in Python 2.7, but is invalid in Python 3.x when mixing integers and tuple parameters.


diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py
index 1e208d80c..84c1cc82e 100644
--- a/Tests/test_image_convert.py
+++ b/Tests/test_image_convert.py
@@ -187,6 +187,7 @@ class TestImageConvert(PillowTestCase):
         def matrix_convert(mode):
             # Arrange
             im = hopper('RGB')
+            im.info['transparency'] = (255, 0, 0)
             matrix = (
                 0.412453, 0.357580, 0.180423, 0,
                 0.212671, 0.715160, 0.072169, 0,
@@ -203,9 +204,11 @@ class TestImageConvert(PillowTestCase):
             target = Image.open('Tests/images/hopper-XYZ.png')
             if converted_im.mode == 'RGB':
                 self.assert_image_similar(converted_im, target, 3)
+                self.assertEqual(converted_im.info['transparency'], (105, 54, 4))
             else:
                 self.assert_image_similar(converted_im,
                                           target.getchannel(0), 1)
+                self.assertEqual(converted_im.info['transparency'], 105)
 
         matrix_convert('RGB')
         matrix_convert('L')

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
blessed==1.20.0
build==1.2.2.post1
certifi==2025.1.31
charset-normalizer==3.4.1
check-manifest==0.50
cov-core==1.15.0
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
olefile==0.47
packaging==24.2
-e git+https://github.com/python-pillow/Pillow.git@1e56ed8c000bfffb198de097fb76c7252904059b#egg=Pillow
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
pytz==2025.2
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
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
      - blessed==1.20.0
      - build==1.2.2.post1
      - certifi==2025.1.31
      - charset-normalizer==3.4.1
      - check-manifest==0.50
      - cov-core==1.15.0
      - 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
      - olefile==0.47
      - packaging==24.2
      - 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
      - pytz==2025.2
      - 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
      - urllib3==2.3.0
      - wcwidth==0.2.13
      - zipp==3.21.0
prefix: /opt/conda/envs/Pillow

Tests/test_image_convert.py::TestImageConvert::test_matrix_xyz Tests/test_image_convert.py::TestImageConvert::test_p_la Tests/test_image_convert.py::TestImageConvert::test_rgba_p Tests/test_image_convert.py::TestImageConvert::test_sanity Tests/test_image_convert.py::TestImageConvert::test_trns_RGB Tests/test_image_convert.py::TestImageConvert::test_trns_l Tests/test_image_convert.py::TestImageConvert::test_trns_p Tests/test_image_convert.py::TestImageConvert::test_trns_p_rgba
Tests/test_image_convert.py::TestImageConvert::test_16bit Tests/test_image_convert.py::TestImageConvert::test_16bit_workaround Tests/test_image_convert.py::TestImageConvert::test_8bit Tests/test_image_convert.py::TestImageConvert::test_default Tests/test_image_convert.py::TestImageConvert::test_gif_with_rgba_palette_to_p Tests/test_image_convert.py::TestImageConvert::test_matrix_identity Tests/test_image_convert.py::TestImageConvert::test_matrix_illegal_conversion Tests/test_image_convert.py::TestImageConvert::test_matrix_wrong_mode
src/PIL/Image.py
src/PIL/Image.py