From d7703845e45a0af5d49ff2626aa1c7840e48789e Mon Sep 17 00:00:00 2001 From: Xevion Date: Thu, 22 Aug 2019 21:03:46 -0500 Subject: [PATCH] XMP/IPTC checking implemented, now in a working state, removed dry run code --- package/__pycache__/xmp.cpython-37.pyc | Bin 2592 -> 2590 bytes package/app.py | 50 +++++++++++++++---------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/package/__pycache__/xmp.cpython-37.pyc b/package/__pycache__/xmp.cpython-37.pyc index c9ba92fac155f95d27fc3a32bfa23cac8e102786..26e952d911a9208454a76bce139ab3065756484f 100644 GIT binary patch delta 82 zcmZ1=GEaopiIz>% diff --git a/package/app.py b/package/app.py index dd83c18..d3e3381 100644 --- a/package/app.py +++ b/package/app.py @@ -31,27 +31,36 @@ def process_file(file_name, xmp_name=None): return round(size, 2), type # Optimizes a file using JPEG thumbnailing and compression. - def _optimize(file_path, size=(512, 512), quality=85): + def _optimize(file_path, size=(512, 512), quality=85, copy=None): image = Image.open(file_path) image.thumbnail(size, resample=Image.ANTIALIAS) - image.save(file_path, format='jpeg', optimize=True, quality=quality) + if copy: + image.save(copy, format='jpeg', optimize=True, quality=quality) + else: + image.save(file_path, format='jpeg', optimize=True, quality=quality) base, ext = os.path.splitext(file_name) temp_file_path = os.path.join(temp_path, base + '.jpeg') try: - # Process the file into a JPEG - rgb = rawpy.imread(os.path.join(input_path, file_name)) - imageio.imsave(os.path.join(temp_file_path), rgb.postprocess()) - rgb.close() + if xmp_name: + # Process the file into a JPEG + rgb = rawpy.imread(os.path.join(input_path, file_name)) + imageio.imsave(temp_file_path, rgb.postprocess()) + rgb.close() - # Information on file sizes - print("Raw Size: {} {}".format(*_size(os.path.join(input_path, file_name))), end=' | ') - print("Resave Size: {} {}".format(*_size(temp_file_path)), end=' | ') - pre = os.path.getsize(temp_file_path) - _optimize(temp_file_path) - post = os.path.getsize(temp_file_path) - print("Optimized Size: {} {} ({}% savings)".format(*_size(temp_file_path), round((1.0 - (post / pre)) * 100), 2) ) + # Information on file sizes + print("Raw Size: {} {}".format(*_size(os.path.join(input_path, file_name))), end=' | ') + print("Resave Size: {} {}".format(*_size(temp_file_path)), end=' | ') + pre = os.path.getsize(temp_file_path) + _optimize(temp_file_path) + post = os.path.getsize(temp_file_path) + print("Optimized Size: {} {} ({}% savings)".format(*_size(temp_file_path), round((1.0 - (post / pre)) * 100), 2) ) + else: + pre = os.path.getsize(os.path.join(input_path, file_name)) + _optimize(os.path.join(input_path, file_name), copy=temp_file_path) + post = os.path.getsize(temp_file_path) + print("Optimized Size: {} {} ({}% savings)".format(*_size(temp_file_path), round((1.0 - (post / pre)) * 100), 2) ) # Open the image, read as bytes, convert to types Image image = Image.open(temp_file_path) @@ -70,18 +79,22 @@ def process_file(file_name, xmp_name=None): print('\tWriting {} tags to output XMP...'.format(len(labels))) parser = xmp.XMPParser(os.path.join(input_path, xmp_name)) parser.add_keywords(labels) + # Save the new XMP file parser.save(os.path.join(output_path, xmp_name)) + # Remove the old XMP file + os.remove(os.path.join(input_path, xmp_name)) # No XMP file is specified, using IPTC tagging else: print('\tWriting {} tags to output {}'.format(len(labels), ext[1:].upper())) info = iptcinfo3.IPTCInfo(os.path.join(input_path, file_name)) info['keywords'].extend(labels) info.save() + # Remove the weird ghsot file created by this iptc read/writer. + os.remove(os.path.join(input_path, file_name + '~')) # Copy dry-run - shutil.copy2(os.path.join(input_path, file_name), os.path.join(output_path, file_name)) - # os.rename(os.path.join(input_path, file_name), os.path.join(output_path, file_name)) - + # shutil.copy2(os.path.join(input_path, file_name), os.path.join(output_path, file_name)) + os.rename(os.path.join(input_path, file_name), os.path.join(output_path, file_name)) except: _cleanup() raise @@ -154,6 +167,5 @@ def run(): raise # Remove the directory, we are done here - print('Cleaning up temporary directory...', end=' ') - os.rmdir(temp_path) - print('Complete.') \ No newline at end of file + print('Cleaning up temporary directory...') + os.rmdir(temp_path) \ No newline at end of file