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?
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.
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.
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.
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)
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With