Skip to content

Instantly share code, notes, and snippets.

@martinmev
Last active June 22, 2018 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinmev/5ea7be14d55e6c857cd66b3405e93bd5 to your computer and use it in GitHub Desktop.
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
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