Skip to content

Instantly share code, notes, and snippets.

@agalea91
Last active November 13, 2019 04:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save agalea91/80fa59d7ebc6491ae5a18941be2924eb to your computer and use it in GitHub Desktop.
Save agalea91/80fa59d7ebc6491ae5a18941be2924eb to your computer and use it in GitHub Desktop.
Resize images to given max px size
# -*- coding: utf-8 -*-
__author__ = 'Alex Galea'
"""
* No longer maintained *
See here for newer versions: https://github.com/agalea91/photo-batch-resize
"""
# Usage e.g.
# python photo-batch-resize-constrained.py --images-folder=lightroom-exports --max-px-size=1080
EXTENSIONS = [
'jpg', 'jpeg', 'png',
'JPG', 'JPEG', 'PNG',
]
import click
import glob
import os
from PIL import Image
@click.command()
@click.option(
'--max-px-size',
default=2560,
help='The max height / width of image in pixels.'
)
@click.option(
'--images-folder',
default='images',
help='Name of folder containing images.'
)
def main(max_px_size, images_folder):
imgs = []
for ext in EXTENSIONS:
imgs += sorted(glob.glob(os.path.join(images_folder, '*.{}'.format(ext))))
print('Image files to resize:')
print('\n'.join(imgs))
output_folder = '{}-{}px'.format(images_folder, max_px_size)
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for img_path in imgs:
try:
resize(img_path, max_px_size, output_folder)
except Exception as e:
with open('error.txt', 'w') as f:
f.write(str(e))
def resize(img_path, max_px_size, output_folder):
with Image.open(img_path) as img:
width_0, height_0 = img.size
out_f_name = os.path.split(img_path)[-1]
out_f_path = os.path.join(output_folder, out_f_name)
if max((width_0, height_0)) <= max_px_size:
print('writing {} to disk (no change from original)'.format(out_f_path))
img.save(out_f_path)
return
if width_0 > height_0:
wpercent = max_px_size / float(width_0)
hsize = int(float(height_0) * float(wpercent))
img = img.resize((max_px_size, hsize), Image.ANTIALIAS)
print('writing {} to disk'.format(out_f_path))
img.save(out_f_path)
return
if width_0 < height_0:
hpercent = max_px_size / float(height_0)
wsize = int(float(width_0) * float(hpercent))
img = img.resize((wsize, max_px_size), Image.ANTIALIAS)
print('writing {} to disk'.format(out_f_path))
img.save(out_f_path)
return
if __name__ == '__main__':
main()
@duhithas
Copy link

duhithas commented Sep 28, 2018

import cv2
import glob
import os

# Get images
imgs = glob.glob('FG/*.jpg')
imgs.extend(glob.glob('FG/*.jpg'))

print('Found files:')
print(imgs)

width = 84
print('Resizing all images be %d pixels wide' % width)

folder = 'resized'
if not os.path.exists(folder):
    os.makedirs(folder)

# Iterate through resizing and saving
for img in imgs:
    pic = cv2.imread(img, cv2.IMREAD_UNCHANGED)
    height = int(width * pic.shape[0] / pic.shape[1])
    pic = cv2.resize(pic, (width, height))
    cv2.imwrite(folder + '/' + img, pic)

creates a folder named resized ..but unable to imwrite 

@Artefact2
Copy link

Alternate version using GNU parallel and ImageMagick

parallel convert {} -resize 2560x2560 outpath/{/.}.jpg ::: inpath/**/*.jpg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment