python-3.x - 错误: name 'Image' not defined

标签 python-3.x numpy python-imaging-library

我正在尝试转置文件夹内的所有图像并尝试执行以下代码:

import os

import glob

path = 'C:/Users/mail2/Desktop/flippin/pics/'
i = 0

for infile in glob.glob( os.path.join(path, '*.jpg') ):

    img = Image.open(infile)
    print ("current file is: " + infile)
    img.transpose(Image.FLIP_LEFT_RIGHT)
    img.transpose(Image.FLIP_TOP_BOTTOM)
    img.transpose(
        Image.FLIP_LEFT_RIGHT).transpose(
        Image.FLIP_TOP_BOTTOM).save("combined%s.jpg")
print('done')

但我不断收到名称'image'未定义错误。

请帮忙。谢谢!!

最佳答案

这是因为您没有定义 Image 来执行此操作,只需从 PIL 模块导入 Image 即可。要实现此目的,只需将此行添加到您的代码中:from PIL import Image:

import os
from PIL import Image
import glob

path = 'C:/Users/mail2/Desktop/flippin/pics/' i = 0

for infile in glob.glob( os.path.join(path, '*.jpg') ):

img = Image.open(infile)
print ("current file is: " + infile)
img.transpose(Image.FLIP_LEFT_RIGHT)
img.transpose(Image.FLIP_TOP_BOTTOM)
img.transpose(
    Image.FLIP_LEFT_RIGHT).transpose(
    Image.FLIP_TOP_BOTTOM).save("combined%s.jpg")

print('done')

关于python-3.x - 错误: name 'Image' not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45529702/

相关文章:

python - 有效地索引 numpy 数组邻居

python - App Engine、PIL 和叠加文本

python-3.x - Python Librosa : What is the default frame size used to compute the MFCC features?

python - Django : Form data not saving

python - 将此 curl 命令转换为 Python 3

python - 使用 PIL 列出图像的所有 RGBA 值

python - 在 Python PIL 中,如何更改图像的质量?

python - 使用 joblib 加载腌制的 scikit-learn 模型时出现 KeyError

python - 使用 Numpy 和 Pandas 优化 Python 代码

python - 从一维 NumPy 数组创建二维掩码