I have a directory that is structured as such:
├── directory
| ├── sub-directory_1
| | ├── img1.jpg
| | └── img2.jpg
| |
| ├── sub-directory_2
| | ├── img1.jpg
| | └── img2.jpg
. .
. .
. .
.
| └── sub-directory_n
| | ├── img1.jpg
| | └── img2.jpg
I have this code that resizes all images in a directory:
from PIL import Image
import os, sys
path = "/directory"
dirs = os.listdir( path )
def resize():
for item in dirs:
if os.path.isfile(path+item):
im = Image.open(path+item)
f, e = os.path.splitext(path+item)
imResize = im.resize((64,64), Image.ANTIALIAS)
imResize.save(f + 'r.jpg', 'JPEG', quality=90)
resize()
Is there a way that I can modify it so that it iteratively resizes all the images in the sub directories?
Does this fit the bill
from PIL import Image
import os, sys
dir_path = "/directory"
def resize_im(path):
if os.path.isfile(path):
im = Image.open(path).resize((64,64), Image.ANTIALIAS)
parent_dir = os.path.dirname(path)
img_name = os.path.basename(path).split('.')[0]
im.save(os.path.join(parent_dir, img_name + 'r.jpg'), 'JPEG', quality=90)
def resize_all(mydir):
for subdir , _ , fileList in os.walk(mydir):
for f in fileList:
try:
full_path = os.path.join(subdir,f)
resize_im(full_path)
except Exception as e:
print('Unable to resize %s. Skipping.' % full_path)
if __name__ == '__main__':
resize_all(dir_path)
Be careful when you save the resized image inside the same directory of the source image. If you run your code twice, it will create lots of extra resized images.
I have cobbled together answers from @Tu Bui and others on stack overflow to generate this, it also maintains aspect ration
from PIL import Image
import os, sys
dir_path = "/Users/tawanda/Downloads/images"
def resize_im(path):
if os.path.isfile(path):
parent_dir = os.path.dirname(path)
img_name = os.path.basename(path).split('.')[0]
img_extension = os.path.basename(path).split('.')[1]
if img_extension.upper() not in ['PNG', 'JPG', 'JPEG']:
print('invalid extension >>', img_extension)
return
img = Image.open(path);
width, height = img.size;
asp_rat = width/height;
# Enter new width (in pixels)
new_width = 900;
# Enter new height (in pixels)
new_height = 900;
new_asp_rat = new_width/new_height;
if (new_asp_rat == asp_rat):
img = img.resize((new_width, new_height), Image.ANTIALIAS);
# adjusts the height to match the width
# NOTE: if you want to adjust the width to the height, instead ->
# uncomment the second line (new_width) and comment the first one (new_height)
else:
new_height = round(new_width / asp_rat);
#new_width = round(new_height * asp_rat);
img = img.resize((new_width, new_height), Image.ANTIALIAS);
if img_extension.upper() == 'PNG':
print('png detected')
img.save(os.path.join(parent_dir, img_name + '_resized.png'), 'PNG', quality=90)
else:
img.save(os.path.join(parent_dir, img_name + '_resized.jpg'), 'JPEG', quality=90)
print(f'resized {img_name}.{img_extension}')
os.remove(path)
def resize_all(mydir):
for subdir , _ , fileList in os.walk(mydir):
for f in fileList:
try:
full_path = os.path.join(subdir,f)
resize_im(full_path)
except Exception as e:
print('Unable to resize %s. Skipping.' % full_path)
if __name__ == '__main__':
resize_all(dir_path)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.