コード例 #1
0
ファイル: BLPImagePlugin.py プロジェクト: yuanf225/Coffee
    def __decode_blp1(self):
        header = StringIO(self.fp.read(28 + 16 * 4 + 16 * 4))

        magic, compression = unpack("<4si", header.read(8))
        encoding, alphaDepth, alphaEncoding, hasMips = unpack(
            "<4b", header.read(4))
        self.size = unpack("<II", header.read(8))
        encoding, subtype = unpack("<ii", header.read(8))
        offsets = unpack("<16I", header.read(16 * 4))
        lengths = unpack("<16I", header.read(16 * 4))

        if compression == 0:
            from PIL.JpegImagePlugin import JpegImageFile
            jpegHeaderSize, = unpack("<I", self.fp.read(4))
            jpegHeader = self.fp.read(jpegHeaderSize)
            extraData = self.fp.read(offsets[0] -
                                     self.fp.tell())  # What IS this?
            data = self.fp.read(lengths[0])
            data = jpegHeader + data
            data = StringIO(data)
            image = JpegImageFile(data)
            image.show()
            raw_input()
            self.tile = image.tile  # PIL is terrible
            self.fp = image.fp
            self.mode = image.mode
            return

        if compression == 1:
            if encoding in (3, 4):
                raise NotImplementedError

            elif encoding == 5:
                data = []
                palette_data = self.fp.read(256 * 4)
                palette = getpalette(palette_data)
                _data = StringIO(self.fp.read(lengths[0]))
                self.mode = "RGB"
                self.tile = []
                while True:
                    try:
                        offset, = unpack("<B", _data.read(1))
                    except StructError:
                        break
                    b, g, r, a = palette[offset]
                    data.append(pack("<BBB", r, g, b))

                data = "".join(data)
                self.im = Image.core.new(self.mode, self.size)
                self.fromstring(data)
                return

            raise ValueError("Expected encoding 3, 4 or 5, got %i instead" %
                             (encoding))

        raise ValueError("Expected compression 0 or 1, got %i instead" %
                         (compression))
コード例 #2
0
async def getImageToBlackWhite(img: JpegImageFile) -> JpegImageFile:
    grey_img = img.copy()
    (width, height) = grey_img.size
    for x in range(width):
        for y in range(height):
            (R, G, B) = img.getpixel((x, y))
            grey_color = int((R + G + B) / 3)
            grey_img.putpixel((x, y), (grey_color, grey_color, grey_color))
    return grey_img
コード例 #3
0
ファイル: BLPImagePlugin.py プロジェクト: GanjaNoel/pym2
	def __decode_blp1(self):
		header = StringIO(self.fp.read(28 + 16*4 + 16*4))
		
		magic, compression = unpack("<4si", header.read(8))
		encoding, alphaDepth, alphaEncoding, hasMips = unpack("<4b", header.read(4))
		self.size = unpack("<II", header.read(8))
		encoding, subtype = unpack("<ii", header.read(8))
		offsets = unpack("<16I", header.read(16*4))
		lengths = unpack("<16I", header.read(16*4))
		
		if compression == 0:
			from PIL.JpegImagePlugin import JpegImageFile
			jpegHeaderSize, = unpack("<I", self.fp.read(4))
			jpegHeader = self.fp.read(jpegHeaderSize)
			extraData = self.fp.read(offsets[0] - self.fp.tell()) # What IS this?
			data = self.fp.read(lengths[0])
			data = jpegHeader + data
			data = StringIO(data)
			image = JpegImageFile(data)
			image.show()
			raw_input()
			self.tile = image.tile # PIL is terrible
			self.fp = image.fp
			self.mode = image.mode
			return
		
		if compression == 1:
			if encoding in (3, 4):
				raise NotImplementedError
			
			elif encoding == 5:
				data = []
				palette_data = self.fp.read(256*4)
				palette = getpalette(palette_data)
				_data = StringIO(self.fp.read(lengths[0]))
				self.mode = "RGB"
				self.tile = []
				while True:
					try:
						offset, = unpack("<B", _data.read(1))
					except StructError:
						break
					b, g, r, a = palette[offset]
					data.append(pack("<BBB", r, g, b))
				
				data = "".join(data)
				self.im = Image.core.new(self.mode, self.size)
				self.fromstring(data)
				return
			
			raise ValueError("Expected encoding 3, 4 or 5, got %i instead" % (encoding))
		
		raise ValueError("Expected compression 0 or 1, got %i instead" % (compression))
コード例 #4
0
ファイル: optimize.py プロジェクト: tyleratredspace/its
def optimize_jpg(
        img: JpegImageFile,
        tmp_file: Any,
        quality: Optional[int] = DEFAULT_JPEG_QUALITY) -> JpegImageFile:
    if not quality:
        quality = DEFAULT_JPEG_QUALITY
    elif quality > 95:
        quality = 95  # 95 is the recommended upper limit on quality for JPEGs in PIL

    img.save(tmp_file.name,
             "JPEG",
             quality=quality,
             optimize=True,
             progressive=True)

    return Image.open(tmp_file.name)
コード例 #5
0
def _rotate_if_required(image: JpegImageFile, exif_obj: Optional[dict]):
    """
    rotate image if exif file has a rotate directive
    (solves chrome bug not respecting orientation exif tag)
    no exif tag manipulation required as this is not saved
    to thumbnails anyway
    """
    if exif_obj:  # the image might have no exif-tags
        # noinspection PyProtectedMember
        exif = dict(exif_obj.items())
        if piexif.ImageIFD.Orientation in exif:
            if exif[piexif.ImageIFD.Orientation] == 3:
                image = image.rotate(180, expand=True)
            elif exif[piexif.ImageIFD.Orientation] == 6:
                image = image.rotate(270, expand=True)
            elif exif[piexif.ImageIFD.Orientation] == 8:
                image = image.rotate(90, expand=True)
    return image
コード例 #6
0
    def _decode_jpeg_stream(self):
        from PIL.JpegImagePlugin import JpegImageFile

        jpeg_header_size, = struct.unpack("<I", self.fd.read(4))
        jpeg_header = self.fd.read(jpeg_header_size)
        self.fd.read(self._blp_offsets[0] - self.fd.tell())  # What IS this?
        data = self.fd.read(self._blp_lengths[0])
        data = jpeg_header + data
        data = BytesIO(data)
        image = JpegImageFile(data)
        self.tile = image.tile  # :/
        self.fd = image.fp
        self.mode = image.mode
コード例 #7
0
async def getImageMD5(img: JpegImageFile) -> str:
    return hashlib.md5(img.tobytes()).hexdigest()
コード例 #8
0
def image_to_binary(image: JpegImageFile) -> ByteString:
    with io.BytesIO() as output:
        image.save(output, format="PNG")
        return output.getvalue()
コード例 #9
0
def get_labels(image: JpegImageFile):
    raw = image.getexif()[piexif.ExifIFD.MakerNote]
    tags = pickle.loads(raw)
    return tags
コード例 #10
0
def save_image_file_locally(image: JpegImageFile, image_name: str) -> None:

    image_directory = app.config["IMAGE_DIRECTORY"]
    image_location = os.path.join(image_directory, image_name)

    image.save(image_location)