Converting Images to other File Types — Small Python projects #3
Images can be saved on computers in many different file types. Some examples are: PNG, JPG or ICO.

Different scenarios might require different file types. So today we will write a simple Python program that can convert a file to a specified image type.
This article is part of a series about small Python projects, you can find the previous episode here:
Introducing our test subject
As a test we will try to convert the following PNG image good_app.png to an image of type ICO:

An ICO file can be used as an icon for a desktop application for example.
To learn how to transform a Python file into a desktop application read:
Using the Pillow package
To be able to easily work with images we will use the Pillow package. After installing Pillow we can easily import the Image class like this: from PIL import Image
The pip install command is: pip install Pillow
To learn more about Python packages and how to install them you can read my article:
Writing the code
The first step is to open the image in Python by passing the filepath to the open method of the Image class:
image = Image.open('In/'+filename)
We will immediately save the opened image without making any changes to it. But first we need to determine the new filename.
The new filename can be build by removing the file extension and adding a new one:
new_file_name = filename.split('.')[0]+'.'+type
Based on the file extension of the new file, the image will be formatted correctly in the new file type when saved:
image.save('Out/'+new_file_name)
Project Setup
Maybe you noticed the directories (file folders) used in the file paths.
The project folder has the Python file and 2 directories. The first directory is called In and is where the images are placed. And the second is called Out, which is where the script will save the converted images.
Converting our code to a function
We will make a function called convert_image_type that takes a filename and type as arguments.
And we include a try-except statement to notify us in case something goes wrong. The except clause is only carried out when an error occurs in the try clause.
from PIL import Image
def convert_image_type(filename,type):
try:
image = Image.open('In/'+filename)
new_file_name = filename.split('.')[0]+'.'+type
image.save('Out/'+new_file_name)
except Exception as e:
print('Something went wrong, check the filename and type extension:')
print(e)
print(filename,type)Test 1 Now we will test the function in a scenario that should work.
convert_image_type('good_app.png','ico') results in…
Success! An ico file appears in the out directory. That file was also successfully used when creating a desktop application.
If we pass a wrong or non-existing filename or filetype the function will print a message.
Test 2
convert_image_type('good_app.png','pico') results in:
Something went wrong, check the filename and type extension:
unknown file extension: .pico
good_app.png picoTest 3
convert_image_type('bad_app.png','ico') results in:
Something went wrong, check the filename and type extension:
[Errno 2] No such file or directory: 'In/bad_app.png'
bad_app.png icoThank you for reading!
You can get full access to all my posts by joining Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:
