IPython: Rename images with their SHA1 chekcsum

I got lazy, I don’t want to rename pictures anymore so I name them with their own sha1 checksum, so I made a really quick script to do that under IPython.

def grapimages():
"""return a list of all images in the current folder """
μ = list()
tmp = !ls | grep '[PpJjGg][NnPpIi][GgEeFf][Gg]\{0,1\}'

for ii in tmp:
if ii.lower().endswith('png') or ii.lower().endswith('jpg') or ii.lower().endswith('jpeg') or ii.lower().endswith('gif'): μ.append(ii)

return μ

def rensha():
"""rename images from the current folder with their own sha1 checksum""" χt = ''
iu = ''

for ii in grapimages():

with open(ii, 'rb', ) as φχer:
χt = sha1(φχer.read()).hexdigest()

if ii.lower().endswith('jpeg'): # + exception for jpegs iu = '{}.jpg'.format(χt)
else:
iu = '{}.{}'.format(χt, ii.split('.')[-1])

! mv $ii $iu
print('done.')

Yes I use IPython’s ability to use shell commands to populate python variables. But I told you it’s quick shit, or “life hack” like those pseudo geeks loves to call them. ;)

Advertisements