-
Notifications
You must be signed in to change notification settings - Fork 9
/
heic2jpg.py
111 lines (94 loc) · 4.02 KB
/
heic2jpg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import os
import errno
import argparse
import time
import subprocess
import shutil
# ############################# Methods ###############################
def create_directory(directory):
''' Creates a directory if it does not already exist.
'''
try:
os.makedirs(directory)
except OSError as e:
if e.errno != errno.EEXIST:
raise
def vprint(verbose, *args, **kwargs):
''' Prints only if verbose is True.
'''
if verbose:
print(*args, **kwargs)
def convert(inp, outp, quality, rec=False, verbose=False):
''' Converts images from HEIC to JPG.
Args:
inp: Input file/directory.
outp: Output file/directory.
quality: JPG quality in [0, 100]. Default is 90.
rec: If True, subdirectories are parsed recursively, else
they are copied.
verbose: Verbosity. Default = False.
'''
if os.path.isfile(inp):
pre, ext = os.path.splitext(inp)
if ext.lower() in ['.heic', '.heif']:
if outp is None:
outp = pre + '.jpg'
assert not os.path.isdir(outp), "Both inp and outp should be files"
pre, ext = os.path.splitext(outp)
assert ext.lower() in ['.jpg']
dirname = os.path.dirname(outp)
create_directory(dirname)
vprint(verbose, 'Converting {} into {}'.format(inp, outp))
subprocess.call('heif-convert -q {} -f jpg -p "{}" -o "{}" "{}"'.format(quality, dirname, pre[len(dirname)+1:], inp), shell=True)
else:
outp = inp if outp is None else outp
vprint(verbose, 'Copying {} directly to {}'.format(inp, outp))
shutil.copy2(src=inp, dst=outp, follow_symlinks=True)
elif os.path.isdir(inp):
if outp is None:
outp = inp
else:
create_directory(outp)
for name in os.listdir(inp):
inpath = os.path.join(inp, name)
outpath = os.path.join(outp, name)
if os.path.isfile(inpath):
pre, ext = os.path.splitext(name)
outpath = os.path.join(outp, pre + '.jpg') if ext.lower() in ['.heic', '.heif'] else outpath
convert(inpath, outpath, quality, rec, verbose)
elif os.path.isdir(inpath) and rec:
convert(inpath, outpath, quality, rec, verbose)
elif os.path.isdir(inpath) and not rec:
shutil.copytree(src=inpath, dst=outpath, symlinks=True,
ignore_dangling_symlinks=True)
vprint(verbose, 'Converted directory {} into {}'.format(inp, outp))
# ############################# Entry Point ###############################
if __name__ == '__main__':
# Initial time
t_init = time.time()
# Parse arguments
parser = argparse.ArgumentParser(description='Convert HEIC image files \
to JPG format using libheif (see https://github.com/NeverMendel/heif-convert).')
parser.add_argument('-d', '--data',
help='Input file/directory.')
parser.add_argument('-o', '--out',
help='Output file/directory. Default: Same as \
input directory/file with .jpg extension.',
default=None)
parser.add_argument('-rec', '--recursive',
help='Recursively process subdirectories if input \
is a directory.',
action='store_true')
parser.add_argument('-v', '--verbose',
help='Increase verbosity.',
action='store_true')
parser.add_argument('-q', '--quality',
help='Quality of converted file (integer in [0, 100])',
type=int, default=90)
args = parser.parse_args()
# Convert files from HEIC to JPG
convert(args.data, args.out, quality=args.quality, rec=args.recursive,
verbose=args.verbose)
# Final time
t_final = time.time()
print('Progam finished in {} secs.'.format(t_final - t_init))