further class refactoring

i have really bad commit names rn lol
This commit is contained in:
Xevion
2019-11-01 21:17:38 -05:00
parent 9165e675be
commit 40d200dd74
3 changed files with 26 additions and 25 deletions

View File

@@ -2,7 +2,7 @@ import os
import sys
# Path Constants
ROOT = sys.path, [0]
ROOT = sys.path[0]
PROCESSING_PATH = os.path.join(ROOT, 'package', 'processing')
INPUT_PATH = os.path.join(PROCESSING_PATH, 'input')
TEMP_PATH = os.path.join(PROCESSING_PATH, 'temp')
@@ -19,4 +19,4 @@ RAW_EXTS = [
"rw2", "rwz", "sr2", "srf", "srw", "tif",
"x3f",
]
LOSSY_EXTS = ["JPEG", "JPG", "PNG"]
LOSSY_EXTS = ["jpeg", "jpg", "png"]

View File

@@ -5,6 +5,7 @@ from google.cloud import vision
from .process import FileProcessor
from . import INPUT_PATH, TEMP_PATH, OUTPUT_PATH, PROCESSING_PATH
from . import RAW_EXTS, LOSSY_EXTS
# Process a single file in these steps:
# 1) Create a temporary file
@@ -13,7 +14,7 @@ from . import INPUT_PATH, TEMP_PATH, OUTPUT_PATH, PROCESSING_PATH
# 4) Delete temporary file, move NEF/JPEG and XMP
# Driver code for the package
def run(client):
def run():
# Ensure that 'input' and 'output' directories are created
if not os.path.exists(INPUT_PATH):
print('Input directory did not exist, creating and quitting.')
@@ -24,7 +25,7 @@ def run(client):
print('Output directory did not exist. Creating...')
os.makedirs(OUTPUT_PATH)
# Clients
# Client
client = vision.ImageAnnotatorClient()
# Find files we want to process based on if they have a corresponding .XMP
@@ -39,9 +40,9 @@ def run(client):
# Process files
for index, file in progressbar.progressbar(list(enumerate(select)), redirect_stdout=True, term_width=110):
name, ext = os.path.splitext(file)
ext = ext.upper()
ext = ext.lower().strip('.')
# Raw files contain their metadata in an XMP file usually
if ext in ['.NEF', '.CR2']:
if ext in RAW_EXTS:
# Get all possible files
identicals = [possible for possible in files
if possible.startswith(os.path.splitext(file)[0])
@@ -68,10 +69,11 @@ def run(client):
else:
print('Processing file {}, \'{}\''.format(index + 1, xmps[0]), end=' | ')
file = FileProcessor(file, xmps[0])
elif ext in BASIC_EXTENSIONS:
file.run(client)
elif ext in LOSSY_EXTS:
print('Processing file {}, \'{}\''.format(index + 1, file), end=' | ')
file = FileProcessor(file, xmps[0])
file = FileProcessor(file)
file.run(client)
except:
os.rmdir(TEMP_PATH)
raise

View File

@@ -17,6 +17,15 @@ class FileProcessor(object):
self.base, self.ext = os.path.splitext(self.file_name)
self.temp_file_path = os.path.join(TEMP_PATH, self.base + '.jpeg')
# Optimizes a file using JPEG thumbnailing and compression.
def _optimize(self, file_path, size=(512, 512), quality=85, copy=None):
image = Image.open(file_path)
image.thumbnail(size, resample=Image.ANTIALIAS)
if copy:
image.save(copy, format='jpeg', optimize=True, quality=quality)
else:
image.save(file_path, format='jpeg', optimize=True, quality=quality)
def rawOptimize(self):
rgb = rawpy.imread(os.path.join(INPUT_PATH, self.file_name))
imageio.imsave(temp_file_path, rgb.postprocess())
@@ -26,13 +35,13 @@ class FileProcessor(object):
print("Raw Size: {} {}".format(*_size(os.path.join(INPUT_PATH, self.file_name))), end=' | ')
print("Resave Size: {} {}".format(*_size(temp_file_path)), end=' | ')
pre = os.path.getsize(temp_file_path)
_optimize(temp_file_path)
self._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) )
def basicOptimize(self):
pre = os.path.getsize(os.path.join(INPUT_PATH, self.file_name))
_optimize(os.path.join(INPUT_PATH, self.file_name), copy=temp_file_path)
self._optimize(os.path.join(INPUT_PATH, self.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) )
@@ -68,16 +77,15 @@ class FileProcessor(object):
os.remove(os.path.join(INPUT_PATH, self.xmp_name))
# No XMP file is specified, using IPTC tagging
else:
print('\tWriting {} tags to output {}'.format(len(labels), ext[1:].upper()))
print('\tWriting {} tags to output {}'.format(len(labels), self.ext[1:].upper()))
info = iptcinfo3.IPTCInfo(os.path.join(INPUT_PATH, self.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, self.file_name + '~'))
# Copy dry-run
# shutil.copy2(os.path.join(INPUT_PATH, self.file_name), os.path.join(OUTPUT_PATH, self.file_name))
os.rename(os.path.join(INPUT_PATH, self.file_name), os.path.join(OUTPUT_PATH, self.file_name))
shutil.copy2(os.path.join(INPUT_PATH, self.file_name), os.path.join(OUTPUT_PATH, self.file_name))
# os.rename(os.path.join(INPUT_PATH, self.file_name), os.path.join(OUTPUT_PATH, self.file_name))
except:
self._cleanup()
raise
@@ -93,13 +101,4 @@ class FileProcessor(object):
def _size(self, file_path):
size, type = os.path.getsize(file_path) / 1024, 'KiB'
if size >= 1024: size /= 1024; type = 'MiB'
return round(size, 2), type
# Optimizes a file using JPEG thumbnailing and compression.
def _optimize(self, file_path, size=(512, 512), quality=85, copy=None):
image = Image.open(file_path)
image.thumbnail(size, resample=Image.ANTIALIAS)
if copy:
image.save(copy, format='jpeg', optimize=True, quality=quality)
else:
image.save(file_path, format='jpeg', optimize=True, quality=quality)
return round(size, 2), type