为什么我的图像没有显示?

时间:2017-11-06 23:37:53

标签: python image numpy python-imaging-library pillow

我有以下Python脚本:

import os
from PIL import Image
import numpy

path = '/my_path'
for root, dirs, files in os.walk(path):
    for file in files:
        if file.startswith('first'):
            img1 = numpy.asarray(Image.open(root + '/' + file))
        if file.startswith('second'):
            img2 = numpy.asarray(Image.open(root + '/' + file))

    img1.show()
    img2.show()

当我运行代码时,我得到以下内容:

Traceback (most recent call last):
  File "test.py", line 14, in <module>
    img1.show()
NameError: name 'img1' is not defined

如果我想首先阅读img1img2,我该如何显示图像?

感谢。

1 个答案:

答案 0 :(得分:1)

我认为你有一个范围问题。如果你在第一个for循环之后立即初始化img1和img2应该可以做到这一点。

相关问题