将文件转换为数字图像失败,“tile无法扩展到图像外”

时间:2014-01-24 21:25:27

标签: python numpy scipy python-imaging-library

我正在尝试从博文http://sarvamblog.blogspot.com/2013/04/clustering-malware-corpus.html

重新创建一些作品
import itertools
import glob
import numpy,scipy, os, array
from scipy.misc import imsave

for filename in list(glob.glob('file/*.file')):
    f = open(filename,'rb');
        #just want to make sure I get the right file'
    print filename
    ln = os.path.getsize(filename); # length of file in bytes
    width = 256;
    rem = ln%width; 
    a = array.array("B"); # uint8 array
    a.fromfile(f,ln-rem);
    f.close(); 
    g = numpy.reshape(a,(len(a)/width,width));
    g = numpy.uint8(g);
    fpng = filename + ".png"
        # make sure the png process and everything else is going'
    print fpng
    scipy.misc.imsave(fpng,g);`

虽然这在1或2个文件上运行得很好,但是一旦我扩展到几十个

,我就会遇到问题
Traceback (most recent call last):
  File "<stdin>", line 14, in <module>
  File "/usr/lib/python2.7/dist-packages/scipy/misc/pilutil.py", line 120, in imsave
   im = toimage(arr)
  File "/usr/lib/python2.7/dist-packages/scipy/misc/pilutil.py", line 183, in toimage
    image = Image.fromstring('L',shape,bytedata.tostring())
  File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1797, in fromstring
    im.fromstring(data, decoder_name, args)
  File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 590, in fromstring
    d.setimage(self.im)
  ValueError: tile cannot extend outside image

我认为我的问题不是A:关闭scipy.misc.imsave或B:没有重置arrarys。任何帮助将不胜感激

2 个答案:

答案 0 :(得分:2)

使用try / except循环进行管理。一旦我这样做,我就能确定只有某些文件正在取消。这些文件非常小(125字节)。我的假设是它们太小而无法创建scipy所需的所有信息

答案 1 :(得分:1)

im.crop(方框)⇒图像 该框是一个4元组,用于定义左,上,右和下像素坐标。

当我的代码中的lower小于upper时,会发生此错误。