Last active
March 5, 2023 11:18
-
-
Save paiv/bd56e33e6325f774ae259c4be24803b8 to your computer and use it in GitHub Desktop.
TinyPNG command line
This file contains 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
#!/usr/bin/env python | |
import tinify # https://tinypng.com/developers/reference/python | |
import tomllib | |
from pathlib import Path | |
def main(input, output, background): | |
if output is None: | |
output = input | |
with open('.env', 'rb') as fp: | |
config = tomllib.load(fp) | |
tinify.key = config['tinify_api_key'] | |
s = tinify.from_file(input) | |
s = s.convert(type='image/png') | |
if background is not None: | |
s = s.transform(background=background) | |
return s.to_file(output) | |
if __name__ == '__main__': | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('input', type=Path, help='input PNG file') | |
parser.add_argument('-o', '--output', type=Path, help='output PNG file') | |
parser.add_argument('-b', '--background', metavar='COLOR', help='replace tranparency with background color') | |
args = parser.parse_args() | |
main( | |
input=args.input, | |
output=args.output, | |
background=args.background | |
) |
This file contains 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
tinify |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment