Rewrite rejpg with argparse and preserve exif orientation.
This commit is contained in:
parent
3f858aefe7
commit
1eae1ae399
1 changed files with 62 additions and 30 deletions
62
rejpg.py
62
rejpg.py
|
@ -1,36 +1,56 @@
|
||||||
'''
|
'''
|
||||||
Recompress all jpg images in the current directory.
|
Recompress all jpg images in the current directory.
|
||||||
Add /r to do nested directories as well.
|
|
||||||
'''
|
'''
|
||||||
|
import argparse
|
||||||
from voussoirkit import bytestring
|
|
||||||
import io
|
import io
|
||||||
import os
|
import os
|
||||||
|
import PIL.ExifTags
|
||||||
import PIL.Image
|
import PIL.Image
|
||||||
import PIL.ImageFile
|
import PIL.ImageFile
|
||||||
import string
|
import string
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from voussoirkit import bytestring
|
||||||
|
|
||||||
PIL.ImageFile.LOAD_TRUNCATED_IMAGES = True
|
PIL.ImageFile.LOAD_TRUNCATED_IMAGES = True
|
||||||
|
|
||||||
if '/r' in sys.argv:
|
for (ORIENTATION_KEY, val) in PIL.ExifTags.TAGS.items():
|
||||||
|
if val == 'Orientation':
|
||||||
|
break
|
||||||
|
|
||||||
|
def rejpg_argparse(args):
|
||||||
|
if args.recurse:
|
||||||
from voussoirkit import spinal
|
from voussoirkit import spinal
|
||||||
walker = spinal.walk_generator()
|
walker = spinal.walk_generator()
|
||||||
files = list(walker)
|
files = list(walker)
|
||||||
|
else:
|
||||||
|
from voussoirkit import pathclass
|
||||||
|
files = pathclass.cwd().listdir()
|
||||||
|
files = [f for f in files if f.is_file]
|
||||||
|
|
||||||
|
files = [f for f in files if f.extension in ['.jpg', '.jpeg']]
|
||||||
files = [f.absolute_path for f in files]
|
files = [f.absolute_path for f in files]
|
||||||
|
|
||||||
else:
|
bytes_saved = 0
|
||||||
files = os.listdir()
|
remaining_size = 0
|
||||||
files = [f for f in files if os.path.isfile(f)]
|
for filename in files:
|
||||||
|
|
||||||
files = [f for f in files if any(ext in f.lower() for ext in ['.jpg', '.jpeg'])]
|
|
||||||
|
|
||||||
bytes_saved = 0
|
|
||||||
remaining_size = 0
|
|
||||||
for filename in files:
|
|
||||||
print(''.join(c for c in filename if c in string.printable))
|
print(''.join(c for c in filename if c in string.printable))
|
||||||
bytesio = io.BytesIO()
|
bytesio = io.BytesIO()
|
||||||
i = PIL.Image.open(filename)
|
i = PIL.Image.open(filename)
|
||||||
|
exif = i._getexif()
|
||||||
|
|
||||||
|
# Preserve orientation according to exif
|
||||||
|
# Thank you Scabbiaza
|
||||||
|
# https://stackoverflow.com/a/26928142
|
||||||
|
if exif is None:
|
||||||
|
pass
|
||||||
|
elif exif[ORIENTATION_KEY] == 3:
|
||||||
|
i = i.rotate(180, expand=True)
|
||||||
|
elif exif[ORIENTATION_KEY] == 6:
|
||||||
|
i = i.rotate(270, expand=True)
|
||||||
|
elif exif[ORIENTATION_KEY] == 8:
|
||||||
|
i = i.rotate(90, expand=True)
|
||||||
|
|
||||||
i.save(bytesio, format='jpeg', quality=80)
|
i.save(bytesio, format='jpeg', quality=80)
|
||||||
|
|
||||||
bytesio.seek(0)
|
bytesio.seek(0)
|
||||||
|
@ -44,5 +64,17 @@ for filename in files:
|
||||||
f.write(new_bytes)
|
f.write(new_bytes)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
print('Saved', bytestring.bytestring(bytes_saved))
|
print('Saved', bytestring.bytestring(bytes_saved))
|
||||||
print('Remaining are', bytestring.bytestring(remaining_size))
|
print('Remaining are', bytestring.bytestring(remaining_size))
|
||||||
|
|
||||||
|
def main(argv):
|
||||||
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
|
|
||||||
|
parser.add_argument('--recurse', dest='recurse', action='store_true')
|
||||||
|
parser.set_defaults(func=rejpg_argparse)
|
||||||
|
|
||||||
|
args = parser.parse_args(argv)
|
||||||
|
return args.func(args)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
raise SystemExit(main(sys.argv[1:]))
|
||||||
|
|
Loading…
Reference in a new issue