In [1]:
import os
import re
import subprocess
import tqdm

In [2]:
data_dir = os.path.expanduser('~/data/highres/')
files = os.listdir(data_dir)

In [3]:
image_re = re.compile('.*(\.jpe?g|\.png|\.tiff?|\.gif)$', re.I)
images = [f for f in files if image_re.match(f)]

In [12]:
for image in tqdm.tqdm_notebook(images):
    name, ext = os.path.splitext(image)
    p1 = subprocess.Popen([
            'convert', os.path.join(data_dir, image),
            '-thumbnail', '256x256^', 
            '-gravity', 'center', 
            '-extent', '256x256',
            os.path.join('thumbs', '256', name + '.thumb' + '.png')
        ])
    p1.wait()
    # create thumbnail of 256 width
    p2 = subprocess.Popen([
            'convert', os.path.join(data_dir, image),
            '-thumbnail', 'x256', 
            os.path.join('thumbs', '256w', name + '.thumb' + '.jpg')
        ])
    p2.wait()