コード例 #1
0
ファイル: forms.py プロジェクト: philpill/romeo
    def _process_image_field(self, field, imagetype, thumbnails):
        filepath = lambda name, label: self.Meta.model.get_image_filepath(
            self.account_id, name, imagetype, label)

        if isinstance(field.data, basestring):
            field.data = field.data.rsplit('/')[-1]
            original_path = filepath(field.data, 'original')
            if not media_bucket.get_key(original_path):
                raise wtforms.ValidationError('%s not found.' % original_path)
        else:
            try:
                image = Image.open(field.data)
                content_type = Image.MIME[image.format]
            except Exception as e:
                raise wtforms.ValidationError(e.message)

            stream = field.data.stream
            stream.seek(0)

            field.data = get_random_filename()
            upload_file(media_bucket, filepath(field.data, 'original'),
                        stream, content_type)

            if thumbnails is True:
                thumbnails = '%s_THUMBNAIL_SIZES' % imagetype.upper()
            if isinstance(thumbnails, basestring):
                thumbnails = current_app.config[thumbnails]
            for size, buf in resize(image, thumbnails, save_to_buffer='jpeg'):
                upload_file(media_bucket, filepath(field.data, str(size[0])),
                            buf, 'image/jpeg')

        return field
コード例 #2
0
ファイル: forms.py プロジェクト: philpill/romeo
def create_cover_thumbnails(video_id, cover_filename=None):
    def _thumbnail(label, size):
        return VideoThumbnail.from_cover_image(cover_filename, video.account_id, label, size)

    video = Video.query.get(video_id)
    assert video.external_id
    cover_filepath = urlparse(video.thumbnails[0].url).path
    if cover_filename != cover_filepath.rsplit('/', 1)[-1]:
        current_app.logger.warning('Unexpected cover image: %d: %s: %s',
                                   video_id, cover_filename, cover_filepath)
        return

    imgdata = StringIO(download_file(media_bucket, cover_filepath))
    image = Image.open(imgdata)
    video.thumbnails = [_thumbnail(None, image.size)]

    sizes = [size for size in current_app.config['COVER_THUMBNAIL_SIZES']
             if size < image.size]
    for size, buf in resize(image, sizes, save_to_buffer='jpeg'):
        thumbnail = _thumbnail(str(size[0]), size)
        upload_file(media_bucket, urlparse(thumbnail.url).path, buf, 'image/jpeg')
        video.thumbnails.append(thumbnail)
        imgdata = buf

    try:
        ooyala_request('assets', video.external_id, 'preview_image_files',
                       method='post', data=imgdata.getvalue())
    except Exception:
        current_app.logger.exception('Unable to set preview image for %d (%s)',
                                     video.id, video.external_id)