Search code examples
python-2.7terminalmacos-high-sierra

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


I have found a script in PYTHON. Need to run it to cut images' sizes. Why this error occurs and How to fix it? (Python 2.7 installed+ installed PIL for current script+ 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)

After I've fixed all issues this text occurs in Terminal:

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'


Solution

  • There is a Hugh difference between Python 2.7(native for Mac OS) and Python 3+(should install with terminal or https://www.python.org/download/releases/3.0/

    Terminal:

    pip3 install Pillow
    

    It will help to make dependencies between PIL and Python3... (moreover check Python3 in folder:

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

    After this steps now I have some code errors, but none of modules errors. I hope it will help someone.