gpt4 book ai didi

python - 将文件转换为数字图像失败,出现 "tile cannot extend outside image"

转载 作者:行者123 更新时间:2023-11-28 16:39:32 25 4
gpt4 key购买 nike

我正在尝试重新创建博客文章中的一些作品 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。任何帮助将不胜感激

最佳答案

设法通过 try/except 循环解决了这个问题。一旦我这样做了,我就能够确定只有某些文件被抵消了。这些文件非常小(125 字节)。我的假设是它们太小而无法创建 scipy 所需的所有信息

关于python - 将文件转换为数字图像失败,出现 "tile cannot extend outside image",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21342758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com