Skip to content

Instantly share code, notes, and snippets.

@ls0f
Created November 13, 2015 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ls0f/e45efe64cb9395f5611b to your computer and use it in GitHub Desktop.
Save ls0f/e45efe64cb9395f5611b to your computer and use it in GitHub Desktop.
convert png to jpg
#!/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