mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-11-04 01:47:47 +03:00 
			
		
		
		
	Parse orientation from XMP tags
This commit is contained in:
		
							parent
							
								
									97280a045c
								
							
						
					
					
						commit
						1e63f772f8
					
				
							
								
								
									
										
											BIN
										
									
								
								Tests/images/xmp_tags_orientation.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								Tests/images/xmp_tags_orientation.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 171 KiB  | 
| 
						 | 
					@ -603,6 +603,11 @@ class TestFilePng:
 | 
				
			||||||
            exif = im._getexif()
 | 
					            exif = im._getexif()
 | 
				
			||||||
        assert exif[274] == 1
 | 
					        assert exif[274] == 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_xmp_tags_orientation(self):
 | 
				
			||||||
 | 
					        with Image.open("Tests/images/xmp_tags_orientation.png") as im:
 | 
				
			||||||
 | 
					            exif = im.getexif()
 | 
				
			||||||
 | 
					        assert exif[274] == 3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_exif_save(self, tmp_path):
 | 
					    def test_exif_save(self, tmp_path):
 | 
				
			||||||
        with Image.open("Tests/images/exif.png") as im:
 | 
					        with Image.open("Tests/images/exif.png") as im:
 | 
				
			||||||
            test_file = str(tmp_path / "temp.png")
 | 
					            test_file = str(tmp_path / "temp.png")
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -35,6 +35,7 @@ import struct
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
import tempfile
 | 
					import tempfile
 | 
				
			||||||
import warnings
 | 
					import warnings
 | 
				
			||||||
 | 
					import xml.etree.ElementTree
 | 
				
			||||||
from collections.abc import Callable, MutableMapping
 | 
					from collections.abc import Callable, MutableMapping
 | 
				
			||||||
from pathlib import Path
 | 
					from pathlib import Path
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1297,10 +1298,30 @@ class Image:
 | 
				
			||||||
            return tuple(extrema)
 | 
					            return tuple(extrema)
 | 
				
			||||||
        return self.im.getextrema()
 | 
					        return self.im.getextrema()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def _parse_xmp_tags(self):
 | 
				
			||||||
 | 
					        if 0x0112 in self._exif:
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        xmp_tags = self.info.get("XML:com.adobe.xmp")
 | 
				
			||||||
 | 
					        if not xmp_tags:
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        root = xml.etree.ElementTree.fromstring(xmp_tags)
 | 
				
			||||||
 | 
					        for elem in root.iter():
 | 
				
			||||||
 | 
					            if elem.tag.endswith("}Description"):
 | 
				
			||||||
 | 
					                break
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        orientation = elem.attrib.get("{http://ns.adobe.com/tiff/1.0/}Orientation")
 | 
				
			||||||
 | 
					        if orientation:
 | 
				
			||||||
 | 
					            self._exif[0x0112] = int(orientation)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def getexif(self):
 | 
					    def getexif(self):
 | 
				
			||||||
        if self._exif is None:
 | 
					        if self._exif is None:
 | 
				
			||||||
            self._exif = Exif()
 | 
					            self._exif = Exif()
 | 
				
			||||||
        self._exif.load(self.info.get("exif"))
 | 
					        self._exif.load(self.info.get("exif"))
 | 
				
			||||||
 | 
					        self._parse_xmp_tags()
 | 
				
			||||||
        return self._exif
 | 
					        return self._exif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def getim(self):
 | 
					    def getim(self):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -940,6 +940,7 @@ class PngImageFile(ImageFile.ImageFile):
 | 
				
			||||||
                "".join(self.info["Raw profile type exif"].split("\n")[3:])
 | 
					                "".join(self.info["Raw profile type exif"].split("\n")[3:])
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
        self._exif.load(exif_info)
 | 
					        self._exif.load(exif_info)
 | 
				
			||||||
 | 
					        self._parse_xmp_tags()
 | 
				
			||||||
        return self._exif
 | 
					        return self._exif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _close__fp(self):
 | 
					    def _close__fp(self):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user