How to Create Practical Command-Line Applications in Python: Image Resizer, Password Generator, and Currency Converter
Command line applications (CLIs) are a time-honored and versatile method of interacting with applications, providing a robust and efficient interface for users.
They offer a unique blend of power, speed, and flexibility, making them an indispensable tool in a developer's arsenal.
In this tutorial, we will delve into the creation of three highly useful command-line applications using Python, a popular and versatile programming language.
Our journey will begin with the development of an image resizer. Next, we will turn our attention to creating a password generator. Finally, we will round out our tutorial with the creation of a currency converter.
Whether you're a seasoned developer or a beginner just starting your coding journey, this tutorial offers valuable insights and practical skills for everyone.
Prerequisites
To follow along, you'll need Python 3.x installed on your system, along with the click library, which you can install with:
pip install click
Additionally, for each of the applications, additional libraries will be needed, but you will install them as we go through the applications and their code.
Image Resizer
We will get started with a simple script that resizes images using the Python Imaging Library (PIL) and the Click library.
You need to have the Python Imaging Library (PIL) installed in your Python environment. If it's not installed, you can install it using pip:
领英推荐
pip install pillow
Here's a basic example (you can create a file called image_resizer.py):
from PIL import Image
import click
@click.command()
@click.argument('input_file', type=click.Path(exists=True))
@click.argument('output_file', type=click.Path())
@click.option('--width', default=None, help='New width of the image.')
@click.option('--height', default=None, help='New height of the image.')
def resize_image(input_file, output_file, width, height):
image = Image.open(input_file)
if width and height:
new_size = (int(width), int(height))
elif width:
width = float(width)
new_size = (int(width), int(image.size[1] * width / image.size[0]))
elif height:
height = float(height)
new_size = (int(image.size[0] * height / image.size[1]), int(height))
else:
raise click.BadOptionUsage(message='Either --width or --height must be specified.', option_name='')
image = image.resize(new_size, Image.BICUBIC)
image.save(output_file)
click.echo(f'Image resized and saved as {output_file}')
if __name__ == '__main__':
resize_image()
This code defines a command-line interface (CLI) for resizing images using the Python Imaging Library (PIL) and the click library.
Here's a breakdown of the code:
You can run this script with different options to resize images. For example, you can resize an image to 200x200 pixels by running the following command:
python image_resizer.py input.png output.png --width 200 --height 200
You can also specify either the width or the height:
python image_resizer.py input.png output.png --width 200
python image_resizer.py input.png output.png --height 200
Password Generator
Now let's focus on creating a password generator command line application. You don't need any additional library other than the Click library.