pilconvert.py :  » GUI » Python-Image-Library » Imaging-1.1.7 » Scripts » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » GUI » Python Image Library 
Python Image Library » Imaging 1.1.7 » Scripts » pilconvert.py
#! /usr/local/bin/python
#
# 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)
#

import site
import getopt, string, sys

from PIL import Image

def usage():
    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"
    sys.exit(1)

if len(sys.argv) == 1:
    usage()

try:
    opt, argv = getopt.getopt(sys.argv[1:], "c:dfgopq:r")
except getopt.error, v:
    print v
    sys.exit(1)

format = None
convert = None

options = { }

for o, a in opt:

    if o == "-f":
        Image.init()
        id = Image.ID[:]
        id.sort()
        print "Supported formats (* indicates output format):"
        for i in id:
            if Image.SAVE.has_key(i):
                print i+"*",
            else:
                print i,
        sys.exit(1)

    elif o == "-c":
        format = a

    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)
    if format:
        apply(im.save, (argv[1], format), options)
    else:
        apply(im.save, (argv[1],), options)
except:
    print "cannot convert image",
    print "(%s:%s)" % (sys.exc_type, sys.exc_value)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.