Welcome, guest | Sign In | My Account | Store | Cart

Converts an image from one format to another using PIL.

Python, 17 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Image Converter
# Supported Formats:
# http://effbot.org/imagingbook/formats.htm
from PIL import Image
import sys, os
if len(sys.argv) != 3:
    print "USAGE:"
    print "[python] ImageConverter.py InputImageFilePath OutputImageFilePath"
    print "Use quotes if file paths/names contain spaces!"
    os._exit(1) # sys.exit()
InputImageFilePath = sys.argv[1]
OutputImageFilePath = sys.argv[2]
imageFormat = ((os.path.splitext(OutputImageFilePath)[1])[1 : ]).upper()
if imageFormat == "JPG": imageFormat = "JPEG"
if imageFormat == "TIF": imageFormat = "TIFF"
image = Image.open(InputImageFilePath)
image.save(OutputImageFilePath, imageFormat)