コード例 #1
0
ファイル: manage.py プロジェクト: shawnjots/eventcity
def fix_speaker_images(event):
    from app.helpers.sessions_speakers.speakers import speaker_image_sizes
    from app.helpers.sessions_speakers.speakers import save_resized_photo
    import urllib
    from app.helpers.storage import generate_hash
    event_id = int(event)
    image_sizes = speaker_image_sizes()
    speakers = Speaker.query.filter_by(event_id=event_id).all()
    for speaker in speakers:
        if speaker.photo and speaker.photo.strip() != '':
            file_relative_path = 'static/media/temp/' + generate_hash(
                str(speaker.id)) + '.jpg'
            file_path = app.config['BASE_DIR'] + '/' + file_relative_path
            urllib.urlretrieve(speaker.photo, file_path)
            speaker.small = save_resized_photo(file_path, event_id, speaker.id,
                                               'small', image_sizes)
            speaker.thumbnail = save_resized_photo(file_path, event_id,
                                                   speaker.id, 'thumbnail',
                                                   image_sizes)
            speaker.icon = save_resized_photo(file_path, event_id, speaker.id,
                                              'icon', image_sizes)
            db.session.add(speaker)
            os.remove(file_path)
            print("Downloaded " + speaker.photo + " into " +
                  file_relative_path)
        print("Processed - " + str(speaker.id))
    db.session.commit()
コード例 #2
0
def save_resized_image(image_file,
                       basewidth,
                       aspect,
                       height_size,
                       upload_path,
                       ext='jpg',
                       remove_after_upload=False):
    """
    Save the resized version of the background image
    :param upload_path:
    :param ext:
    :param remove_after_upload:
    :param height_size:
    :param aspect:
    :param basewidth:
    :param image_file:
    :return:
    """
    filename = '{filename}.{ext}'.format(filename=get_image_file_name(),
                                         ext=ext)

    img = Image.open(image_file)
    if aspect == 'on':
        width_percent = (basewidth / float(img.size[0]))
        height_size = int((float(img.size[1]) * float(width_percent)))

    img = img.resize((basewidth, height_size), PIL.Image.ANTIALIAS)

    temp_file_relative_path = 'static/media/temp/' + generate_hash(
        str(image_file)) + get_image_file_name() + '.jpg'
    temp_file_path = app.config['BASE_DIR'] + '/' + temp_file_relative_path

    img.save(temp_file_path)

    file = UploadedFile(file_path=temp_file_path, filename=filename)

    if remove_after_upload:
        os.remove(image_file)

    uploaded_url = upload(file, upload_path)

    os.remove(temp_file_path)

    return uploaded_url
コード例 #3
0
def fix_speaker_images(event):
    from app.helpers.sessions_speakers.speakers import speaker_image_sizes
    from app.helpers.sessions_speakers.speakers import save_resized_photo
    import urllib
    from app.helpers.storage import generate_hash
    event_id = int(event)
    image_sizes = speaker_image_sizes()
    speakers = Speaker.query.filter_by(event_id=event_id).all()
    for speaker in speakers:
        if speaker.photo and speaker.photo.strip() != '':
            file_relative_path = 'static/media/temp/' + generate_hash(str(speaker.id)) + '.jpg'
            file_path = app.config['BASE_DIR'] + '/' + file_relative_path
            urllib.urlretrieve(speaker.photo, file_path)
            speaker.small = save_resized_photo(file_path, event_id, speaker.id, 'small', image_sizes)
            speaker.thumbnail = save_resized_photo(file_path, event_id, speaker.id, 'thumbnail', image_sizes)
            speaker.icon = save_resized_photo(file_path, event_id, speaker.id, 'icon', image_sizes)
            db.session.add(speaker)
            os.remove(file_path)
            print("Downloaded " + speaker.photo + " into " + file_relative_path)
        print("Processed - " + str(speaker.id))
    db.session.commit()
コード例 #4
0
def save_resized_image(image_file, basewidth, aspect, height_size, upload_path,
                       ext='jpg', remove_after_upload=False):
    """
    Save the resized version of the background image
    :param upload_path:
    :param ext:
    :param remove_after_upload:
    :param height_size:
    :param aspect:
    :param basewidth:
    :param image_file:
    :return:
    """
    filename = '{filename}.{ext}'.format(filename=get_image_file_name(), ext=ext)

    img = Image.open(image_file)
    if aspect == 'on':
        width_percent = (basewidth / float(img.size[0]))
        height_size = int((float(img.size[1]) * float(width_percent)))

    img = img.resize((basewidth, height_size), PIL.Image.ANTIALIAS)

    temp_file_relative_path = 'static/media/temp/' + generate_hash(str(image_file)) + get_image_file_name() + '.jpg'
    temp_file_path = app.config['BASE_DIR'] + '/' + temp_file_relative_path

    img.save(temp_file_path)

    file = UploadedFile(file_path=temp_file_path, filename=filename)

    if remove_after_upload:
        os.remove(image_file)

    uploaded_url = upload(file, upload_path)

    os.remove(temp_file_path)

    return uploaded_url