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

Add Support for webp format #6035

Merged
merged 9 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions examples/reference/panes/Image.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"outputs": [],
"source": [
"jpg_pane = pn.pane.Image('https://assets.holoviz.org/panel/samples/jpeg_sample.jpeg')\n",
"png_pane = pn.pane.Image('https://assets.holoviz.org/panel/samples/png_sample.png')"
"png_pane = pn.pane.Image('https://assets.holoviz.org/panel/samples/png_sample.png')\n",
"webp_pane = pn.pane.Image('https://mistral.ai/images/logo_hubc88c4ece131b91c7cb753f40e9e1cc5_2589_256x0_resize_q97_h2_lanczos_3.webp')"
ahuang11 marked this conversation as resolved.
Show resolved Hide resolved
]
},
{
Expand All @@ -58,7 +59,7 @@
},
"outputs": [],
"source": [
"pn.Column(jpg_pane, png_pane)"
"pn.Column(jpg_pane, png_pane, webp_pane)"
]
},
{
Expand Down
34 changes: 34 additions & 0 deletions panel/pane/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,3 +496,37 @@ def _transform_object(self, obj: Any) -> Dict[str, Any]:
page = f'#page={self.start_page}' if getattr(self, 'start_page', None) else ''
html = f'<embed src="{obj}{page}" width={w!r} height={h!r} type="application/pdf">'
return dict(text=escape(html))

class WEBP(ImageBase):
ahuang11 marked this conversation as resolved.
Show resolved Hide resolved
"""
The `WEBP` pane embeds a .webp image file in a panel if
provided a local path, or will link to a remote image if provided
a URL.

Reference: https://developers.google.com/speed/webp/docs/riff_container

:Example:

>>> WEBP(
... 'https://www.gstatic.com/webp/gallery/4.sm.webp',
... alt_text='A nice tree',
... link_url='https://en.wikipedia.org/wiki/WebP',
... width=500,
... caption='A nice tree'
... )
"""

filetype: ClassVar[str] = 'webp'

_extensions: ClassVar[Tuple[str, ...]] = ('webp',)

@classmethod
def _imgshape(cls, data):
import struct
b = BytesIO(data)
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can use a context manager here:

with BytesIO(data) as b:
    ...

b.read(12) # Skip RIFF header
if b.read(4) != b'VP8 ': # Check if VP8 chunk is present
raise ValueError("Invalid WebP file")
b.read(3) # Skip VP8 header
w, h = struct.unpack("<HH", b.read(4)) # Width and height are little-endian
return int(w), int(h)
Loading