【发布时间】:2017-08-24 14:46:36
【问题描述】:
我有这张图片 =>
这里是写在3.txt文件中的以上黄色框的所有坐标。
#Y X Height Width
46 135 158 118
46 281 163 104
67 494 188 83
70 372 194 101
94 591 207 98
252 132 238 123
267 278 189 105
320 741 69 141
322 494 300 135
323 389 390 124
380 726 299 157
392 621 299 108
449 312 227 93
481 161 425 150
678 627 285 91
884 13 650 437
978 731 567 158
983 692 60 43
1402 13 157 114
我的意图是裁剪这些框并将所有框保存为图像。我为此编写了一个代码,但出现错误。
这是我的代码 =>
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
from os import listdir
#from scipy.misc import imsave
ARR = np.empty([1,4])
# print(ARR)
i = 0
k = 0
img = Image.open('3.png')
fo = open("3.txt", "r")
for line in fo:
if not line.startswith('#'):
for word in line.split():
ARR[0][i] = int(word)
print(int(word))
# ARR[0][i] = int(word)
i = i +1
img2 = img.crop((int(ARR[0][1]), int(ARR[0][0]), int(ARR[0][0] + ARR[0][2]), int(ARR[0][1] + ARR[0][3])))
name = "new-img" + str(k) + ".png"
img2.save(name)
k = k + 1
i = 0
我收到这些错误 =>
Traceback(最近一次调用最后一次):文件“reshape.py”,第 26 行,在 img2.save(name) 文件“/usr/lib/python2.7/dist-packages/PIL/Image.py”,第 1468 行,保存中 save_handler(self, fp, filename) 文件“/usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.py”,第 624 行,在 _保存 ImageFile._save(im, _idat(fp, chunk), [("zip", (0,0)+im.size, 0, rawmode)]) 文件 "/usr/lib/python2.7/dist-packages/ PIL/ImageFile.py", 第 462 行,在 _save e.setimage(im.im, b) SystemError: tile cannot extend outside image
我该如何解决这些问题?
【问题讨论】:
-
错误状态 * 瓦片不能延伸到图像之外*,这意味着您正在尝试裁剪超出图像尺寸的区域。检查文本文件是否包含正确的坐标。您可能会对图像的坐标系感到困惑(我认为)。在继续之前一定要复习一下……
-
谢谢,我想你是 r8。 @JeruLuke
-
在这样做之后 => img2 = img.crop((int(ARR[0][0]), int(ARR[0][1]), int(ARR[0][0] ] + ARR[0][2]), int(ARR[0][1] + ARR[0][3]))) 它正在工作,谢谢@JeruLuke
-
太棒了!我在 OpenCV 中多次犯过同样的错误
标签: python image image-processing computer-vision