Пример #1
0
    def _get_raw_data(self, image, format_, quality, image_info=None, progressive=False):
        # Increase (but never decrease) PIL buffer size
        ImageFile.MAXBLOCK = max(ImageFile.MAXBLOCK, image.size[0] * image.size[1])
        bf = BufferIO()

        params = {
            'format': format_,
            'quality': quality,
            'optimize': 1,
        }

        # keeps icc_profile
        if 'icc_profile' in image_info:
            params['icc_profile'] = image_info['icc_profile']

        raw_data = None

        if format_ == 'JPEG' and progressive:
            params['progressive'] = True
        try:
            # Do not save unnecessary exif data for smaller thumbnail size
            params.pop('exif', {})
            image.save(bf, **params)
        except (IOError, OSError):
            # Try without optimization.
            params.pop('optimize')
            image.save(bf, **params)
        else:
            raw_data = bf.getvalue()
        finally:
            bf.close()

        return raw_data
Пример #2
0
    def _get_raw_data(self, image, format_, quality, image_info=None, progressive=False):
        # Increase (but never decrease) PIL buffer size
        ImageFile.MAXBLOCK = max(ImageFile.MAXBLOCK, max(image.size)**2)
        bf = BufferIO()

        params = {
            'format': format_,
            'quality': quality,
            'optimize': 1,
        }

        # keeps icc_profile
        if 'icc_profile' in image_info:
            params['icc_profile'] = image_info['icc_profile']

        raw_data = None

        if format_ == 'JPEG' and progressive:
            params['progressive'] = True
        try:
            # Do not save unnecessary exif data for smaller thumbnail size
            params.pop('exif', {})
            image.save(bf, **params)
        except (IOError, OSError):
            # Try without optimization.
            params.pop('optimize')
            image.save(bf, **params)
        else:
            raw_data = bf.getvalue()
        finally:
            bf.close()

        return raw_data
Пример #3
0
    def _get_raw_data(self, image, format_, quality, image_info=None, progressive=False):
        # Increase (but never decrease) PIL buffer size
        ImageFile.MAXBLOCK = max(ImageFile.MAXBLOCK, image.size[0] * image.size[1])
        bf = BufferIO()

        params = {"format": format_, "quality": quality, "optimize": 1}

        # keeps icc_profile
        if "icc_profile" in image_info:
            params["icc_profile"] = image_info["icc_profile"]

        raw_data = None

        if format_ == "JPEG" and progressive:
            params["progressive"] = True
        try:
            # Do not save unnecessary exif data for smaller thumbnail size
            params.pop("exif", {})
            image.save(bf, **params)
        except (IOError, OSError):
            # Try without optimization.
            params.pop("optimize")
            image.save(bf, **params)
        else:
            raw_data = bf.getvalue()
        finally:
            bf.close()

        return raw_data
Пример #4
0
    def _get_raw_data(self, image, format_, quality, image_info=None, progressive=False):
        ImageFile.MAXBLOCK = image.size[0] * image.size[1]
        bf = BufferIO()

        params = {
            'format': format_,
            'quality': quality,
            'optimize': 1,
        }

        params.update(image_info)

        if format_ == 'JPEG' and progressive:
            params['progressive'] = True
        try:
            image.save(bf, **params)
        except IOError:
            maxblock = ImageFile.MAXBLOCK

            try:
                # Temporary encrease ImageFile MAXBLOCK
                ImageFile.MAXBLOCK = image.size[0] * image.size[1]
                image.save(bf, **params)
            except IOError:
                params.pop('optimize')
                image.save(bf, **params)
            finally:
                ImageFile.MAXBLOCK = maxblock

        raw_data = bf.getvalue()
        bf.close()

        return raw_data
Пример #5
0
    def _get_raw_data(self, image, format_, quality, progressive=False):
        ImageFile.MAXBLOCK = image.size[0] * image.size[1]
        buffer = BufferIO()

        params = {
            'format': format_,
            'quality': quality,
            'optimize': 1,
        }

        if format_ == 'JPEG' and progressive:
            params['progressive'] = True
        try:
            image.save(buffer, **params)
        except IOError:
            maxblock = ImageFile.MAXBLOCK

            try:
                # Temporary encrease ImageFile MAXBLOCK
                ImageFile.MAXBLOCK = image.size[0] * image.size[1]
                image.save(buffer, **params)
            except IOError:
                params.pop('optimize')
                image.save(buffer, **params)
            finally:
                ImageFile.MAXBLOCK = maxblock

        raw_data = buffer.getvalue()
        buffer.close()

        return raw_data
Пример #6
0
    def _get_raw_data(self, image, format_, quality, image_info=None, progressive=False):
        # Increase (but never decrease) PIL buffer size
        ImageFile.MAXBLOCK = max(ImageFile.MAXBLOCK, image.size[0] * image.size[1])
        bf = BufferIO()

        params = {
            'format': format_,
            'quality': quality,
            'optimize': 1,
        }

        params.update(image_info)

        raw_data = None

        if format_ == 'JPEG' and progressive:
            params['progressive'] = True
        try:
            image.save(bf, **params)
        except (IOError, OSError):
            # Try without optimization.
            params.pop('optimize')
            image.save(bf, **params)
        else:
            raw_data = bf.getvalue()
        finally:
            bf.close()

        return raw_data
Пример #7
0
    def _get_raw_data(self,
                      image,
                      format_,
                      quality,
                      image_info=None,
                      progressive=False):
        # Increase (but never decrease) PIL buffer size
        ImageFile.MAXBLOCK = max(ImageFile.MAXBLOCK,
                                 image.size[0] * image.size[1])
        bf = BufferIO()

        params = {
            'format': format_,
            'quality': quality,
            'optimize': 1,
        }

        params.update(image_info)

        raw_data = None

        if format_ == 'JPEG' and progressive:
            params['progressive'] = True
        try:
            image.save(bf, **params)
        except (IOError, OSError):
            # Try without optimization.
            params.pop('optimize')
            image.save(bf, **params)
        else:
            raw_data = bf.getvalue()
        finally:
            bf.close()

        return raw_data