使用matplotlib.image从文件夹中读取图像

时间:2017-08-06 03:31:55

标签: python-2.7 opencv matplotlib

我试图在一个图中的循环中一个接一个地显示图像,这意味着在显示图像1之后,在几秒之后,图像2显示在同一图中。 到目前为止,我有以下代码:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

%matplotlib inline

for i in range(1,4):
     PATH = "kodim01.png"
     N = "%02d" % i
     print PATH.replace("01", N)
     image = mpimg.imread(PATH) # images are color images
     plt.show()
     plt.imshow(image)

然而,它显示一次图像(第一张图像)3次。虽然路径发生了变化。图像不会改变。请看下面的结果: Here

我怎样才能1)将所有图像一个接一个地显示在一个图中,即将连续图像替换为前一个图像  例如每张图像之间延迟1秒。 2)显示所有图像,不仅重复一个图像?

由于

4 个答案:

答案 0 :(得分:1)

更一致的方法是:

  1. 使用gem uninstall heroku将所有图像名称获取到变量中。例如dir()图像将保存路径指向的目录中的所有图像名称。
  2. 然后像这样遍历图像:

    images = dir(path)
  3. 我不喜欢字符串操作的原因是因为它是一个短期修复。以这种方式做得更好,以便它能以更通用的格式工作。

答案 1 :(得分:1)

这是我的解决方案:

my @mathenv = qw(int frac prod sum oint bigvee bigcup bigcap bigoplus bigotimes);

while(<DATA>) {  $mathchk .= $_;  }

$boolChk = 'False';
$mathchk=~s#\\left\(((?:(?!\\(?:left|right)).)*)\\right\)# my $fulcnt=$&; my $Content=$1;
foreach my $singlemath(@mathenv)
{
    \#print "S: -$singlemath-\n";
    if($fulcnt=~m/\\$singlemath/gi)
    {  $boolChk = 'True';  }
}
print ": $boolChk \t$fulcnt\n"; system 'pause'; 
if($boolChk eq 'True') {   }
if($boolChk eq 'False') {  $fulcnt = "\($Content\)";  }
($fulcnt);
#esg;

#print $mathchk;

__DATA__
"\left({H^1}, \int {H^2}\right)"
"\left({H^1},\bigoplus{H^2}\right)"
"\left({H^1},{H^2}\right)"
"\left({H^1}, \bigvee {H^2}\right)"

答案 2 :(得分:1)

使用Error:Cannot add a configuration with name 'endpoints' as a configuration with that name already exists. 后端

Private Sub Form_Timer() Me.Requery End Sub 后端将matplotlib图显示为png图像。您可以使用%matplotlib inline显示图像,在这种情况下,会在一段时间后显示另一张图像。

%matplotlib inline

请注意,在这种情况下,您不会在同一图中显示图像,而是将旧图像替换为新图像。

使用IPython.display后端

您可以直接使用交互式后端来显示您的情节。这允许使用import matplotlib.pyplot as plt import matplotlib.image as mpimg from IPython import display import time %matplotlib inline PATH = "../data/kodim{0:02d}.png" for i in range(1,4): p = PATH.format(i) #print p image = mpimg.imread(p) # images are color images plt.gca().clear() plt.imshow(image); display.display(plt.gcf()) display.clear_output(wait=True) time.sleep(1.0) # wait one second

%matplotlib notebook

答案 3 :(得分:0)

import glob
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
%matplotlib inline

images = []
for img_path in glob.glob('folder/*.png'):
    images.append(mpimg.imread(img_path))

plt.figure(figsize=(20,10))
columns = 5
for i, image in enumerate(images):
    plt.subplot(len(images) / columns + 1, columns, i + 1)
    plt.imshow(image)

其中“文件夹”包含扩展名为.png的图像

相关问题