Last active
June 22, 2018 18:57
-
-
Save martinmev/5ea7be14d55e6c857cd66b3405e93bd5 to your computer and use it in GitHub Desktop.
Xonsh: "c" - creates pretty part of the URL, "resizeDir" - resize and auto orient images in given directory
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
import unicodedata | |
def cData_(x): | |
s = unicodedata.normalize('NFKD', x).encode('ascii', 'ignore').lower().decode('ascii') | |
newS = ''.join(map(lambda x: x if x.isalnum() else '-', s)) | |
oldS = None | |
while oldS != newS: | |
oldS = newS | |
newS = newS.replace('--', '-') | |
if newS: | |
if newS[-1] == '-': | |
newS = newS[:-1] | |
print(newS) | |
return None | |
aliases['c'] = lambda a,b: cData_(' '.join(a)) | |
import os | |
import glob | |
import PIL.Image # Pillow python library | |
def resizeDir(path = '', autoOrient = True): | |
if path: | |
path += '/' | |
for pattern in ('*.jpg', '*.JPG', '*.png', '*.PNG'): | |
for x in glob.glob(path + pattern): | |
if not autoOrient: | |
try: | |
if max(PIL.Image.open(x).size) <= 2048: | |
continue | |
except Exception as e: | |
print(x, e) | |
continue | |
rname = x + '_resized.jpg' | |
if autoOrient: | |
conv = !(convert @(x) -auto-orient -resize '2048x2048>' @(rname)) | |
else: | |
conv = !(convert @(x) -resize '2048x2048>' @(rname)) | |
if (conv.rtn == 0) and (autoOrient or not conv.errors): | |
os.rename(rname, x) | |
else: | |
print(x) | |
try: | |
os.remove(rname) | |
except Exception: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment