【发布时间】:2018-08-26 16:31:56
【问题描述】:
我在 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(最近一次调用最后一次):
文件“resize.py”,第 16 行,在 文件名 = os.fsdecode(f)
AttributeError: 'module' 对象没有属性 'fsdecode'
【问题讨论】:
-
说'os'模块没有'fsdecode'函数
-
这可能意味着您使用的 python 版本在 os 模块中没有 fsdecode ...
-
python --version Python 2.7.10 已经尝试安装3.6,但是系统PATH被2.7覆盖了-_-
-
嗯,试试 doin python3 —version
-
*已从 python.org/downloads 手动安装 3.6.4 结果:上次登录:3 月 24 日星期六 14:25:50 在 ttys000 Oleksandrs-MacBook-Air:~ jaskier$ python --version Python 2.7。 10
标签: python-2.7 terminal macos-high-sierra