当前位置:首页 > 开发 > 编程语言 > Python > 正文

python 下载远程图片并生成缩略图

发表于: 2014-06-25   作者:cooler1217   来源:转载   浏览:
摘要: #coding=utf-8 from PIL import Image import os import os.path import StringIO import re import datetime import urllib def thumbnail(filename, size=(50, 50), output_filename=None): image
#coding=utf-8
from PIL import Image
import os
import os.path
import StringIO
import re
import datetime
import urllib
def thumbnail(filename, size=(50, 50), output_filename=None):
    image = Image.open(filename)
    if image.mode not in ('L', 'RGB'):
        image = image.convert('RGB')
    image = image.resize(size)
    # get the thumbnail data in memory.
    if not output_filename:
        output_filename = get_default_thumbnail_filename(filename)
    image.save(output_filename, image.format) 
    return output_filename
def thumbnail_string(buf, size=(50, 50)):
    f = StringIO.StringIO(buf)
    image = Image.open(f)
    if image.mode not in ('L', 'RGB'):
        image = image.convert('RGB')
    image = image.resize(size, Image.ANTIALIAS)
    o = StringIO.StringIO()
    image.save(o, "JPEG")
    return o.getvalue()
    
def get_default_thumbnail_filename(filename):
    path, ext = os.path.splitext(filename)
    return path + '.thumb.jpg'
def download_image(url):
    path = os.path.join('c:/', '',url.split('/')[-1])
    urllib.urlretrieve(url, path)
    return path
    
def get_image_url(content, user_id='1', directory='blog'):
    """
    if article's content has imgs,return first img's src
    if not has, give us null
    """
    
    pattern = 'src="?(.+?.)(gif|jpg|gif|jpg|bmp|png)'
    #pattern = '</?(IMG|img)[^<>]*/?>'
    
    lists = []
    lists = re.findall(pattern, content)
    thum_name = ''
    print lists
    for l in lists:
        try:
            original_path = '%s%s' % (l[0],l[1])
            
            #生成缩略图名称,路径
            thum_name = '%s.%s' % (datetime.datetime.now().strftime("%Y%m%d%H%M%S"), l[1])
            thum_dir = '%s/%s/small/' % (user_id, directory)
            thum_path = os.path.join('e:/', thum_dir + thum_name)
            print thum_path
            
            try:
                os.makedirs(os.path.join('e:/', thum_dir))
            except:
                pass
            
            if l[0].startswith('http://'):#处理远程图片
                
                #temp_path = os.path.join(global_settings.MEDIA_ROOT, original_path.split('/')[-1])
                temp_path = os.path.join('c:/', original_path.split('/')[-1])
                            
                #下载过程图片
                urllib.urlretrieve(original_path, temp_path)
               
                #生成缩略图
                thumbnail(temp_path, (60,60), thum_path)
                os.remove(temp_path)
                
            else:#处理本地上传图片
                original_dir = re.sub('/upload_media/', '', original_path) 
                print original_dir
                original_path = os.path.join('e:/', original_dir)
                print original_path
                
                #生成缩略图
                #thumbnail(original_path, (60,60), thum_path)
           
        except:
            pass
            
            
get_image_url("""<p><img id="lg" height="129" src="http://www.baidu.com/img/baidu_logo.gif" width="270" usemap= /><img height="0" width="0" alt="" src="/upload_media/13/blog/image/right.gif" /></p><br />""")

python 下载远程图片并生成缩略图

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号