Skip to content

Commit

Permalink
demo_utils/imread_pil.py: return demo pattern if pillow is not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Dec 8, 2024
1 parent 89e79fa commit 7400b8c
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion bindings/imgui_bundle/demos_python/demo_utils/imread_pil.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,63 @@
from numpy.typing import NDArray


_HAS_PIL = False
try:
from PIL import Image
_HAS_PIL = True
except ImportError:
import logging
logging.error("""
pillow is required to read images for Dear ImGui Bundle demo (using demo pattern instead). Install it with:
pip install pillow # or conda install pillow
""")


def _dummy_image(with_alpha: bool) -> NDArray[np.uint8]:
"""
Generates a 400x400 RGBA image with a visually appealing sine wave interference pattern
and a transparent background.
"""
width, height = 400, 400

# Create a grid of x and y coordinates
x = np.linspace(-1 * np.pi, 1 * np.pi, width)
y = np.linspace(-1 * np.pi, 1 * np.pi, height)
X, Y = np.meshgrid(x, y)

# Calculate sine wave interference pattern
pattern = np.sin(X**2 + Y**2) + np.sin(3 * X + 2.5 * Y)

# Normalize the pattern to range [0, 1]
normalized_pattern = (pattern - pattern.min()) / (pattern.max() - pattern.min())

# Map the pattern to RGB colors
R = (np.sin(2 * np.pi * normalized_pattern) * 127 + 128).astype(np.uint8)
G = (np.cos(3 * np.pi * normalized_pattern + np.pi / 2) * 127 + 128).astype(np.uint8)
B = (np.sin(2 * np.pi * normalized_pattern + np.pi) * 127 + 128).astype(np.uint8)

# Combine into an RGB image
rgb_image = np.dstack((R, G, B))

if not with_alpha:
return rgb_image

# Create an alpha channel: fully opaque for non-zero patterns
alpha = (normalized_pattern > 0.15).astype(np.uint8) * 255

# Combine RGB and alpha channels into RGBA
rgba_image = np.dstack((rgb_image, alpha))

return rgba_image




def imread_pil(image_file: str, convert_to_bgr: bool = False, load_alpha: bool = False) -> NDArray[np.uint]:
"""Read an image from a file using PIL, returns a numpy array."""
from PIL import Image
if not _HAS_PIL:
return _dummy_image(load_alpha)

image_pil = Image.open(image_file)

def rgb_to_bgr(image: NDArray[np.uint]) -> NDArray[np.uint]:
Expand Down

0 comments on commit 7400b8c

Please sign in to comment.