Created
January 9, 2022 00:43
-
-
Save Palisand/9b60a79d491e94548130efcebcb56642 to your computer and use it in GitHub Desktop.
An old convert_to_jpg python script written fresh outta college
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
convert_to_jpg | |
Converts image formats that browsers don't commonly support (TIFF, BMP, SGI, RGB, etc.) | |
to JPGs and stores them in the same directory if an output location is not specified. | |
Please ignore the following warning: | |
[swscaler @ 0x7fdabb825e00] deprecated pixel format used, make sure you did set range correctly | |
Usage: | |
convert_to_jpg.py <input_file_or_directory> [-o=<output_directory>] [-q=<image_quality>] | |
convert_to_jpg.py (-h | --help) | |
Options: | |
-h --help Show this screen | |
-o=<output_directory> Specify output directory for converted file(s) (defaults to input location) | |
-q=<image_quality> Specify the image quality (1 - 31, highest to lowest) [default: 1] | |
""" | |
import os | |
import sys | |
import subprocess | |
from docopt import docopt | |
FILE_TYPES = (".TIF", ".TIFF", ".BMP", ".SGI", ".RGB") | |
QUALITY_MIN = 1 | |
QUALITY_MAX = 31 | |
def convert_file(fpath, outdir, quality): | |
""" | |
Converts a supported image file to a JPG and stores the file in the same directory or specified directory | |
:param fpath: the path of the file to be converted | |
:param outdir: an existing output directory | |
:return: None | |
""" | |
if fpath.upper().endswith(FILE_TYPES): | |
filename = os.path.splitext(os.path.basename(fpath))[0] + '.jpg' | |
fout = outdir + '/' + filename if outdir else os.path.dirname(fpath) + '/' + filename | |
subprocess.call(['ffmpeg', '-i', fpath, '-q', quality, '-pix_fmt', 'yuvj422p', fout]) | |
else: | |
sys.exit(fpath + ": File type not supported") | |
def convert_dir(dir_, outdir, quality): | |
""" | |
Converts any supported image files found within a specified directory to JPGs | |
and stores the files in the same directory or specified directory | |
:param dir_: the path of the directory containing files to be converted | |
:param outdir: an existing output directory | |
:return: None | |
""" | |
files = [dir_ + '/' + f for f in os.listdir(dir_) if f.upper().endswith(FILE_TYPES)] | |
if files: | |
for f in files: | |
filename = os.path.splitext(os.path.basename(f))[0] + '.jpg' | |
fout = outdir + '/' + filename if outdir else dir_ + '/' + filename | |
subprocess.call(['ffmpeg', '-i', f, '-q', quality, '-pix_fmt', 'yuvj422p', fout]) | |
else: | |
sys.exit(dir_ + ": No supported file types found") | |
def main(dir_or_file, outdir, quality): | |
if os.path.isfile(dir_or_file): | |
convert_file(dir_or_file, outdir, quality) | |
elif os.path.isdir(dir_or_file): | |
convert_dir(dir_or_file, outdir, quality) | |
else: | |
sys.exit("Unknown Error") | |
if __name__ == "__main__": | |
args = docopt(__doc__) | |
in_, out_, qlty_ = args["<input_file_or_directory>"], args["-o"], args["-q"] | |
in_ = os.path.abspath(in_) | |
if not os.path.exists(in_): | |
sys.exit(in_ + ": No such file or directory") | |
if out_: | |
out_ = os.path.abspath(out_) | |
if not os.path.exists(out_) or not os.path.isdir(out_): | |
sys.exit(out_ + ": No such directory") | |
if int(qlty_) > QUALITY_MAX: | |
print qlty_ + " exceeds maximum quality, using " + str(QUALITY_MAX) | |
qlty_ = str(QUALITY_MAX) | |
elif int(qlty_) < QUALITY_MIN: | |
print qlty_ + " falls below minimum quality, using " + str(QUALITY_MIN) | |
qlty_ = str(QUALITY_MIN) | |
main(in_, out_, qlty_) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment