Create a gist now

Instantly share code, notes, and snippets.

@martinmev /.xonshrc
Last active Jun 22, 2018

Embed
What would you like to do?
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