Repository: python-pillow/Pillow

Issue:
iPython cannot display RGBA image after upgrade to 10.0.0
<!--
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.
-->
Previously we only have `_repr_png_`, so we can always display RGBA in ipython.

now we also have `_repr_jpg_` for some reason, the default way to display it is this jpg function, thus when we have RGBA image we cannot display it.

### What did you do?
in ipython qtconsole, I try to display RGBA image

```python
from PIL import Image
im = Image.new("RGBA", (126, 187))
display(im)
```
### What did you expect to happen?

show the image without error.

The default repr should be png or at least for RGBA image it should be png.

### What actually happened?

display default to choose the jpeg formatter.
```
SError: cannot write mode RGBA as JPEG
```
### What are your OS, Python and Pillow versions?

* OS:  ubuntu 22.04
* Python:  3.10.6
* Pillow: 10.0.0

<!--
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
from PIL import Image
im = Image.new("RGBA", (126, 187))
display(im)

```



Discussion:

Resolving patch:
diff --git a/src/PIL/Image.py b/src/PIL/Image.py
--- a/src/PIL/Image.py
+++ b/src/PIL/Image.py
@@ -641,9 +641,8 @@ def _repr_image(self, image_format, **kwargs):
         b = io.BytesIO()
         try:
             self.save(b, image_format, **kwargs)
-        except Exception as e:
-            msg = f"Could not save to {image_format} for display"
-            raise ValueError(msg) from e
+        except Exception:
+            return None
         return b.getvalue()
 
     def _repr_png_(self):


Test patch:
diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py
--- a/Tests/test_file_jpeg.py
+++ b/Tests/test_file_jpeg.py
@@ -929,11 +929,10 @@ def test_repr_jpeg(self):
             assert repr_jpeg.format == "JPEG"
             assert_image_similar(im, repr_jpeg, 17)
 
-    def test_repr_jpeg_error(self):
+    def test_repr_jpeg_error_returns_none(self):
         im = hopper("F")
 
-        with pytest.raises(ValueError):
-            im._repr_jpeg_()
+        assert im._repr_jpeg_() is None
 
 
 @pytest.mark.skipif(not is_win32(), reason="Windows only")
diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py
--- a/Tests/test_file_png.py
+++ b/Tests/test_file_png.py
@@ -532,11 +532,10 @@ def test_repr_png(self):
             assert repr_png.format == "PNG"
             assert_image_equal(im, repr_png)
 
-    def test_repr_png_error(self):
+    def test_repr_png_error_returns_none(self):
         im = hopper("F")
 
-        with pytest.raises(ValueError):
-            im._repr_png_()
+        assert im._repr_png_() is None
 
     def test_chunk_order(self, tmp_path):
         with Image.open("Tests/images/icc_profile.png") as im: