Created
November 13, 2015 03:27
-
-
Save ls0f/e45efe64cb9395f5611b to your computer and use it in GitHub Desktop.
convert png to jpg
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 | |
# http://stackoverflow.com/questions/9166400/convert-rgba-png-to-rgb-with-pil | |
# http://stackoverflow.com/questions/1962795/how-to-get-alpha-value-of-a-png-image-with-pil | |
import sys | |
from PIL import Image | |
def main(): | |
png = Image.open(sys.argv[1]) | |
png.load() | |
background = Image.new("RGB", png.size, (255, 255, 255)) | |
try: | |
background.paste(png, mask=png.split()[3]) | |
except IndexError: | |
background.paste(png, mask=png.convert('RGBA').split()[3]) | |
background.save(sys.argv[2], 'JPEG', quality=90) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment