-2

嗨,我在文件夹 a 中有一个包含彩色图像的文件夹,我想将它们更改为灰度图像并保存在文件夹 b 中

from PIL import Image 
import os
# Changing image to gray and scaling to 256x128
WORK_DIR = 'D:/folder/data/' #working folder 

source = WORK_DIR + 'a'
target = WORK_DIR +'b'

for dirpath, filenames in os.walk(source):
    for file in filenames:
        image_file = Image.open(os.path.join(dirpath, file))
        image_file.draft('L', (256, 128)) #convert to gray and 256x128
        image_file.save(os.path.join(target, file))

我收到以下错误,我不确定它是什么意思,我该如何解决?

----> 7 for dirpath, filenames in os.walk(source):
      8         for file in filenames:
      9                 image_file = Image.open(os.path.join(dirpath, file))

ValueError: too many values to unpack (expected 2)

谢谢!

4

1 回答 1

1

您收到此错误是因为 os.walk() 在一个元组(根、目录、文件)中返回 3 个项目。

只是改变

for dirpath, filenames in os.walk(source):

for root, dirpath, filenames in os.walk(source):

作为丹 d。指出,你应该改变

os.path.join(dirpath, file)

os.path.join(root, file)
于 2018-12-20T00:27:50.523 回答