-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f9ec20
commit 68dd7f9
Showing
17 changed files
with
257 additions
and
405 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from io import BytesIO | ||
|
||
from PIL import Image | ||
|
||
from viam.media.video import CameraMimeType, ViamImage | ||
|
||
from ..viam_rgba_plugin import RGBA_FORMAT_LABEL | ||
|
||
# Formats that are supported by PIL | ||
LIBRARY_SUPPORTED_FORMATS = ["JPEG", "PNG", RGBA_FORMAT_LABEL] | ||
|
||
|
||
def viam_to_pil_image(image: ViamImage) -> Image.Image: | ||
""" | ||
Convert a ViamImage to a PIL.Image. | ||
Args: | ||
image (ViamImage): The image to convert. | ||
Returns: | ||
Image.Image: The resulting PIL.Image | ||
""" | ||
return Image.open(BytesIO(image.data), formats=LIBRARY_SUPPORTED_FORMATS) | ||
|
||
|
||
def pil_to_viam_image(image: Image.Image, mime_type: CameraMimeType) -> ViamImage: | ||
""" | ||
Convert a PIL.Image to a ViamImage. | ||
Args: | ||
image (Image.Image): The image to convert. | ||
mime_type (CameraMimeType): The mime type to convert the image to. | ||
Returns: | ||
ViamImage: The resulting ViamImage | ||
""" | ||
if mime_type.name in LIBRARY_SUPPORTED_FORMATS: | ||
buf = BytesIO() | ||
if image.mode == "RGBA" and mime_type == CameraMimeType.JPEG: | ||
image = image.convert("RGB") | ||
image.save(buf, format=mime_type.name) | ||
data = buf.getvalue() | ||
else: | ||
raise ValueError(f"Cannot encode image to {mime_type}") | ||
|
||
return ViamImage(data, mime_type) |
Oops, something went wrong.