Improve rejpg.

This commit is contained in:
voussoir 2023-06-24 12:04:46 -07:00
parent fa2610e206
commit 4c2e8f8e70

View file

@ -25,7 +25,7 @@ def compress_to_filesize(image, target_size, *, exif=None):
current = 100 current = 100
while True: while True:
# print(lower, current, upper) # print(lower, current, upper)
if lower == (upper - 1): if lower == 100 or lower == (upper - 1):
break break
bio = io.BytesIO() bio = io.BytesIO()
image.save(bio, format='jpeg', exif=exif, quality=current) image.save(bio, format='jpeg', exif=exif, quality=current)
@ -47,19 +47,18 @@ def rejpg_argparse(args):
patterns = pipeable.input_many(args.patterns, skip_blank=True, strip=True) patterns = pipeable.input_many(args.patterns, skip_blank=True, strip=True)
files = spinal.walk(recurse=args.recurse, glob_filenames=patterns) files = spinal.walk(recurse=args.recurse, glob_filenames=patterns)
files = [f.absolute_path for f in files]
bytes_saved = 0 bytes_saved = 0
remaining_size = 0 remaining_size = 0
for filename in files: for file in files:
log.info('Processing %s.', filename) log.info('Processing %s.', file.absolute_path)
image = PIL.Image.open(filename) image = PIL.Image.open(file.absolute_path)
icc_profile = image.info.get('icc_profile') icc_profile = image.info.get('icc_profile')
(image, exif) = imagetools.rotate_by_exif(image) (image, exif) = imagetools.rotate_by_exif(image)
if args.filesize and file.size < args.filesize:
continue
if args.filesize: if args.filesize:
target_size = bytestring.parsebytes(args.filesize)
bytesio = compress_to_filesize(image, target_size, exif=exif) bytesio = compress_to_filesize(image, target_size, exif=exif)
else: else:
bytesio = io.BytesIO() bytesio = io.BytesIO()
@ -67,14 +66,12 @@ def rejpg_argparse(args):
bytesio.seek(0) bytesio.seek(0)
new_bytes = bytesio.read() new_bytes = bytesio.read()
old_size = os.path.getsize(filename) old_size = file.size
new_size = len(new_bytes) new_size = len(new_bytes)
remaining_size += new_size remaining_size += new_size
if new_size < old_size: if new_size < old_size:
bytes_saved += (old_size - new_size) bytes_saved += (old_size - new_size)
f = open(filename, 'wb') file.write('wb', new_bytes)
f.write(new_bytes)
f.close()
log.info('Saved %s.', bytestring.bytestring(bytes_saved)) log.info('Saved %s.', bytestring.bytestring(bytes_saved))
log.info('Remaining are %s.', bytestring.bytestring(remaining_size)) log.info('Remaining are %s.', bytestring.bytestring(remaining_size))
@ -86,7 +83,7 @@ def main(argv):
parser.add_argument('patterns', nargs='+', default={'*.jpg', '*.jpeg'}) parser.add_argument('patterns', nargs='+', default={'*.jpg', '*.jpeg'})
parser.add_argument('--quality', type=int, default=80) parser.add_argument('--quality', type=int, default=80)
parser.add_argument('--filesize', type=str, default=None) parser.add_argument('--filesize', type=bytestring.parsebytes, default=None)
parser.add_argument('--recurse', action='store_true') parser.add_argument('--recurse', action='store_true')
parser.set_defaults(func=rejpg_argparse) parser.set_defaults(func=rejpg_argparse)