You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

图像分割数据预处理遇TypeError:resize()参数size重复赋值问题求助

Fixing TypeError: resize() got multiple values for argument 'size' in Image Segmentation Data Preparation

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:

  1. Invalid Image Instance Creation
    You can't create a PIL Image with im = Image() — the Image class 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.

  2. Incorrect resize() Usage
    The resize() method is an instance method (you call it on an existing Image object), not a static method to invoke on an empty im variable. Your line img = im.resize(img, size = (IMG_HEIGHT, IMG_WIDTH)) passes the numpy array img as the first positional argument (which gets interpreted as the size parameter, since that's the first expected argument for resize()), then you explicitly pass size=... — 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) and np.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:05:57