Example #1
0
def thumbnail(file, size='104x104'):
    try:
        # defining the size
        x, y = [int(x) for x in size.split('x')]
        # defining the filename and the miniature filename
        filehead, filetail = os.path.split(file.path)
        basename, format = os.path.splitext(filetail)
        miniature = basename + '_' + size + '.png'
        filename = file.path
        miniature_filename = os.path.join(filehead, miniature)
        filehead, filetail = os.path.split(file.url)
        miniature_url = filehead + '/' + miniature
        if os.path.exists(miniature_filename) and os.path.getmtime(
                filename) > os.path.getmtime(miniature_filename):
            os.unlink(miniature_filename)
        # if the image wasn't already resized, resize it
        if not os.path.exists(miniature_filename):
            image = Image.open(filename)
            processor = SmartResize(width=x, height=y)
            image = processor.process(image)
            try:
                image.save(miniature_filename, 'png', quality=90, optimize=1)
            except Exception as e:
                print 'Error: %s' % e
                image.save(miniature_filename, 'png', quality=90)

        return miniature_url
    except Exception as e:
        print 'Error: %s' % e
        return ""
Example #2
0
def resize(uploaded_file: 'SimpleUploadedFile', **options):
    buffer = BytesIO()
    img = Image.open(uploaded_file)
    processor = SmartResize(**options)
    new_img = processor.process(img)
    new_img.save(buffer, format='png')
    return SimpleUploadedFile(name=utils.replace_extension(
        uploaded_file.name, 'png'),
                              content=buffer.getvalue(),
                              content_type=uploaded_file.content_type)
Example #3
0
def thumbnail(file, size='104x104'):
    try:

        # We will place the thumbnails in a directory alongside the image,
        # which will be named the same as the image except with "-thumbnail"
        # appended to it. Within that directory, there will be other images
        # named according to the requested size, for example "30x30.png" and
        # so forth. This way, the profile image names are entirely predictable
        # from the image names alone.
        thumb_dir = file.path + '-thumbnail'

        # Make sure that the thumbnail directory exists.
        if not os.path.isdir(thumb_dir):
            os.mkdir(thumb_dir)

        # Figure out the stand-alone filename ("30x30.png", for example) and
        # subsequently define the full local path name to the thumbnail and
        # its corresponding URL.
        thumb_filename = size + '.png'
        thumb_fullpath = os.path.join(thumb_dir, thumb_filename)
        thumb_url = file.url + '-thumbnail/' + thumb_filename

        # Check if the image is newer than the requested thumbnail, and if so,
        # remove the thumbnail so that it will be regenerated.
        if os.path.exists(thumb_fullpath) and os.path.getmtime(
                file.path) > os.path.getmtime(thumb_fullpath):
            os.unlink(thumb_fullpath)

        # Create the thumbnail if it does not already exist.
        if not os.path.exists(thumb_fullpath):
            width, height = [int(dimension) for dimension in size.split('x')]
            processor = SmartResize(width=width, height=height)
            image = processor.process(Image.open(file.path))
            image.save(thumb_fullpath, 'png', quality=90, optimize=True)

        return thumb_url

    except Exception as e:
        print 'Error: %s' % e
        return ""
def view_upload_test(request, bucket, token):
    BUCKET_UPlOAD_FILE = '{}/alpha/story/upload/file'.format(SERVER_BUCKET)

    instance = ImageTest.objects.first()
    if not instance:
        raise Http404

    image_name = os.path.basename(instance.image.name)
    image = Image.open(BytesIO(instance.image.read()))

    processor = SmartResize(256, 256)
    image_result = processor.process(image)

    image_data = BytesIO()
    image_result.save(image_data, format=image.format)

    image.close()

    m = MultipartEncoder(fields={
        'upload_type':
        'file',
        'files': (
            normalize(image_name),
            image_data.getvalue(),
            image_result.format,
        ),
    }, )

    response = requests.request(
        'post',
        BUCKET_UPlOAD_FILE,
        data=m,
        headers={
            'authorization': 'Bearer {}'.format(token),
            'Content-Type': m.content_type,
        },
    )

    r_filename = None
    r_description = None
    if response.status_code == requests.codes.ok:
        obj = response.json()
        r_filename = obj['data'][0]['filename']
        r_description = obj['data'][0]['description']

        # estructura del bucket privado de bigtincan
        # https://{servidor}/f/{bucket_id}/upload/{filename}
        # https://pubapi.bigtincan.com/f/{bucket_id}/upload/

        instance.image_url = '{}/f/{}/upload/{}'.format(
            SERVER_BUCKET,
            bucket,
            r_filename,
        )

        # eliminamos el archivo del disco
        instance.image.delete(save=True)

    response.close()
    image_result.close()

    if not instance.image.closed:
        instance.image.close()

    return JsonResponse({
        'token': token,
        'name': image_name,
        'filename': r_filename,
        'description': r_description,
        'file_url': instance.image_url,
    })