Skip to content

Instantly share code, notes, and snippets.

@futursolo
Last active March 25, 2017 13:49
Show Gist options
  • Save futursolo/d89bbd145e7e443d01343f513502c509 to your computer and use it in GitHub Desktop.
Save futursolo/d89bbd145e7e443d01343f513502c509 to your computer and use it in GitHub Desktop.
ImgTrans
#!/usr/bin/env python3
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org>
import sys
try:
from PIL import Image
except ImportError as e:
sys.exit(
"Pillow is not installed, "
"please install with `pip install Pillow -U`.")
import argparse
import os
import io
def parse_args(args):
parser = argparse.ArgumentParser(
description="ImgTrans -- Convert Image Format.")
parser.add_argument(
"input", help="Path of the input file. Put '_STDIN_' to use PIPE.")
parser.add_argument(
"output",
help="Path of the output file. Put '_STDOUT_.[FORMAT]' to use PIPE.")
parser.add_argument(
"--thumbnail",
help="Make the output file a thumbnail of the input file.",
action="store_true")
return parser.parse_args(args)
def main(args=sys.argv[1:]):
args = parse_args(args)
if args.input == "_STDIN_":
input_buf = io.BytesIO(sys.stdin.buffer.read())
img = Image.open(input_buf)
else:
img = Image.open(args.input)
if args.thumbnail:
img.thumbnail((512, 512))
plain_name, output_ext = os.path.splitext(args.output)
output_ext = output_ext[1:]
if plain_name == "_STDOUT_":
output_place = io.BytesIO()
else:
output_place = args.output
if output_ext.lower() == "gif":
img.save(
output_place, format=output_ext, save_all=True, duration=1000)
else:
img.save(output_place, format=output_ext)
if plain_name == "_STDOUT_":
output_place.seek(0, io.SEEK_SET)
sys.stdout.buffer.write(output_place.read())
sys.stdout.buffer.flush()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment