Compare commits
	
		
			5 commits
		
	
	
		
			master
			...
			galerie_se
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|   | a44fcce1c8 | ||
|   | 2cbaee10aa | ||
|   | 1d157e7e68 | ||
|   | d999c46e5e | ||
|   | 88695313c1 | 
					 1 changed files with 38 additions and 0 deletions
				
			
		|  | @ -100,3 +100,41 @@ class Galerie(models.Model): | |||
| 	class Meta: | ||||
| 		verbose_name = 'Galerie' | ||||
| 		verbose_name_plural = 'Galerie' | ||||
| 
 | ||||
| 	# TODO: patří to spíš sem, nebo do nějakých utils? | ||||
| 	def setrid_galerii_podle_exifu(self): | ||||
| 		"""Setřídí galerii podle data v EXIF tazích. | ||||
| 
 | ||||
| 		Velmi experimentální, zatím pro použití jen webařem v shellu (důvod: | ||||
| 		EXIF se trochu blbě parsuje, není jasné, jestli se má použít DateTime, | ||||
| 		DateTimeOriginal nebo DateTimeDigitized, EXIF nemusí mít informace o | ||||
| 		časové zóně a tenhle kód časovou zónu ignoruje, vůbec nezachovává | ||||
| 		pořadí).""" | ||||
| 
 | ||||
| 		from PIL import Image, ExifTags, UnidentifiedImageError | ||||
| 		from datetime import datetime | ||||
| 		EXIF_DATETIME_FMT = "%Y:%m:%d %H:%M:%S" | ||||
| 
 | ||||
| 		self.obrazek_set.update(poradi=None) | ||||
| 		for obrazek in self.obrazek_set.all(): | ||||
| 			try: | ||||
| 				obr = Image.open(obrazek.obrazek_velky) | ||||
| 			except UnidentifiedImageError as e: | ||||
| 				raise ValueError from e | ||||
| 
 | ||||
| 			exif = obr.getexif() # Pokud tam není, tak si Pillow podle zdrojáků vyhaluzí prázdný. | ||||
| 			ifd_exif = {} | ||||
| 			if ExifTags.IFD.Exif in exif: ifd_exif = exif.get_ifd(ExifTags.IFD.Exif) | ||||
| 			date_str = (False # zarovnání | ||||
| 				or exif.get(ExifTags.Base.DateTimeOriginal, None) | ||||
| 				or ifd_exif.get(ExifTags.Base.DateTimeOriginal, None) | ||||
| 				or exif.get(ExifTags.Base.DateTime, None) | ||||
| 				or ifd_exif.get(ExifTags.Base.DateTime, None) | ||||
| 				or exif.get(ExifTags.Base.DateTimeDigitized, None) | ||||
| 				or ifd_exif.get(ExifTags.Base.DateTimeDigitized, None) | ||||
| 				) | ||||
| 			if date_str is None: continue | ||||
| 			dt = datetime.strptime(date_str, EXIF_DATETIME_FMT) | ||||
| 			poradi = int(dt.strftime("1%d%H%M%S")) | ||||
| 			obrazek.poradi = poradi | ||||
| 			obrazek.save() | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue