Created
May 28, 2020 16:49
-
-
Save isaaclw/b459d73a207e8cb70ae11d73d80d78a8 to your computer and use it in GitHub Desktop.
Bulk Resize Pictures #media #pictures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
""" | |
Update the LONG_SIZE and SHORT_SIZE | |
then provide a folder to scan (scan_folder) | |
and a folder to write new files (output_folder) | |
""" | |
import glob | |
import os | |
import subprocess | |
from PIL import Image | |
LONG_SIZE = int(1024 * 1.5) | |
SHORT_SIZE = int(768 * 1.5) | |
CONVERT = '/usr/bin/convert' | |
def resize_photo(photoname, outfile, long_size=LONG_SIZE, short_size=SHORT_SIZE, | |
profile=None): | |
""" Resize the photo | |
- photoname: file to convert | |
- outfile: file to save to | |
- long_size: the longer size, see profile: lanscape, portrait | |
- short_size: the shorter size, see profile: lanscape, portrait | |
- profile: landscape or portrait, None means detect | |
""" | |
if not os.path.exists(os.path.dirname(outfile)): | |
try: | |
os.makedirs(os.path.dirname(outfile)) | |
except OSError as exc: # Guard against race condition | |
if exc.errno != errno.EEXIST: | |
raise | |
im = Image.open(photoname) | |
if profile is None: | |
if im.size[1] > im.size[0]: | |
profile = 'portrait' | |
else: | |
profile = 'landscape' # this can be default | |
if profile == 'landscape': | |
if im.size[0] <= long_size: | |
print("correct size", photoname) | |
return False | |
subprocess.call([CONVERT, photoname, '-resize', '%sx%s' % (long_size, | |
short_size), outfile]) | |
return True | |
elif profile == 'portrait': | |
if im.size[1] <= long_size: | |
print("correct size", photoname) | |
return False | |
subprocess.call([CONVERT, photoname, '-resize', '%sx%s' % (short_size, | |
long_size), outfile]) | |
return True | |
def resize_folder(infolder, outfolder): | |
for file in glob.glob('%s/**' % infolder, recursive=True): | |
try: | |
Image.open(file) | |
except OSError: | |
print("skipping non-image", file) | |
except IsADirectoryError: | |
print("skipping directory", file) | |
except: | |
print("other error", file) | |
else: | |
infile = file | |
outfile = file.replace(infolder, outfolder) | |
resize_photo(infile, outfile) | |
if __name__ == '__main__': | |
from optparse import OptionParser | |
parser = OptionParser() | |
parser.add_option('-s', '--scan', | |
type='string', | |
dest='scan_folder', | |
help='The folder to scan and resize') | |
parser.add_option('-o', '--output', | |
type='string', | |
dest='output_folder', | |
help='The folder to store the new files') | |
options, args = parser.parse_args() | |
if not options.scan_folder or not options.output_folder: | |
raise Exception("Use '--scan' and '--output'. Use '--help' for info") | |
if not os.path.isdir(options.scan_folder): | |
raise Exception("Needs a valid path scan") | |
if not os.path.isdir(options.output_folder): | |
os.makedirs(options.output_folder) | |
resize_folder(options.scan_folder, options.output_folder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment