首页 文章

保存图像期间,PIL中的“SystemError:tile无法扩展到图像外部”

提问于
浏览
2

我有这个Image =>

enter image description here

这里是上面黄色框的所有坐标,用 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行,在_save 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无法扩展到图像外部

我该如何解决这些问题?

1 回答

  • 3

    参考注释,由于将坐标不正确地传递给PIL的 crop() 函数而发生错误 .

    the documentation中所述,该函数返回一个四元组的图像( xywidthheight ) .

    在给定的文本文件中,第一列中提到了 y 坐标,第二列中提到了 x 坐标 . 但是, crop() 函数接受 x 坐标值作为第一个参数,接受 y 坐标作为第二个参数 .

    这同样适用于OpenCV

    关于这一点,这是ANOTHER POST .

相关问题