I’m often using python to rename files for me or process them in batches when I need to save some time / immense boredom, and I do it so infrequently I generally have to look at reference material.
Here some code for future reference.
""" Python Rename Script ========= This is just some one off code I wrote to save myself from doing repetative tasks. It probably took as long as manually renaming them and cropping the files. The source files were in this format: - name fun.jpg - name work.jpg I needed them in the following format: - team_name.jpg - team_name2.jpg and at specific dimensions. I at first just wanted to rename, but found out some images were odd sizes. """ import re import os import shutil from PIL import Image, ImageOps dirname = os.path.abspath('.') file_paths = os.listdir(dirname) for _file in file_paths: try: matches = re.findall('(.*) (fun|work)\.jpg', _file) # find name & if fun or work. name = matches _type = matches prefix = 'team_' suffix = '' if _type == 'fun': suffix = '2' new_name = prefix + name + suffix + '.jpg' image = Image.open(_file) imagefit = ImageOps.fit(image, (150, 170), Image.ANTIALIAS, 0, (0.5, 0)) imagefit.save('tmp/'+ new_name, 'JPEG', quality=90) except Exception, e: print e, _file # I don't really care about when this fails.. # it would fail on any regex match failure like .DS_STORE (OSX file) # or my tmp directory