예제 #1
0
def thumbnail(url):
    log.debug('- URL %s %s' % (type(url), url))
    hash = md5(url).hexdigest()
    path = os.path.join(settings.thumbnail_path, hash[:2], hash)
    if os.path.isfile(path) and os.stat(path) > 0:
        log.debug('%s: thumbnail exists' % path)
        return
    log.info('Making thumbnail %s %s' % (path, url))

    if cache_get('thumbnail:%s' % hash):
        return

    cache_store('thumbnail:%s' % hash, 1, 60)

    try:
        dirname = os.path.join(settings.thumbnail_path, hash[:2])
        try:
            os.mkdir(dirname)
        except OSError, e:
            if e.errno == 17:
                log.debug('OSError %s: %s %s' % (e.errno, e.strerror, dirname))
            else:
                log.warn('OSError %s: %s %s' % (e.errno, e.strerror, dirname))

        opener = urllib2.build_opener()
        opener.addheaders = [('User-agent', 'Mozilla/5.0')]
        resp = opener.open(url)
        #resp = urllib2.urlopen(url)
        buf = StringIO(resp.read())
        img = Image.open(buf)
        if img.size[0] * img.size[1] > settings.max_image_size:
            log.error('too big: %sx%s %s' % (img.size[0], img.size[1], url))
            return

        img.load()

        fmt = img.format

        if fmt == 'JPEG':
            img = _rotate(img)

        elif fmt == 'GIF':
            img.seek(0)
            #img = img.copy()

        img.thumbnail(settings.thumbnail_size, Image.ANTIALIAS)
        img.save(path, fmt, **img.info)
예제 #2
0
파일: worker.py 프로젝트: skobkin/point
def thumbnail(url):
    log.debug('- URL %s %s' % (type(url), url))
    hash = md5(url).hexdigest()
    dirname = os.path.join(settings.thumbnail_path, hash[:2])
    path = os.path.join(dirname, hash)
    if os.path.isfile(path) and os.stat(path) > 0:
        log.debug('%s: thumbnail exists' % path)
        return
    log.info('Making thumbnail %s %s' % (path, url))

    if cache_get('thumbnail:%s' % hash):
        return

    cache_store('thumbnail:%s' % hash, 1, 60)

    try:
        try:
            os.mkdir(dirname)
        except OSError, e:
            if e.errno == 17:
                log.debug('OSError %s: %s %s' % (e.errno, e.strerror, dirname))
            else:
                log.warn('OSError %s: %s %s' % (e.errno, e.strerror, dirname))

        opener = urllib2.build_opener()
        opener.addheaders = [('User-agent', 'Mozilla/5.0')]
        resp = opener.open(url)
        #resp = urllib2.urlopen(url)
        buf = StringIO(resp.read())
        img = Image.open(buf)
        if img.size[0] * img.size[1] > settings.max_image_size:
            log.error('too big: %sx%s %s' % (img.size[0], img.size[1], url))
            return

        img.load()

        fmt = img.format

        if fmt == 'JPEG':
            img = _rotate(img)

        elif fmt == 'GIF':
            img.seek(0)
            #img = img.copy()

        img.thumbnail(settings.thumbnail_size, Image.ANTIALIAS)
        img.save(path, fmt, **img.info)
예제 #3
0
def attach(path, dest, filename, remove=True):
    if isinstance(path, str):
        path = path.decode('utf-8')
    if isinstance(dest, str):
        dest = dest.decode('utf-8')
    if isinstance(filename, str):
        filename = filename.decode('utf-8')

    dirname = os.path.join(settings.media_path, dest)
    error = False
    try:
        os.makedirs(dirname)
    except OSError, e:
        if e.errno == 17:
            log.debug('OSError %s: %s %s' % (e.errno, e.strerror, dirname))
        else:
            log.warn('OSError %s: %s %s' % (e.errno, e.strerror, dirname))
            error = True
예제 #4
0
        if fmt == 'JPEG':
            img = _rotate(img)

        elif fmt == 'GIF':
            img.seek(0)
            #img = img.copy()

        img.thumbnail(settings.thumbnail_size, Image.ANTIALIAS)
        img.save(path, fmt, **img.info)

    #except IOError, e:
    #    log.error('IOError %s' % str(e))
    #    fd = open(path, 'w')
    #    fd.close()
    except urllib2.HTTPError, e:
        log.warn('HTTPError %s: %s' % (e.code, url))
        fd = open(path, 'w')
        fd.close()
    except urllib2.URLError, e:
        log.warn('URLError %s: %s' % (e.reason, url))
    except Exception, e:
        log.error(traceback.format_exc())
    finally:
        cache_del('thumbnail:%s' % hash)

def _attach_image(src, dest, filename):
    def thumb(name, size, orig=False):
        fd = open(src)
        img = Image.open(fd)
        img.load()