mirror of
				https://github.com/KevinMidboe/leifs-image-processor.git
				synced 2025-10-29 17:50:20 +00:00 
			
		
		
		
	Also handle rotating images that do not have exif data supplied.
This commit is contained in:
		
							
								
								
									
										43
									
								
								processor.py
									
									
									
									
									
								
							
							
						
						
									
										43
									
								
								processor.py
									
									
									
									
									
								
							| @@ -1,6 +1,6 @@ | |||||||
| import glob | import glob | ||||||
| import os | import os | ||||||
| from PIL import Image | from PIL import Image, ExifTags | ||||||
| import concurrent.futures | import concurrent.futures | ||||||
| import argparse | import argparse | ||||||
| import fileinput | import fileinput | ||||||
| @@ -47,24 +47,35 @@ def processImage(file, outputPath=None): | |||||||
|     } |     } | ||||||
|  |  | ||||||
| def rotateFromExifMetadata(image): | def rotateFromExifMetadata(image): | ||||||
|  |   """ This function autorotates a picture """ | ||||||
|   try: |   try: | ||||||
|     for orientation in ExifTags.TAGS.keys(): |     exif = image._getexif() | ||||||
|       if ExifTags.TAGS[orientation]=='Orientation': |   except AttributeError as e: | ||||||
|         break |     print("Could not get exif - Bad image!") | ||||||
|       exif=dict(image._getexif().items()) |     return False | ||||||
|  |  | ||||||
|       if exif[orientation] == 3: |   (width, height) = image.size | ||||||
|         image=image.rotate(180, expand=True) |   if not exif: | ||||||
|       elif exif[orientation] == 6: |     if width > height: | ||||||
|         image=image.rotate(270, expand=True) |       return image.rotate(90) | ||||||
|       elif exif[orientation] == 8: |   else: | ||||||
|         image=image.rotate(90, expand=True) |     orientation_key = 274 # cf ExifTags | ||||||
|  |     if orientation_key in exif: | ||||||
|  |       orientation = exif[orientation_key] | ||||||
|  |       rotate_values = { | ||||||
|  |         3: 180, | ||||||
|  |         6: 270, | ||||||
|  |         8: 90 | ||||||
|  |       } | ||||||
|        |        | ||||||
|       return image |       if orientation in rotate_values: | ||||||
|   except (AttributeError, KeyError, IndexError): |         # Rotate and return the picture | ||||||
|     # cases: image don't have getexif |         return image.rotate(rotate_values[orientation]) | ||||||
|     print('unable to find orientation exif metadata') |       else: | ||||||
|     pass |         if width > height: | ||||||
|  |           return image.rotate(90) | ||||||
|  |  | ||||||
|  |   return image | ||||||
|  |  | ||||||
| def generateFilename(fileID, modifier, outputPath): | def generateFilename(fileID, modifier, outputPath): | ||||||
|     filename = "{}_{}.{}".format(fileID, modifier, OUTPUT_EXTENSION) |     filename = "{}_{}.{}".format(fileID, modifier, OUTPUT_EXTENSION) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user