Skip to content

Instantly share code, notes, and snippets.

@paiv
Last active March 5, 2023 11:18
Show Gist options
  • Save paiv/bd56e33e6325f774ae259c4be24803b8 to your computer and use it in GitHub Desktop.
Save paiv/bd56e33e6325f774ae259c4be24803b8 to your computer and use it in GitHub Desktop.
TinyPNG command line
#!/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
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment