图像分割数据预处理遇TypeError:resize()参数size重复赋值问题求助
Hey there, let's walk through what's causing this error and fix your code properly.
What's Going Wrong?
Your error TypeError: resize() got multiple values for argument 'size' stems from two critical mistakes in how you're using PIL's Image class:
Invalid Image Instance Creation
You can't create a PIL Image withim = Image()— theImageclass doesn't support a blank constructor like that. You need to work with actual image objects, either by opening files directly or converting numpy arrays to PIL Images.Incorrect
resize()Usage
Theresize()method is an instance method (you call it on an existing Image object), not a static method to invoke on an emptyimvariable. Your lineimg = im.resize(img, size = (IMG_HEIGHT, IMG_WIDTH))passes the numpy arrayimgas the first positional argument (which gets interpreted as thesizeparameter, since that's the first expected argument forresize()), then you explicitly passsize=...— this duplicate value is exactly what triggers the error.
Step-by-Step Fixes
First, adjust your import to the more standard format:
from PIL import Image # More standard than from PIL.Image import Image import os import numpy as np from tqdm import tqdm from skimage.io import imread # Assuming you're using scikit-image's imread
Then, fix the image and mask resizing logic in your loop. The core fix involves converting numpy arrays to PIL Images, resizing them, then converting back to numpy arrays:
for n, id_ in tqdm(enumerate(image_ids), total=len(image_ids)): path = DATA_PATH # Read image as numpy array img = imread(os.path.join(path, id_, 'images', f'page{id_}.png'))[:, :IMG_CHANNELS] # Corrected image resize step pil_img = Image.fromarray(img) # Important: PIL uses (width, height) order for size (reversed from numpy's shape) resized_pil_img = pil_img.resize((IMG_WIDTH, IMG_HEIGHT)) img = np.array(resized_pil_img) X[n] = img mask = np.zeros((IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool) # Fix mask resizing logic (same issue as image resize) mask_dir = os.path.join(path, 'masks') for mask_file in os.listdir(mask_dir): mask_ = imread(os.path.join(mask_dir, mask_file)) # Convert to PIL Image, resize, convert back to numpy pil_mask = Image.fromarray(mask_) resized_pil_mask = pil_mask.resize((IMG_WIDTH, IMG_HEIGHT)) mask_ = np.array(resized_pil_mask) # Expand dimensions and update combined mask mask_ = np.expand_dims(mask_, axis=-1) mask = np.maximum(mask, mask_) Y[n] = mask
Key Takeaways
- Shape Order Difference: PIL uses
(width, height)for size parameters, while numpy arrays use(height, width, channels)for image shapes. Mixing these up will result in stretched or incorrectly sized images. - Format Conversion: Always use
Image.fromarray(numpy_array)andnp.array(pil_image)to switch between numpy and PIL formats when working with resizing or other PIL-specific operations. - No Empty Image Objects: Never create a blank
Image()instance — always work with image data loaded from files or converted from existing arrays.
内容的提问来源于stack exchange,提问作者muadgra

