在Python中,我使用wand来获取exif数据(创建真实日期图像),并获取图像高度和宽度以传递给命令行。这就是我用魔杖的全部内容。除非脚本遇到带有错误数据的图像,否则一切运行良好,这是处理250,000多个图像时发生的。当文件本身损坏时,它会导致整个脚本失败。我需要找到一种方法来允许脚本继续执行,即使图像无法正确加载。
我正在使用以下导入
import inspect, os, shutil, datetime, time, string, configparser, fnmatch, os, MySQLdb, sys, ntpath, math
import MySQLdb.cursors as cursors
from wand.image import Image
from wand.display import display
from tendo import singleton
以下是失败的行:
with Image(filename=file_original_path) as image:
这是追溯:
Traceback (most recent call last):
File "process-files.py", line 244, in <module>
main()
File "process-files.py", line 216, in main
with Image(filename=file_original_path) as image:
File "/home/pc/.local/lib/python2.7/site-packages/wand/image.py", line 2744, in __init__
self.read(filename=filename, resolution=resolution)
File "/home/pc/.local/lib/python2.7/site-packages/wand/image.py", line 2822, in read
self.raise_exception()
File "/home/pc/.local/lib/python2.7/site-packages/wand/resource.py", line 222, in raise_exception
raise e
wand.exceptions.CoderError: Not a TIFF or MDI file, bad magic number 1280 (0x500). `assets/output/27/original.tif' @ error/tiff.c/TIFFErrors/566
Exception TypeError: TypeError("object of type 'NoneType' has no len()",) in <bound method Image.__del__ of <wand.image.Image: (empty)>> ignored
除了通行证之外,尝试BTW也不起作用。谢谢。
答案 0 :(得分:1)
尝试/除了工作,不会导致整个脚本失败。
import traceback
from wand.image import Image
from wand.display import display
def doSomething(f):
_status = 'error'
try:
print "processing file: %s" % f
with Image(filename=f) as image:
print image
_status = 'processed'
except:
traceback.print_exc()
finally:
return _status
if __name__ == '__main__':
result = {}
images = ['/tmp/foo.png', '/tmp/bar.png', '/usr/share/locale/kde4/l10n/cf/flag.png']
for i in images:
status = doSomething(i)
result[i] = status
print result
输出:{'/tmp/foo.png': 'error', '/usr/share/locale/kde4/l10n/cf/flag.png': 'processed', '/tmp/bar.png': 'error'}
答案 1 :(得分:0)
错了......尝试除了传球确实有效。混淆类似的回调。