Skip to content

Instantly share code, notes, and snippets.

@wakhub
Created September 27, 2013 09:46
Show Gist options
  • Save wakhub/6726307 to your computer and use it in GitHub Desktop.
Save wakhub/6726307 to your computer and use it in GitHub Desktop.
Image converter script for iPhone app and Android app
# http://stackoverflow.com/questions/9166400/convert-rgba-png-to-rgb-with-pil
# http://stackoverflow.com/questions/11484204/python-invert-image-with-transparent-background-pil-gimp
import argparse
import StringIO
import Image
import ImageOps
class Command(object):
def __init__(self):
parser = argparse.ArgumentParser(description="Image processing")
parser.add_argument("-i", "--input",
help="Input file",
required=True)
parser.add_argument("-o", "--output",
help=("Onput file."
" If you don't set -o parameter,"
" It outputs png image data"))
parser.add_argument("-v", "--invert",
help="Invert image color",
action="store_true")
parser.add_argument("--width",
help="Change width",
type=int)
parser.add_argument("--height",
help="Change height",
type=int)
args = parser.parse_args()
image = Image.open(args.input).convert("RGBA")
(width, height) = image.size
if args.invert:
image = self.invert(image)
if args.width:
width = args.width
if args.height:
height = args.height
image = image.resize((width, height), Image.ANTIALIAS)
if args.output:
image.save(args.output)
else:
output = StringIO.StringIO()
image.save(output, "PNG")
image_value = output.getvalue()
output.close()
print image_value
def invert(self, image):
(r, g, b, a) = image.split()
def f(image):
return image.point(lambda p: 255 - p)
(r, g, b) = [f(i) for i in (r, g, b)]
return Image.merge(image.mode, (r, g, b, a))
if __name__ == "__main__":
Command()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment