python - 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))
解决方案
您不能将图像连接到字符串。您需要将 image_resized 替换为字符串数据类型(即文件名),因为skimage.transform.resize()将图像作为 N 维数组返回。
推荐阅读
- excel - 尝试将新的格式化指令插入现有代码时收到“编译错误:Next without For”
- java - 用用户输入的日期填充数组
- mysql - 从工作子选择中插入日期发生错误
- asp.net-mvc - 发生异常时如何中止提交?
- python - NOT NULL 约束失败:home_profile.user_id
- javascript - 如何在不刷新网页的情况下刷新测验
- php - PHP 中会话名称的限制是什么?
- java - 类中的这些字段有什么区别
- mysql - 在迁移中使用我的 schema.rb 时,我收到关于 ID 字段的错误
- matrix - IML 的替代品?在没有 IML 的情况下运行 SAS Viya,需要合并两个非嵌套集群的协方差矩阵