Created
April 3, 2014 13:35
-
-
Save twmht/9954468 to your computer and use it in GitHub Desktop.
Multiprocessing
This file contains hidden or 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 urllib2 | |
from multiprocessing.dummy import Pool as ThreadPool | |
urls = [ | |
'http://www.python.org', | |
'http://www.python.org/about/', | |
'http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html', | |
'http://www.python.org/doc/', | |
'http://www.python.org/download/', | |
'http://www.python.org/getit/', | |
'http://www.python.org/community/', | |
'https://wiki.python.org/moin/', | |
'http://planet.python.org/', | |
'https://wiki.python.org/moin/LocalUserGroups', | |
'http://www.python.org/psf/', | |
'http://docs.python.org/devguide/', | |
'http://www.python.org/community/awards/' | |
# etc.. | |
] | |
# Make the Pool of workers | |
pool = ThreadPool(4) | |
# Open the urls in their own threads | |
# and return the results | |
results = pool.map(urllib2.urlopen, urls) | |
#close the pool and wait for the work to finish | |
pool.close() | |
pool.join() |
This file contains hidden or 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 PIL | |
from multiprocessing import Pool | |
from PIL import Image | |
SIZE = (75,75) | |
SAVE_DIRECTORY = 'thumbs' | |
def get_image_paths(folder): | |
return (os.path.join(folder, f) | |
for f in os.listdir(folder) | |
if 'jpeg' in f) | |
def create_thumbnail(filename): | |
im = Image.open(filename) | |
im.thumbnail(SIZE, Image.ANTIALIAS) | |
base, fname = os.path.split(filename) | |
save_path = os.path.join(base, SAVE_DIRECTORY, fname) | |
im.save(save_path) | |
if __name__ == '__main__': | |
folder = os.path.abspath( | |
'11_18_2013_R000_IQM_Big_Sur_Mon__e10d1958e7b766c3e840') | |
os.mkdir(os.path.join(folder, SAVE_DIRECTORY)) | |
images = get_image_paths(folder) | |
pool = Pool() | |
pool.map(create_thumbnail,images) | |
pool.close() | |
pool.join() |
This file contains hidden or 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 PIL | |
from multiprocessing import Pool | |
from PIL import Image | |
SIZE = (75,75) | |
SAVE_DIRECTORY = 'thumbs' | |
def get_image_paths(folder): | |
return (os.path.join(folder, f) | |
for f in os.listdir(folder) | |
if 'jpeg' in f) | |
def create_thumbnail(filename): | |
im = Image.open(filename) | |
im.thumbnail(SIZE, Image.ANTIALIAS) | |
base, fname = os.path.split(filename) | |
save_path = os.path.join(base, SAVE_DIRECTORY, fname) | |
im.save(save_path) | |
if __name__ == '__main__': | |
folder = os.path.abspath( | |
'11_18_2013_R000_IQM_Big_Sur_Mon__e10d1958e7b766c3e840') | |
os.mkdir(os.path.join(folder, SAVE_DIRECTORY)) | |
images = get_image_paths(folder) | |
for image in images: | |
create_thumbnail(Image) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment