Kasper Bielecki

From the class Code as a Liberal Art · Spring 2022

Image Format Converter

Unit 1, Tutorial 3

arguments.py

import sys
from PIL import Image
import re

print( sys.argv )

print( len(sys.argv) )

if len(sys.argv) != 2:
    exit("This command requires one argument: the name of an image file")

img = Image.open( sys.argv[1] )

print("You typed the filename: " + sys.argv[1] )
print("This is a " + img.format)
print(img.format_description)
print("Size: " + str(img.size) )

base_filename = re.sub("\.[a-zA-Z0-9]*", "", sys.argv[1])

img.save( base_filename + ".jpg" )
img.save( base_filename + ".gif" )
img.save( base_filename + ".tiff" )
img.save( base_filename + ".png" )
Tutorial 3 image