mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-11-04 01:47:47 +03:00 
			
		
		
		
	Added reading of JPEG2000 comments
This commit is contained in:
		
							parent
							
								
									cdf5fd439c
								
							
						
					
					
						commit
						cbde5b2626
					
				
							
								
								
									
										
											BIN
										
									
								
								Tests/images/comment.jp2
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								Tests/images/comment.jp2
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
						 | 
				
			
			@ -353,6 +353,17 @@ def test_subsampling_decode(name):
 | 
			
		|||
        assert_image_similar(im, expected, epsilon)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_comment():
 | 
			
		||||
    with Image.open("Tests/images/comment.jp2") as im:
 | 
			
		||||
        assert im.info["comment"] == b"Created by OpenJPEG version 2.5.0"
 | 
			
		||||
 | 
			
		||||
    # Test an image that is truncated partway through a codestream
 | 
			
		||||
    with open("Tests/images/comment.jp2", "rb") as fp:
 | 
			
		||||
        b = BytesIO(fp.read(130))
 | 
			
		||||
        with Image.open(b) as im:
 | 
			
		||||
            pass
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.mark.parametrize(
 | 
			
		||||
    "test_file",
 | 
			
		||||
    [
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -43,6 +43,12 @@ Added ``corners`` argument to ``ImageDraw.rounded_rectangle()``
 | 
			
		|||
``corners``. This a tuple of Booleans, specifying whether to round each corner,
 | 
			
		||||
``(top_left, top_right, bottom_right, bottom_left)``.
 | 
			
		||||
 | 
			
		||||
Reading JPEG comments
 | 
			
		||||
^^^^^^^^^^^^^^^^^^^^^
 | 
			
		||||
 | 
			
		||||
When opening a JPEG2000 image, the comment may now be read into
 | 
			
		||||
:py:attr:`~PIL.Image.Image.info`.
 | 
			
		||||
 | 
			
		||||
Security
 | 
			
		||||
========
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -218,6 +218,8 @@ class Jpeg2KImageFile(ImageFile.ImageFile):
 | 
			
		|||
                self._size, self.mode, self.custom_mimetype, dpi = header
 | 
			
		||||
                if dpi is not None:
 | 
			
		||||
                    self.info["dpi"] = dpi
 | 
			
		||||
                if self.fp.read(12).endswith(b"jp2c\xff\x4f\xff\x51"):
 | 
			
		||||
                    self._parse_comment()
 | 
			
		||||
            else:
 | 
			
		||||
                msg = "not a JPEG 2000 file"
 | 
			
		||||
                raise SyntaxError(msg)
 | 
			
		||||
| 
						 | 
				
			
			@ -254,6 +256,28 @@ class Jpeg2KImageFile(ImageFile.ImageFile):
 | 
			
		|||
            )
 | 
			
		||||
        ]
 | 
			
		||||
 | 
			
		||||
    def _parse_comment(self):
 | 
			
		||||
        hdr = self.fp.read(2)
 | 
			
		||||
        length = struct.unpack(">H", hdr)[0]
 | 
			
		||||
        self.fp.seek(length - 2, os.SEEK_CUR)
 | 
			
		||||
 | 
			
		||||
        while True:
 | 
			
		||||
            marker = self.fp.read(2)
 | 
			
		||||
            if not marker:
 | 
			
		||||
                break
 | 
			
		||||
            typ = marker[1]
 | 
			
		||||
            if typ in (0x90, 0xD9):
 | 
			
		||||
                # Start of tile or end of codestream
 | 
			
		||||
                break
 | 
			
		||||
            hdr = self.fp.read(2)
 | 
			
		||||
            length = struct.unpack(">H", hdr)[0]
 | 
			
		||||
            if typ == 0x64:
 | 
			
		||||
                # Comment
 | 
			
		||||
                self.info["comment"] = self.fp.read(length - 2)[2:]
 | 
			
		||||
                break
 | 
			
		||||
            else:
 | 
			
		||||
                self.fp.seek(length - 2, os.SEEK_CUR)
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def reduce(self):
 | 
			
		||||
        # https://github.com/python-pillow/Pillow/issues/4343 found that the
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user