Build a Command Line Tool to Resize and Change Formats of Images Using Python | by Arjun M S | Aug, 2022

A look at the Pixage tool

1*WVsgLM TtOABYMgYd1Lozw

Image size limit is one of the worst parts when we are uploading some files to any website. There are many free websites that allow you to resize the images for free but we can’t afford to lose our privacy by uploading the images to any websites.

As a part of a hackathon hosted by TinkerHub Foundation. Ajmal Mohad and I have built a Python CLI tool for resizing and changing the formats of images.

Pixage is a command line image format tool using which we can use to convert image formats, resize images, and other optional pre-processing. It supports all the common formats including PNG, JPEG, and JPG. Pixage provides an easy interface to customize various parameters like resolution, compression, etc.

  1. Enlarge and reduce image resolution
  2. Convert image formats
  1. colorama==0.4.4
  2. Pillow==9.0.1
  3. typer==0.4.0

First, you need to import all the basic packages and initialize them

Since this is a CLI tool we need to create a function that will act as arguments while running the script

For example,

We are creating a function to change the image format from JPG/JPEG to PNG

Similarly, we are creating multiple functions which will act as arguments while executing the script in the terminal.

  1. PNG to JPG

2. Reduce the dimensions of the Image

3. Enlarge the dimensions of the Image

4. Help Command

Now you must be wondering what the options() function will be doing in the previous code blocks.

So, now let’s define the options function:

Finally, let’s define the main() function:

The if condition will check whether an argument is passed while executing the script:

Open the terminal and change the location to the folder with the Python file.

Run the following command to see the magic: topng /home/arjun-ms/Documents/Pixage/ reduce /home/arjun-ms/Documents/Pixage/cat.jpeg 2

News Credit

%d bloggers like this: