我正在尝试解决cv2.imwrite不断抛出的错误,它不断到达文件夹中的相同图像,然后在cv2.imwrite行说“-215:断言失败”,而我写的运气并不好失败的保险柜为该类型的错误,它正在正确读取,所以我不知道为什么它不会写。
我尝试了以下两种方法:
writeStatus = cv2.imwrite('output/'+ i, cropped)
if writeStatus is True:
print('Image Written')
else:
print('Error saving photo')
cv2.imwrite('rejected/'+ i, image)
if cv2.imwrite('output/'+ i, cropped) == True:
print('Image write succeeded')
else:
if cv2.imwrite('rejected/'+ i, image)== True:
print ('Cropped image could not be written, sent to rejects')
else:
print('writing error, script stopped')
break
我不知道为什么它不保存图像,到目前为止,它似乎只讨厌这个特定的图像,即使图像仅在文件夹中也不会写,我也不会在乎,只要我能输出这样的图像即可。拒绝文件夹,而不是杀死整个脚本。编辑尝试使用的
writeStatus = cv2.imwrite('output/'+ i, cropped)
if writeStatus is True:
print('Image Written')
else:
print('Error saving photo')
continue
并且错误仍然停止了脚本
最佳答案
建议#1
如果我们看一下语句:
writeStatus = cv2.imwrite('output/'+ i, cropped)
'output/' + i
应该是图像名称,裁剪后应该是图像。据我了解,您想保存
i
th值,但是正确的方法是:from os.path import join
filename = join('output', str(i) + '.png') # you can use other extensions like .jpg
cv2.imwrite(filename, cropped)
但是,如果cropped
变量是图像,则为true。建议2
如果单个图像引起问题并且您不在乎,则可以使用
try-except
块和continue
关键字try:
writeStatus = cv2.imwrite(...)
.
.
except:
continue
关于python - 如何处理CV2(Cv2.imwrite)中的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63564966/