2013-04-21 10:38:36 +04:00
|
|
|
#!/usr/bin/env python
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# The Python Imaging Library.
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# convert image files
|
|
|
|
#
|
|
|
|
# History:
|
|
|
|
# 0.1 96-04-20 fl Created
|
|
|
|
# 0.2 96-10-04 fl Use draft mode when converting images
|
|
|
|
# 0.3 96-12-30 fl Optimize output (PNG, JPEG)
|
|
|
|
# 0.4 97-01-18 fl Made optimize an option (PNG, JPEG)
|
|
|
|
# 0.5 98-12-30 fl Fixed -f option (from Anthony Baxter)
|
|
|
|
#
|
|
|
|
|
2012-10-16 06:27:35 +04:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2015-04-24 02:41:33 +03:00
|
|
|
import getopt
|
|
|
|
import string
|
|
|
|
import sys
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
2015-04-24 02:41:33 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def usage():
|
2012-10-16 06:27:35 +04:00
|
|
|
print("PIL Convert 0.5/1998-12-30 -- convert image files")
|
|
|
|
print("Usage: pilconvert [option] infile outfile")
|
|
|
|
print()
|
|
|
|
print("Options:")
|
|
|
|
print()
|
|
|
|
print(" -c <format> convert to format (default is given by extension)")
|
|
|
|
print()
|
|
|
|
print(" -g convert to greyscale")
|
|
|
|
print(" -p convert to palette image (using standard palette)")
|
|
|
|
print(" -r convert to rgb")
|
|
|
|
print()
|
|
|
|
print(" -o optimize output (trade speed for size)")
|
|
|
|
print(" -q <value> set compression quality (0-100, JPEG only)")
|
|
|
|
print()
|
|
|
|
print(" -f list supported file formats")
|
2010-07-31 06:52:47 +04:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if len(sys.argv) == 1:
|
|
|
|
usage()
|
|
|
|
|
|
|
|
try:
|
|
|
|
opt, argv = getopt.getopt(sys.argv[1:], "c:dfgopq:r")
|
2012-10-11 07:52:53 +04:00
|
|
|
except getopt.error as v:
|
2012-10-16 06:27:35 +04:00
|
|
|
print(v)
|
2010-07-31 06:52:47 +04:00
|
|
|
sys.exit(1)
|
|
|
|
|
2015-04-02 11:45:24 +03:00
|
|
|
output_format = None
|
2010-07-31 06:52:47 +04:00
|
|
|
convert = None
|
|
|
|
|
2015-04-24 02:41:33 +03:00
|
|
|
options = {}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
for o, a in opt:
|
|
|
|
|
|
|
|
if o == "-f":
|
|
|
|
Image.init()
|
2012-10-17 07:39:56 +04:00
|
|
|
id = sorted(Image.ID)
|
2012-10-16 06:27:35 +04:00
|
|
|
print("Supported formats (* indicates output format):")
|
2010-07-31 06:52:47 +04:00
|
|
|
for i in id:
|
2012-10-16 01:18:27 +04:00
|
|
|
if i in Image.SAVE:
|
2012-10-16 06:27:35 +04:00
|
|
|
print(i+"*", end=' ')
|
2010-07-31 06:52:47 +04:00
|
|
|
else:
|
2012-10-16 06:27:35 +04:00
|
|
|
print(i, end=' ')
|
2010-07-31 06:52:47 +04:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
elif o == "-c":
|
2015-04-02 11:45:24 +03:00
|
|
|
output_format = a
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
if o == "-g":
|
|
|
|
convert = "L"
|
|
|
|
elif o == "-p":
|
|
|
|
convert = "P"
|
|
|
|
elif o == "-r":
|
|
|
|
convert = "RGB"
|
|
|
|
|
|
|
|
elif o == "-o":
|
|
|
|
options["optimize"] = 1
|
|
|
|
elif o == "-q":
|
|
|
|
options["quality"] = string.atoi(a)
|
|
|
|
|
|
|
|
if len(argv) != 2:
|
|
|
|
usage()
|
|
|
|
|
|
|
|
try:
|
|
|
|
im = Image.open(argv[0])
|
|
|
|
if convert and im.mode != convert:
|
|
|
|
im.draft(convert, im.size)
|
|
|
|
im = im.convert(convert)
|
2015-04-02 11:45:24 +03:00
|
|
|
if output_format:
|
|
|
|
im.save(argv[1], output_format, **options)
|
2010-07-31 06:52:47 +04:00
|
|
|
else:
|
2012-10-21 01:05:13 +04:00
|
|
|
im.save(argv[1], **options)
|
2010-07-31 06:52:47 +04:00
|
|
|
except:
|
2012-10-16 06:27:35 +04:00
|
|
|
print("cannot convert image", end=' ')
|
|
|
|
print("(%s:%s)" % (sys.exc_info()[0], sys.exc_info()[1]))
|