Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pillow Image.save() [Errno 13] Permission denied

So I am trying to loop a bunch of images in a folder, do some changes and save them in a subdirectory and I am having permission denied issues.

from PIL import Image
import os

path = 'D:/my_path/'


dirs = os.listdir( path )

new_folder = 'out'

if not os.path.exists(path + new_folder):
    os.makedirs(path + new_folder)

def resize():
    num=0
    for item in dirs:

        #print(path + item)
        if os.path.isfile(path+item):
            im = Image.open(path+item).convert('RGB')

            imResize = im.resize((64, 64), Image.ANTIALIAS)
            im.Resize = os.rename(os.path.join(path, item),os.path.join(path, 'bad_' + str(num)) )
            imResize.save(path + new_folder, 'JPEG', quality=90)

            num+=1
        #if num > 1000:
            #break
resize()

PermissionError: [Errno 13] Permission denied: 'D:/my_path/out/'

Error triggers on this line imResize.save(path + new_folder, 'JPEG', quality=90) Any idea why?

like image 905
Inkplay_ Avatar asked Jul 16 '18 13:07

Inkplay_


People also ask

Can t open file Errno 13 permission denied?

How to Fix the IOError: [Errno 13] Permission denied in Python. To fix this, you need to enter the right path to the file you want to access, not the folder. Let's say we have two files in the Test_folder . After providing the path of the file, the error is resolved.

How to fix permission denied in Python?

Permission denied simply means the system is not having permission to write the file to that folder. Give permissions to the folder using "sudo chmod 777 " from terminal and try to run it.

How to resolve error 13 in Python?

The PermissionError: [errno 13] permission denied error occurs when you try to access a file from Python without having the necessary permissions. To fix this error, use the chmod or chown command to change the permissions of the file so that the right user and/or group can access the file.


2 Answers

imResize.save(path + new_folder, 'JPEG', quality=90) Doesn't look right to me. You have a directory named D:/my_path/out, and here you're trying to save the file to the name D:/my_path/out. That's already a directory, so you can't save a file with that same name.

Try choosing a name for your file that doesn't conflict with the name of an existing directory.

outputfilename = os.path.join(path, new_folder, "myoutputfile_{}.jpg".format(num))
imResize.save(outputfilename, 'JPEG', quality=90)
like image 69
Kevin Avatar answered Nov 03 '22 04:11

Kevin


Working code:

rename = 'bad_img_'

def resize():
    num=0
    for item in dirs:
        if os.path.isfile(path+item):
            im = Image.open(path+item).convert('RGB')
            imResize = im.resize((64, 64), Image.ANTIALIAS)
            imResize.save(os.path.join(save_dir, rename + str(num)) + '.JPG', 'JPEG', quality=90)
            num+=1

resize()
like image 35
Inkplay_ Avatar answered Nov 03 '22 03:11

Inkplay_