Last active
December 16, 2015 16:29
-
-
Save diffused/5463747 to your computer and use it in GitHub Desktop.
Perform distributed image processing, such as resizing, using an IPython cluster
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Start a basic, 4 node, local, ipython cluster, using: | |
# ipcluster start -n 4 | |
# | |
# then run: | |
# ipython parallel_process_images.py | |
# | |
# this example reads in ./test_images/*.jpg | |
# and outputs a bunch of .png files to ./resized_test_images/ | |
from IPython.parallel import require | |
from IPython.parallel import Client | |
def run(): | |
files = [f for f in glob.glob('./test_images/*.jpg') if 'palette' not in f] | |
client = Client() | |
view = client.load_balanced_view() | |
async_results = [] | |
for f in files: | |
ar = view.apply_async( | |
process_image, | |
f, | |
save_to_dir="./resized_test_images" | |
) | |
async_results.append(ar) | |
print("Submitted tasks: ", len(async_results)) | |
client.wait(async_results) | |
for ar in async_results: | |
print ar.display_outputs() | |
def process_image(file, save_to_dir): | |
import png_image_set # requires png_image_set.py | |
print "processing %s" % file | |
p = png_image_set.PngImageSet() | |
p.make_png_imageset(file, save_to_dir=save_to_dir) | |
if __name__ == '__main__': | |
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import glob | |
from PIL import Image, ImageOps, ImageChops | |
class PngImageSet(object): | |
''' | |
Resizes images by re-fitting them into a given size, | |
pads with white background and maintains aspect ratio. | |
If no save_to_dir is specified, then dir of original | |
image is used. | |
Adds width to filename. | |
eg: | |
somepicture.jpg => somepicture.160.png | |
''' | |
def make_png_imageset( | |
self, | |
file, | |
save_to_dir=None, | |
sizes=[(80,80), (160, 160), (240,240), (320,320), (640,640)], | |
quality=85 | |
): | |
file = os.path.abspath(file) | |
file_dir = os.path.dirname(file) | |
file_noext, file_ext = os.path.splitext(file) | |
file_noext = os.path.basename(file_noext) | |
if save_to_dir is not None: | |
save_to_dir = os.path.abspath(save_to_dir) | |
else: | |
save_to_dir = file_dir | |
image = Image.open(file) | |
for size in sizes: | |
new_image_file = os.path.join(save_to_dir, file_noext + '.' + str(size[0]) + '.png') | |
new_image = image.copy() | |
new_image.thumbnail(size, Image.ANTIALIAS) | |
offset_x = max( (size[0] - new_image.size[0]) / 2, 0 ) | |
offset_y = max( (size[1] - new_image.size[1]) / 2, 0 ) | |
thumb = Image.new(mode='RGBA',size=size, color=(255,255,255,255)) | |
thumb.paste(new_image, (offset_x, offset_y)) | |
thumb.save(new_image_file, 'PNG', quality=quality) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment