is_visual_object

pyhelpers.ops.is_visual_object(obj)[source]

Determines if an object is a known figure or image type.

This function checks for visual objects post-conversion, supporting Matplotlib Figures/Axes, Seaborn grids, Plotly figures, and PIL Images. It is designed to be library-agnostic by checking for common methods (‘.show’ or ‘.savefig’) and using string-based type inspection to avoid explicit imports of heavy libraries.

Parameters:

obj (Any) – The object to inspect.

Returns:

True if the object is identified as a figure or image, else False.

Return type:

bool

Examples:

>>> from pyhelpers.ops import is_visual_object
>>> import matplotlib.pyplot as plt
>>> from PIL import Image
>>> import numpy as np

>>> # Case 1: Matplotlib Figure
>>> fig, ax = plt.subplots()
>>> is_visual_object(fig)
True

>>> # Case 2: PIL Image converted from array
>>> img = Image.fromarray(np.zeros((10, 10), dtype=np.uint8))
>>> is_visual_object(img)
True

>>> # Case 3: Raw data (not converted)
>>> is_visual_object([1, 2, 3])
False