lllyasviel/Fooocus · error · ValueError
Cannot process this value as an Image
Error message
Cannot process this value as an Image
What it means
get_config()/serialize of the vendored gradio Image converts an example value y into base64 for the frontend; it accepts np.ndarray, PIL.Image, and str/Path (file or URL) and raises ValueError('Cannot process this value as an Image') for anything else. This typically fires when preparing example outputs for display, not during prediction.
Source
Thrown at modules/gradio_hijack.py:332
def postprocess(
self, y: np.ndarray | _Image.Image | str | Path | None
) -> str | None:
"""
Parameters:
y: image as a numpy array, PIL Image, string/Path filepath, or string URL
Returns:
base64 url data
"""
if y is None:
return None
if isinstance(y, np.ndarray):
return processing_utils.encode_array_to_base64(y)
elif isinstance(y, _Image.Image):
return processing_utils.encode_pil_to_base64(y)
elif isinstance(y, (str, Path)):
return client_utils.encode_url_or_file_to_base64(y)
else:
raise ValueError("Cannot process this value as an Image")
def set_interpret_parameters(self, segments: int = 16):
"""
Calculates interpretation score of image subsections by splitting the image into subsections, then using a "leave one out" method to calculate the score of each subsection by whiting out the subsection and measuring the delta of the output value.
Parameters:
segments: Number of interpretation segments to split image into.
"""
self.interpretation_segments = segments
return self
def _segment_by_slic(self, x):
"""
Helper method that segments an image into superpixels using slic.
Parameters:
x: base64 representation of an image
"""
x = processing_utils.decode_base64_to_image(x)
if self.shape is not None:View on GitHub (pinned to ae05379cc9)
Solutions
- Convert tensors: img = tensor.mul(255).clamp(0,255).byte().cpu().numpy() (HWC) or wrap with PIL.Image.fromarray before returning/exemplifying.
- Give examples as filesystem paths or URLs (str/Path), not raw bytes.
- Ensure list-of-images goes to gr.Gallery, not Image.
Example fix
# before return torch_tensor # into gr.Image output # after return Image.fromarray(torch_tensor.mul(255).clamp(0,255).byte().cpu().numpy())
Defensive patterns
Strategy: type-guard
Validate before calling
import numpy as np
from PIL import Image
from pathlib import Path
def to_image_value(v):
if isinstance(v, Image.Image) or isinstance(v, (str, Path)) or isinstance(v, np.ndarray):
return v
if hasattr(v, 'cpu'): # torch tensor
return Image.fromarray(v.mul(255).clamp(0, 255).byte().cpu().numpy())
raise TypeError(f'not a serializable image value: {type(v)}') Type guard
def is_image_value(v) -> bool:
import numpy as np
from PIL import Image as PILImage
from pathlib import Path
return isinstance(v, (np.ndarray, PILImage.Image, str, Path)) Prevention
- Convert torch tensors to PIL/numpy in the prediction function's return path, not in the UI layer.
- Give gr.Image examples as path strings; use gr.Gallery for lists of images.
When it happens
Trigger: Passing examples=[...] or returning values containing bytes, a torch.Tensor, a list of images, or None-with-wrong-wrapper to the component's serialization path (e.g. examples=[[42]] or examples=[[tensor]]).
Common situations: Returning a torch.Tensor from a prediction function that feeds an Image output; examples referencing file-like objects instead of paths; numpy scalars or float arrays of unexpected shape.
Related errors
- Invalid value for parameter `type`: {type}. Please choose fr
- Invalid value for parameter `source`: {source}. Please choos
- Image streaming only available if source is 'webcam'.
- Unknown type: {self.type}. Please choose from: 'numpy', 'pil
- Unsupported image type in input
AI-assisted analysis of lllyasviel/Fooocus@ae05379cc9 (2026-08-15).
Data as JSON: /api/errors/78ed0beaa266e37f.
Report an issue: GitHub.