re_file = re.compile(r'(?i)(.*)(\.jpg|\.gif|\.png|\.bmp)$') thumb = '-thumb'
for fn in os.listdir('.'): m = re_file.match(fn) if m and os.path.isfile(fn) and not m.group(1).endswith(thumb): r = Image.open(fn) r.thumbnail((200,200), Image.ANTIALIAS) fn_new = m.group(1) + thumb + m.group(2) print fn, '->', fn_new r.save(fn_new)