{"record":{"id":"b132b956bb427941","repo":"huggingface/transformers","slug":"size-must-have-2-elements-representing-the-height","errorCode":null,"errorMessage":"size must have 2 elements representing the height and width of the output image","messagePattern":"size must have 2 elements representing the height and width of the output image","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/image_transforms.py","lineNumber":479,"sourceCode":"            The channel dimension format for the output image. Can be one of:\n                - `\"channels_first\"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.\n                - `\"channels_last\"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.\n            If unset, will use the inferred format of the input image.\n        input_data_format (`str` or `ChannelDimension`, *optional*):\n            The channel dimension format for the input image. Can be one of:\n                - `\"channels_first\"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.\n                - `\"channels_last\"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.\n            If unset, will use the inferred format of the input image.\n    Returns:\n        `np.ndarray`: The cropped image.\n    \"\"\"\n    requires_backends(center_crop, [\"vision\"])\n\n    if not isinstance(image, np.ndarray):\n        raise TypeError(f\"Input image must be of type np.ndarray, got {type(image)}\")\n\n    if not isinstance(size, Iterable) or len(size) != 2:\n        raise ValueError(\"size must have 2 elements representing the height and width of the output image\")\n\n    if input_data_format is None:\n        input_data_format = infer_channel_dimension_format(image)\n    output_data_format = data_format if data_format is not None else input_data_format\n\n    # We perform the crop in (C, H, W) format and then convert to the output format\n    image = to_channel_dimension_format(image, ChannelDimension.FIRST, input_data_format)\n\n    orig_height, orig_width = get_image_size(image, ChannelDimension.FIRST)\n    crop_height, crop_width = size\n    crop_height, crop_width = int(crop_height), int(crop_width)\n\n    # In case size is odd, (image_shape[0] + size[0]) // 2 won't give the proper result.\n    top = (orig_height - crop_height) // 2\n    bottom = top + crop_height\n    # In case size is odd, (image_shape[1] + size[1]) // 2 won't give the proper result.\n    left = (orig_width - crop_width) // 2\n    right = left + crop_width","sourceCodeStart":461,"sourceCodeEnd":497,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/image_transforms.py#L461-L497","documentation":"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.","triggerScenarios":"center_crop(img, size=224) (int), center_crop(img, size=(224, 224, 3)), or size=None.","commonSituations":"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.","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."],"exampleFix":"# before\ncropped = center_crop(img, size=224)\n\n# after\ncropped = center_crop(img, size=(224, 224))","handlingStrategy":"validation","validationCode":"if isinstance(size, int):\n    size = (size, size)\nassert isinstance(size, (tuple, list)) and len(size) == 2, \"crop size must be (height, width)\"","typeGuard":"def is_hw_pair(s) -> bool:\n    return isinstance(s, (tuple, list)) and len(s) == 2","tryCatchPattern":null,"preventionTips":["Normalize legacy int crop_size values to (c, c) when loading old configs.","Keep resize size and crop size handling in one shared helper."],"tags":["image-processing","crop","validation","valueerror"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}