Пример #1
0
def pil_image(source, exif_orientation=True, **options):
    """
    Try to open the source file directly using PIL, ignoring any errors.

    exif_orientation

        If EXIF orientation data is present, perform any required reorientation
        before passing the data along the processing pipeline.

    """
    # Use a StringIO wrapper because if the source is an incomplete file like
    # object, PIL may have problems with it. For example, some image types
    # require tell and seek methods that are not present on all storage
    # File objects.
    if not source:
        return
    source = StringIO(source.read())
    try:
        image = Image.open(source)
        # Fully load the image now to catch any problems with the image
        # contents.
        image.load()
    except Exception:
        return

    if exif_orientation:
        image = utils.exif_orientation(image)
    return image
Пример #2
0
def pil_image(source, exif_orientation=True, **options):
    """
    Try to open the source file directly using PIL, ignoring any errors.

    exif_orientation

        If EXIF orientation data is present, perform any required reorientation
        before passing the data along the processing pipeline.

    """
    # Use a StringIO wrapper because if the source is an incomplete file like
    # object, PIL may have problems with it. For example, some image types
    # require tell and seek methods that are not present on all storage
    # File objects.
    if not source:
        return
    source = StringIO(source.read())
    try:
        image = Image.open(source)
        # Fully load the image now to catch any problems with the image
        # contents.
        image.load()
    except Exception:
        log.exception('Caught exception in thumbnail_url')
        return

    if exif_orientation:
        image = utils.exif_orientation(image)
    return image
Пример #3
0
def pil_image(source, exif_orientation=True, **options):
    """
    Try to open the source file directly using PIL, ignoring any errors.

    exif_orientation

        If EXIF orientation data is present, perform any required reorientation
        before passing the data along the processing pipeline.

    """
    # Use a BytesIO wrapper because if the source is an incomplete file like
    # object, PIL may have problems with it. For example, some image types
    # require tell and seek methods that are not present on all storage
    # File objects.
    if not source:
        return
    source = BytesIO(source.read())

    image = Image.open(source)
    # Fully load the image now to catch any problems with the image contents.
    try:
        # An "Image file truncated" exception can occur for some images that
        # are still mostly valid -- we'll swallow the exception.
        image.load()
    except IOError:
        pass
    # Try a second time to catch any other potential exceptions.
    image.load()

    if exif_orientation:
        image = utils.exif_orientation(image)
    return image
Пример #4
0
def ffmpeg_frame(source, frame='00:00:01', **options):

    if not source:
        return
    # Generates random filename for data stream and writes stream to temp-file
    filename = ''.join(choices(ascii_lowercase + ascii_uppercase, k=8))
    with open(filename, 'wb') as f:
        f.write(source.read())

    try:
        # Opens file with ffmpeg, grabs frame at 1 second & removes tempfile
        cmd = [
            'ffmpeg', '-i', filename, '-ss', frame, '-vframes', '1', '-f',
            'image2', 'pipe:1'
        ]
        data = Popen(cmd, stdin=PIPE, stdout=PIPE,
                     stderr=PIPE).communicate(source.read())
        os.remove(filename)
        if not data[0]:
            return
    except (Exception, IndexError, FileNotFoundError):
        return

    image = Image.open(BytesIO(data[0]))
    try:
        image.load()
    except IOError:
        pass

    image = utils.exif_orientation(image)
    return image
Пример #5
0
    def render(self, img):

        img = exif_orientation(img)

        img = colorspace(img)
        # img = autocrop(img)

        if self.poi[0] and self.poi[1]:
            source_x, source_y = [float(v) for v in img.size]
            target = [self.poi[0] / source_x, self.poi[1] / source_y]

            for i in range(2):
                if target[i] > 1.:
                    target[i] = 100.
                else:
                    target[i] *= 100

        else:
            target = None

        img = scale_and_crop(img,
                             self.size,
                             crop=self.crop,
                             upscale=self.upscale,
                             zoom=self.zoom,
                             target=target)

        # img = filters(img)
        # img = background(img)

        return img
Пример #6
0
def _get_image_or_404(identifier, load_image=False):
    """
    Return image matching `identifier`.

    The `identifier` is expected to be a raw image ID for now, but may be
    more complex later.
    """
    ik_image = get_object_or_404(ICEkitImage, id=identifier)
    if not load_image:
        return ik_image, None

    ####################################################################
    # Image-loading incantation cribbed from easythumbnail's `pil_image`
    image = Image.open(BytesIO(ik_image.image.read()))
    # Fully load the image now to catch any problems with the image contents.
    try:
        # An "Image file truncated" exception can occur for some images that
        # are still mostly valid -- we'll swallow the exception.
        image.load()
    except IOError:
        pass
    # Try a second time to catch any other potential exceptions.
    image.load()
    if True:  # Support EXIF orientation data
        image = et_utils.exif_orientation(image)
    ####################################################################

    return ik_image, image
Пример #7
0
def pil_image(source, **options):
    """
    Try to open the source file directly using PIL, ignoring any errors.
    """
    # Use a StringIO wrapper because if the source is an incomplete file like
    # object, PIL may have problems with it. For example, some image types
    # require tell and seek methods that are not present on all storage
    # File objects.
    source = StringIO(source.read())
    try:
        image = Image.open(source)
    except Exception:
        return
    # If EXIF orientation data is present, perform any required reorientation
    # before passing the data along the processing pipeline.
    image = utils.exif_orientation(image)
    return image