Skip to content

Commit

Permalink
Use pil insteadof imageio (#33)
Browse files Browse the repository at this point in the history
* check on the latent batchsize

* remove imageio pacakge depencies
  • Loading branch information
doombeaker authored Jul 15, 2024
1 parent 34c31f1 commit e7df1eb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
10 changes: 5 additions & 5 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
NODE_DISPLAY_NAME_MAPPINGS.update(**kolors.NODE_DISPLAY_NAME_MAPPINGS)


# from . import nodes_controlnet_union_sdxl
from . import nodes_controlnet_union_sdxl

# NODE_CLASS_MAPPINGS.update(**nodes_controlnet_union_sdxl.NODE_CLASS_MAPPINGS)
# NODE_DISPLAY_NAME_MAPPINGS.update(
# **nodes_controlnet_union_sdxl.NODE_DISPLAY_NAME_MAPPINGS
# )
NODE_CLASS_MAPPINGS.update(**nodes_controlnet_union_sdxl.NODE_CLASS_MAPPINGS)
NODE_DISPLAY_NAME_MAPPINGS.update(
**nodes_controlnet_union_sdxl.NODE_DISPLAY_NAME_MAPPINGS
)
37 changes: 25 additions & 12 deletions image_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import io
import base64
import io
import os
from typing import Union, List
from PIL import Image
import imageio.v2 as imageio


import numpy as np
from PIL import Image
import torch

BIZYAIR_DEBUG = os.getenv("BIZYAIR_DEBUG", False)


def convert_image_to_rgb(image: Image.Image) -> Image.Image:
if image.mode != "RGB":
Expand All @@ -14,27 +18,36 @@ def convert_image_to_rgb(image: Image.Image) -> Image.Image:


def encode_image_to_base64(
image: Image.Image, format: str = "WEBP", quality: int = 100
image: Image.Image, format: str = "png", quality: int = 100
) -> str:
image = convert_image_to_rgb(image)
with io.BytesIO() as output:
imageio.imwrite(output, image, format=format, quality=quality)
image.save(output, format=format, quality=quality)
output.seek(0)
img_bytes = output.getvalue()
print(f"encode_image_to_base64: {format_bytes(len(img_bytes))}")
if BIZYAIR_DEBUG:
print(f"encode_image_to_base64: {format_bytes(len(img_bytes))}")
return base64.b64encode(img_bytes).decode("utf-8")


def decode_base64_to_np(img_data: str, format: str = "WEBP") -> np.ndarray:
def decode_base64_to_np(img_data: str, format: str = "png") -> np.ndarray:
img_bytes = base64.b64decode(img_data)
print(f"decode_base64_to_np: {format_bytes(len(img_bytes))}")
if BIZYAIR_DEBUG:
print(f"decode_base64_to_np: {format_bytes(len(img_bytes))}")
with io.BytesIO(img_bytes) as input_buffer:
img = imageio.imread(input_buffer, format=format)
return img
img = Image.open(input_buffer)
img = img.convert("RGBA")
return np.array(img)


def decode_base64_to_image(img_data: str, format: str = "WEBP") -> Image.Image:
return Image.fromarray(decode_base64_to_np(img_data, format))
def decode_base64_to_image(img_data: str) -> Image.Image:
img_bytes = base64.b64decode(img_data)
with io.BytesIO(img_bytes) as input_buffer:
img = Image.open(input_buffer)
if BIZYAIR_DEBUG:
format_info = img.format.upper() if img.format else "Unknown"
print(f"decode image format: {format_info}")
return img


def format_bytes(num_bytes: int) -> str:
Expand Down

0 comments on commit e7df1eb

Please sign in to comment.