Last active
December 21, 2015 18:09
-
-
Save thisiswei/6345806 to your computer and use it in GitHub Desktop.
create thumbnails
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 re | |
import logging | |
import Image | |
from StringIO import StringIO | |
MAX_THUMBNAIL_WIDTH = 320. | |
THUMBNAIL_DIR = 'thumbnails' | |
IMG_REG_URL = re.compile(r'"(http://s7.jcrew.com/is/image/.*?)\"') | |
JCREW_URL = 'http://www.jcrew.com/womens_category/shoes.jsp?iNextCategory=-1' | |
def get_all_imgs(url): | |
img_urls = get_all_img_urls(url) | |
for i, e in enumerate(img_urls): | |
create_and_store_thumbnail(e, str(i)) | |
def get_all_img_urls(url): | |
resp = requests.get(url) | |
return IMG_REG_URL.findall(resp.content) | |
def create_and_store_thumbnail(url, name): | |
try: | |
r = requests.get(url) | |
img = Image.open(StringIO(r.content)) | |
original_width = img.size[0] | |
if original_width > MAX_THUMBNAIL_WIDTH: | |
original_height = img.size[1] | |
height = original_width / MAX_THUMBNAIL_WIDTH * original_height | |
img.thumbnail((MAX_THUMBNAIL_WIDTH, height), Image.ANTIALIAS) | |
if not os.path.exists(THUMBNAIL_DIR): | |
os.makedirs(THUMBNAIL_DIR) | |
img.save('./thumbnails/%s' % name + '.' + img.format) | |
except Exception: | |
logging.info("cannot create thumbnail for url: %s") % url | |
def main(): | |
get_all_imgs(JCREW_URL) | |
if __name__ == "__main__": | |
exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment