首页 > 解决方案 > numpy.core._exceptions.UFuncTypeError: ufunc 'add' 不包含带有签名匹配的循环

问题描述

我正在遍历一个目录以调整图像大小,然后将新调整的图像保存在另一个现有目录中。我不断收到以下错误:

Traceback (most recent call last):
  File "preprocessingdatacopy.py", line 23, in <module>
    new_filename = image_resized + 'new.png'
numpy.core._exceptions.UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32')

编码:

import matplotlib.pyplot as plt
import skimage
from sklearn import preprocessing
from skimage import data, color
import os
from skimage.transform import resize, rescale
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from skimage import io
import cv2
import os
from pathlib import Path
directory_in_str = "/home/briannagopaul/imagemickey/"
directory = os.fsencode(directory_in_str)
for file in os.listdir(directory):
    print(file)
    filename = directory_in_str + os.fsdecode(file)
    if filename.endswith(".png"):
        img = mpimg.imread(filename)
        # cropped = img[484:384, 380:384]
        image_resized = resize(img, (128, 128))
        path2 = ("/home/briannagopaul/preprocessed")
        new_filename = image_resized + 'new.png'
        image_file.save(str(path2 / new_filename))


标签: pythonpathscikit-imageimage-preprocessing

解决方案


您不能将图像连接到字符串。您需要将 image_resized 替换为字符串数据类型(即文件名),因为skimage.transform.resize()将图像作为 N 维数组返回。


推荐阅读