在python中闪烁多个图像

时间:2016-08-19 06:24:57

标签: python opencv matplotlib

我是python的新手,我正在努力闪烁(显示)像516张图片一样。之前有很多这样的问题,但没有人帮助过我。 这是代码

import cv2
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
for i in range(1,516,1):
    #a=cv2.imread('test01001.tif')
    abb=cv2.imread('This PC\G:\TRAINING\1\test0100%d.tif'%(i))
    cv2.imshow('test0100%d.tif'%(i),abb)

plt.show()

显示的错误是

Using matplotlib backend: Qt4Agg
Populating the interactive namespace from numpy and matplotlib
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-10-709418c86b86> in <module>()
      7     #a=cv2.imread('test01001.tif')
      8     abb=cv2.imread('This PC\G:\TRAINING\1\test0100%d.png'%(i))
----> 9     cv2.imshow('test0100%d.tif'%(i),abb)
     10 
     11 plt.show()

error: ..\..\..\..\opencv\modules\highgui\src\window.cpp:266: error: (-215) size.width>0 && size.height>0 in function cv::imshow

请帮帮我。我一直试图纠正这个错误很多天但没有任何帮助我。如果代码也在答案中发布,我将不胜感激 非常感谢

调试代码

Using matplotlib backend: Qt4Agg
Populating the interactive namespace from numpy and matplotlib
> <ipython-input-2-d89d3a2017ad>(11)<module>()
-> cv2.imshow('test0100%d.tif'%(i),abb)
(Pdb) n
error: '..\\..\\..\\..\\opencv\\modules\\highgui\\src\\window.cpp:266: error: (-215) size.width>0 && size.height>0 in function cv::imshow\n'
> <ipython-input-2-d89d3a2017ad>(11)<module>()
-> cv2.imshow('test0100%d.tif'%(i),abb)
(Pdb) n
--Return--
> <ipython-input-2-d89d3a2017ad>(11)<module>()->None
-> cv2.imshow('test0100%d.tif'%(i),abb)
(Pdb) n
error: '..\\..\\..\\..\\opencv\\modules\\highgui\\src\\window.cpp:266: error: (-215) size.width>0 && size.height>0 in function cv::imshow\n'
> c:\anaconda2\lib\site-packages\ipython\core\interactiveshell.py(3066)run_code()
-> exec(code_obj, self.user_global_ns, self.user_ns)
(Pdb) n
> c:\anaconda2\lib\site-packages\ipython\core\interactiveshell.py(3069)run_code()
-> sys.excepthook = old_excepthook
(Pdb) n
> c:\anaconda2\lib\site-packages\ipython\core\interactiveshell.py(3070)run_code()
-> except SystemExit as e:
(Pdb) n
> c:\anaconda2\lib\site-packages\ipython\core\interactiveshell.py(3075)run_code()
-> except self.custom_exceptions:
(Pdb) n
> c:\anaconda2\lib\site-packages\ipython\core\interactiveshell.py(3080)run_code()
-> except:
(Pdb) n
> c:\anaconda2\lib\site-packages\ipython\core\interactiveshell.py(3081)run_code()
-> if result is not None:
(Pdb) n
> c:\anaconda2\lib\site-packages\ipython\core\interactiveshell.py(3082)run_code()
-> result.error_in_exec = sys.exc_info()[1]
(Pdb) n
> c:\anaconda2\lib\site-packages\ipython\core\interactiveshell.py(3083)run_code()
-> self.showtraceback()

2 个答案:

答案 0 :(得分:0)

imread的文件路径很可能无效,因此您无法在abb中获得正确的图像,当然无法显示该图像,因此您会收到有关图像大小的错误。

答案 1 :(得分:0)

您可以使用python os模块浏览目录, 而且你不需要绘制闪光图像。

import cv2
import os         # import os module

path = r"G:\TRAINING\1"
img_names = os.listdir(path) # return all image names in given path

for name in img_names:
    im = cv2.imread( os.path.join(path,name) )
    cv2.imshow('image' ,im)
    cv2.waitKey(300)  # no of millisecond to wait for next image

cv2.destroyAllWindows()