Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added _repr_jpeg_() for IPython display_jpeg #7135

Merged
merged 11 commits into from May 16, 2023
Merged

Added _repr_jpeg_() for IPython display_jpeg #7135

merged 11 commits into from May 16, 2023

Conversation

n3011
Copy link
Contributor

@n3011 n3011 commented May 4, 2023

Changes proposed in this pull request:

  • Add _repr_jpg_ and _repr_jpeg_ method for ipython display (similar to currently existing _repr_png_)

Signed-off-by: Ishant Mrinal Haloi <mrinal.haloi11@gmail.com>
@radarhere
Copy link
Member

If I understand correctly, this is not just for passing an image object directly to IPython, like in #1090 (comment)

from PIL import Image
im = Image.new("RGB", (512, 512), "blue")
im

and not just for IPython.display.display, but specifically for IPython.display.display_jpeg, yes?

src/PIL/Image.py Outdated Show resolved Hide resolved
src/PIL/Image.py Outdated Show resolved Hide resolved
@n3011
Copy link
Contributor Author

n3011 commented May 5, 2023

If I understand correctly, this is not just for passing an image object directly to IPython, like in #1090 (comment)

from PIL import Image
im = Image.new("RGB", (512, 512), "blue")
im

and not just for IPython.display.display, but specifically for IPython.display.display_jpeg, yes?

Yes this is display_jpeg.

@radarhere radarhere changed the title Add _repr_jpg_ for ipython display Added _repr_jpg_ for IPython display May 5, 2023
src/PIL/Image.py Outdated
@@ -633,19 +633,36 @@ def _repr_pretty_(self, p, cycle):
)
)

def _repr_png_(self):
def _repr_image(self, format):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change format to something that doesn't shadow a builtin?

n3011 and others added 4 commits May 6, 2023 10:31
Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com>
Only assert image is similar
Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com>
@n3011 n3011 changed the title Added _repr_jpg_ for IPython display Added _repr_jpeg_ for IPython display May 12, 2023
src/PIL/Image.py Outdated Show resolved Hide resolved
src/PIL/Image.py Outdated Show resolved Hide resolved
@radarhere radarhere added the JPEG label May 13, 2023
n3011 and others added 2 commits May 13, 2023 07:32
Co-authored-by: Ondrej Baranovič <3819630+nulano@users.noreply.github.com>
Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com>
Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please could you also mention this in the release notes?

https://github.com/python-pillow/Pillow/blob/main/docs/releasenotes/10.0.0.rst

src/PIL/Image.py Show resolved Hide resolved
src/PIL/Image.py Outdated Show resolved Hide resolved
src/PIL/Image.py Outdated Show resolved Hide resolved
n3011 and others added 3 commits May 14, 2023 11:11
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
@radarhere
Copy link
Member

Release notes have now been added - https://pillow--7135.org.readthedocs.build/en/7135/releasenotes/10.0.0.html#support-display-jpeg-in-ipython

@hugovk hugovk merged commit a58034b into python-pillow:main May 16, 2023
58 of 59 checks passed
@n3011 n3011 deleted the jpg_repr branch May 16, 2023 09:20
@radarhere radarhere changed the title Added _repr_jpeg_ for IPython display Added _repr_jpeg_ for IPython display_jpeg May 16, 2023
@radarhere radarhere changed the title Added _repr_jpeg_ for IPython display_jpeg Added _repr_jpeg_() for IPython display_jpeg May 17, 2023
@smason
Copy link
Contributor

smason commented Jul 4, 2023

I'm wondering what the purpose of this change is. I'm interested to know if it's mainly to support calling display_jpeg on an image?

If so, this seems to be having unintended consequences as I'm now getting both a PNG and JPEG encoded image saved in each cell. For example, a cell I just created includes:

     "data": {
      "image/jpeg": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsI>
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAC7kAAAu5CAAAAABw4BI+AAEAAElEQVR4AQBSgK1/AdfANnmuHKdSoc6W0wL98OSB+b>
      "text/plain": [
       "<PIL.Image.Image image mode=L size=3001x3001>"
      ]
     },

which might well double resulting .ipynb file sizes, depending on how either encoder deals with the image.

@radarhere
Copy link
Member

I'm interested to know if it's mainly to support calling display_jpeg on an image?

Yes. That is what the original poster was after, and that is what we have announced.

Discussion of double images can continue in #7259

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants