我收到错误:“类型错误:字符串索引必须是整数”当我尝试裁剪图像时我正在尝试编写一个裁剪矩形图像的函数,以便制作方形图像并居中。def squared_and_resized(img,resized_dim): image = cv2.imread(img) img_height,img_width = image.shape[:2] if (img_width > img_height): start_row = 0 end_row = img_height start_col = math.floor((img_width - img_height) /2) end_col = math.floor((img_width + img_height) /2) else: start_col = 0 end_col = img_width start_row = math.floor((img_height-img_width)/2) end_row = start_row + img_width squared_img = img[start_row:end_row , start_col:end_col] resized_img = cv2.resize(squared_img,(resized_dim, resized_dim)) return resized_img
2 回答
犯罪嫌疑人X
TA贡献2080条经验 获得超4个赞
错误在行中:squared_img = img[start_row:end_row , start_col:end_col]
。经过代码检查,它似乎img
是str
传递给此方法并稍后用于图像切片的类型。您可能需要使用squared_img = image[start_row:end_row , start_col:end_col]
为了在未来缓解此问题,请使用有意义的名称。在这种情况下,方法参数可以命名为image_path
.
添加回答
举报
0/150
提交
取消