Browse Source

Support stripping _only_ GPS and/or orientation shit (as well as all EXIF tags obv), optimise checking for EXIF etc and added version thing for keks lol

Wazakindjes 3 years ago
parent
commit
3b1882b408
1 changed files with 70 additions and 33 deletions
  1. 70 33
      hurr.py

+ 70 - 33
hurr.py

@@ -4,6 +4,7 @@ import datetime
 import gntp.notifier
 import os
 import paramiko
+import piexif
 import pyperclip
 import random
 import re
@@ -19,6 +20,8 @@ SKIPPEM = SKIPPEM_ALT = SKIPPEM_DB = False
 AMOUNT = 0
 SSH = None
 GROWL = None
+EXIF_ORIENT_TAG = -1
+VERSION = '1.12'
 MUHCONF = {
 	'growlname': None,
 	'icondir': None,
@@ -38,6 +41,8 @@ MUHCONF = {
 	'scrotpattern_alt': '',
 	'scrotdir_db': '',
 	'scrotpattern_db': '',
+	'exifgps': None,
+	'exifrotate': None,
 	'fuckexif': None,
 }
 
@@ -128,44 +133,70 @@ def growlnotif(ID, errorid=None, errormsg=None, desc=None):
 def upload(local, remote):
 	global AMOUNT, MUHCONF
 
-	if MUHCONF['fuckexif'] == True:
+	strippem_gps = (not MUHCONF['exifgps'])
+	strippem_all = MUHCONF['fuckexif']
+	rotatem = MUHCONF['exifrotate']
+
+	growlnotif(0)
+	if strippem_gps or rotatem or strippem_all:
 		try:
+			growlnotif(3, desc="Checking for EXIF data")
 			image_exif = Image.open(local)
-			exif = dict(image_exif._getexif().items())
-			found_orient = False
+			exif_raw = piexif.load(image_exif.info['exif'])
+			gotexif = False
 
-			growlnotif(3, desc="Stripping EXIF data")
-			for oi in ExifTags.TAGS.keys():
-				if ExifTags.TAGS[oi] == 'Orientation':
-					found_orient = True
+			for etype in exif_raw.keys():
+				if exif_raw[etype] != {} and exif_raw[etype] != None:
+					gotexif = True
 					break
 
-			if found_orient == True and exif[oi] != 1:
-				growlnotif(3, desc="Rotating image from EXIF orientation tag first tho lol")
-
-				orientation = exif[oi]
-				if orientation is 6:
-					image_exif = image_exif.rotate(-90, expand=True)
-				elif orientation is 8:
-					image_exif = image_exif.rotate(90, expand=True)
-				elif orientation is 3:
-					image_exif = image_exif.rotate(180, expand=True)
-				elif orientation is 2:
-					image_exif = image_exif.transpose(Image.FLIP_LEFT_RIGHT)
-				elif orientation is 5:
-					image_exif = image_exif.rotate(-90, expand=True).transpose(Image.FLIP_LEFT_RIGHT)
-				elif orientation is 7:
-					image_exif = image_exif.rotate(90, expand=True).transpose(Image.FLIP_LEFT_RIGHT)
-				elif orientation is 4:
-					image_exif = image_exif.rotate(180, expand=True).transpose(Image.FLIP_LEFT_RIGHT)
-
-			image_exif.save(local, exif="")
-			image_exif.close()
-
-		except Exception as e:
+			if gotexif:
+				if strippem_gps and not strippem_all:
+					try:
+						if exif_raw['GPS'] != {}:
+							exif_raw.pop('GPS')
+							print "Stripped GPS EXIF"
+					except:
+						pass
+
+				if rotatem or strippem_all:
+					try:
+						orientation = exif_raw['0th'][EXIF_ORIENT_TAG]
+						if orientation in [6, 8, 3, 2, 5, 7, 4]:
+							print "Found orientation %d instead of 1, rotating/flipping that shit" % (orientation)
+							growlnotif(3, desc="Rotating image from EXIF orientation tag lol")
+							exif_raw['0th'][EXIF_ORIENT_TAG] = 1
+
+							if orientation is 6:
+								image_exif = image_exif.rotate(-90, expand=True)
+							elif orientation is 8:
+								image_exif = image_exif.rotate(90, expand=True)
+							elif orientation is 3:
+								image_exif = image_exif.rotate(180, expand=True)
+							elif orientation is 2:
+								image_exif = image_exif.transpose(Image.FLIP_LEFT_RIGHT)
+							elif orientation is 5:
+								image_exif = image_exif.rotate(-90, expand=True).transpose(Image.FLIP_LEFT_RIGHT)
+							elif orientation is 7:
+								image_exif = image_exif.rotate(90, expand=True).transpose(Image.FLIP_LEFT_RIGHT)
+							elif orientation is 4:
+								image_exif = image_exif.rotate(180, expand=True).transpose(Image.FLIP_LEFT_RIGHT)
+
+							if exif_raw['thumbnail'] != {} and not strippem_all:
+								exif_raw.pop('thumbnail')
+								print "Stripped thumbnail IFD to prevent previews etc from fucking up"
+					except:
+						pass
+
+					if strippem_all:
+						print "Stripping all EXIF data lol"
+						exif_raw = {}
+					image_exif.save(local, exif=piexif.dump(exif_raw))
+					image_exif.close()
+
+		except:
 			pass
 
-	growlnotif(0)
 	try:
 		SSH.connect(MUHCONF['server'], username=MUHCONF['joeser'], key_filename="%s/%s" % (MUHCONF['sshdir'], MUHCONF['sshkey']));
 		SSH.exec_command('find %s -type f -size 0 -exec rm "{}" \;' % MUHCONF['remotebase'])
@@ -262,6 +293,7 @@ if __name__ == "__main__":
 	MUHCONF['icondir'] = "%s/icinz" % muhdir
 	Config = ConfigParser.ConfigParser()
 	gottem = Config.read(muhdir + "/hurr.conf")
+	print "Starting up HurrDroppert v%s lol" % VERSION
 	print "Parsing config: %s/hurr.conf\r\n" % muhdir
 
 	if gottem == []:
@@ -276,7 +308,7 @@ if __name__ == "__main__":
 					cval = None
 					if copt in ["archivecount", "archivewait", "archivetime"]:
 						cval = Config.getint(csect, copt)
-					elif copt in ["fuckexif"]:
+					elif copt in ["exifgps", "exifrotate", "fuckexif"]:
 						cval = Config.getboolean(csect, copt)
 					else:
 						cval = (Config.get(csect, copt)).strip();
@@ -312,12 +344,17 @@ if __name__ == "__main__":
 		cval = MUHCONF[copt]
 		print "** %s = %s" % (copt, cval)
 
+	for oi in ExifTags.TAGS.keys():
+		if ExifTags.TAGS[oi] == 'Orientation':
+			EXIF_ORIENT_TAG = oi
+			break
+
 	print "\r\nGetting current directory contents"
 	OLDFILES = getscrots()
 	OLDFILES_ALT = getscrots_alt()
 	OLDFILES_DB = getscrots_db()
 
-	print "Setting up SSH using key pair: %s/%s" % (MUHCONF['sshdir'], MUHCONF['sshkey'])
+	print "Setting up SSH using key pair: %s/%s{,.pub}" % (MUHCONF['sshdir'], MUHCONF['sshkey'])
 	SSH = paramiko.SSHClient()
 	SSH.known_hosts = None