Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 4 additions & 5 deletions gradio/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,9 +1357,9 @@ def postprocess(self, y: np.ndarray | PIL.Image | str | Path) -> str:
dtype = "file"
else:
raise ValueError("Cannot process this value as an Image")
if dtype in ["numpy", "pil"]:
if dtype == "pil":
y = np.array(y)
if dtype == "pil":
out_y = processing_utils.encode_pil_to_base64(y)
elif dtype == "numpy":
out_y = processing_utils.encode_array_to_base64(y)
elif dtype == "file":
out_y = processing_utils.encode_url_or_file_to_base64(y)
Expand Down Expand Up @@ -3262,8 +3262,7 @@ def postprocess(self, y: List[np.ndarray | PIL.Image | str] | None) -> List[str]
if isinstance(img, np.ndarray):
img = processing_utils.encode_array_to_base64(img)
elif isinstance(img, PIL.Image.Image):
img = np.array(img)
img = processing_utils.encode_array_to_base64(img)
img = processing_utils.encode_pil_to_base64(img)
elif isinstance(img, str):
img = processing_utils.encode_url_or_file_to_base64(img)
else:
Expand Down
21 changes: 20 additions & 1 deletion gradio/processing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import numpy as np
import requests
from ffmpy import FFmpeg, FFprobe, FFRuntimeError
from PIL import Image, ImageOps
from PIL import Image, ImageOps, PngImagePlugin

from gradio import encryptor

Expand Down Expand Up @@ -103,6 +103,25 @@ def save_pil_to_file(pil_image, dir=None):
return file_obj


def encode_pil_to_base64(pil_image):
with BytesIO() as output_bytes:

# Copy any text-only metadata
use_metadata = False
metadata = PngImagePlugin.PngInfo()
for key, value in pil_image.info.items():
if isinstance(key, str) and isinstance(value, str):
metadata.add_text(key, value)
use_metadata = True

pil_image.save(
output_bytes, "PNG", pnginfo=(metadata if use_metadata else None)
)
bytes_data = output_bytes.getvalue()
base64_str = str(base64.b64encode(bytes_data), "utf-8")
return "data:image/png;base64," + base64_str


def encode_array_to_base64(image_array):
with BytesIO() as output_bytes:
pil_image = Image.fromarray(_convert(image_array, np.uint8, force_copy=False))
Expand Down
17 changes: 17 additions & 0 deletions test/test_processing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ def test_encode_array_to_base64(self):
output_base64 = gr.processing_utils.encode_array_to_base64(numpy_data)
self.assertEqual(output_base64, deepcopy(media_data.ARRAY_TO_BASE64_IMAGE))

def test_encode_pil_to_base64(self):
img = Image.open("gradio/test_data/test_image.png")
img = img.convert("RGB")
img.info = {} # Strip metadata
output_base64 = gr.processing_utils.encode_pil_to_base64(img)
self.assertEqual(output_base64, deepcopy(media_data.ARRAY_TO_BASE64_IMAGE))

def test_encode_pil_to_base64_keeps_pnginfo(self):
input_img = Image.open("gradio/test_data/test_image.png")
input_img = input_img.convert("RGB")
input_img.info = {"key1": "value1", "key2": "value2"}

encoded_image = gr.processing_utils.encode_pil_to_base64(input_img)
decoded_image = gr.processing_utils.decode_base64_to_image(encoded_image)

self.assertEqual(decoded_image.info, input_img.info)

def test_resize_and_crop(self):
img = Image.open("gradio/test_data/test_image.png")
new_img = gr.processing_utils.resize_and_crop(img, (20, 20))
Expand Down