AttributeError: 'module' object has no attribute 'fsdecode'; PYTHON 2.7; mac os X

AttributeError: 'module' object has no attribute 'fsdecode'; PYTHON 2.7; mac os X

我在 PYTHON 中找到了一个脚本。需要 运行 它来削减图像的大小。 为什么会出现此错误以及如何解决? (Python 2.7 已安装+ 当前脚本已安装 PIL+ Imaging 1.1.7;Mac OS)

from __future__ import division
from __future__ import print_function
from __future__ import absolute_import

import argparse
import os
from PIL import Image

parser = argparse.ArgumentParser()
parser.add_argument('--image_dir', help='Directory of images to resize')
args = parser.parse_args()

image_dir = os.getcwd() + "/" + args.image_dir

for f in os.listdir(image_dir):
    filename = os.fsdecode(f)
    image = Image.open(image_dir + '/' + filename)
    print(image_dir + '/' + filename)
    height, width = image.size
    if width > 1000:
        resize_amt = 1000 / width
        new_height = int(round(height * resize_amt))
        image = image.resize((new_height, 1000))
        image.save(os.getcwd() + "/" + image_dir + "/" + filename)

在我解决所有问题后,终端中出现此文本:

Oleksandrs-MacBook-Air: jaskier$ python resize.py --image_dir=/Images/

Traceback (most recent call last):

File "resize.py", line 16, in filename = os.fsdecode(f)

AttributeError: 'module' object has no attribute 'fsdecode'

Python 2.7(Mac OS 的原生版本)和 Python 3+(应使用终端安装或 https://www.python.org/download/releases/3.0/

航站楼:

pip3 install Pillow

这将有助于在 PIL 和 Python3 之间建立依赖关系...(此外检查文件夹中的 Python3:

/Library/Frameworks/Python.framework/Versions/3.6/bin/ The pip3 should be also there if you've done all correctly.

执行完这些步骤后,我出现了一些代码错误,但是 none 个模块错误。我希望它会对某人有所帮助。