huggingface/transformers · error · ValueError
size must have 2 elements representing the height and width
Error message
size must have 2 elements representing the height and width of the output image
What it means
center_crop requires `size` to be an iterable of exactly two values (crop height, crop width); a non-iterable (bare int) or wrong-length iterable raises ValueError. Unlike resize helpers, ints are not auto-expanded to squares.
Source
Thrown at src/transformers/image_transforms.py:479
The channel dimension format for the output image. Can be one of:
- `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
- `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
If unset, will use the inferred format of the input image.
input_data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format for the input image. Can be one of:
- `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
- `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
If unset, will use the inferred format of the input image.
Returns:
`np.ndarray`: The cropped image.
"""
requires_backends(center_crop, ["vision"])
if not isinstance(image, np.ndarray):
raise TypeError(f"Input image must be of type np.ndarray, got {type(image)}")
if not isinstance(size, Iterable) or len(size) != 2:
raise ValueError("size must have 2 elements representing the height and width of the output image")
if input_data_format is None:
input_data_format = infer_channel_dimension_format(image)
output_data_format = data_format if data_format is not None else input_data_format
# We perform the crop in (C, H, W) format and then convert to the output format
image = to_channel_dimension_format(image, ChannelDimension.FIRST, input_data_format)
orig_height, orig_width = get_image_size(image, ChannelDimension.FIRST)
crop_height, crop_width = size
crop_height, crop_width = int(crop_height), int(crop_width)
# In case size is odd, (image_shape[0] + size[0]) // 2 won't give the proper result.
top = (orig_height - crop_height) // 2
bottom = top + crop_height
# In case size is odd, (image_shape[1] + size[1]) // 2 won't give the proper result.
left = (orig_width - crop_width) // 2
right = left + crop_widthView on GitHub (pinned to a597f97485)
Solutions
- Pass crop_size as a 2-tuple: center_crop(img, (224, 224)).
- When loading configs, normalize int crop_size to (c, c).
- Use get_size_dict / the processor layer which handles legacy int configs.
Example fix
# before cropped = center_crop(img, size=224) # after cropped = center_crop(img, size=(224, 224))
Defensive patterns
Strategy: validation
Validate before calling
if isinstance(size, int):
size = (size, size)
assert isinstance(size, (tuple, list)) and len(size) == 2, "crop size must be (height, width)" Type guard
def is_hw_pair(s) -> bool:
return isinstance(s, (tuple, list)) and len(s) == 2 Prevention
- Normalize legacy int crop_size values to (c, c) when loading old configs.
- Keep resize size and crop size handling in one shared helper.
When it happens
Trigger: center_crop(img, size=224) (int), center_crop(img, size=(224, 224, 3)), or size=None.
Common situations: Configs where crop_size was written as an int (some older configs do), copying resize-style int size into a crop call, or including a channel dim in the tuple.
Related errors
- {param_name} must have one of the following set of keys: {VA
- Unsupported channel dimension format: {channel_dim}
- size must have 1 or 2 elements if it is a list or tuple
- size must have 2 elements
- mean must have {num_channels} elements if it is an iterable,
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/b132b956bb427941.
Report an issue: GitHub.