We frequently receive JPG images that do not meet our size requirements, so we need to resize them accordingly. In this article, we will learn how to resize JPG images in Python. This article provides a step-by-step guide and code snippets.
This article covers the following topics:
- Python API to resize JPG images
- Resize JPG images
- JPG image resizer using resize type
- Resize the JPG image proportionally
- Try JPG image resizing online
- Free learning resources
Resize Images in Python - Free Download
We will use Aspose.Imaging for Python to resize images programmatically. It provides a powerful image processing API to manipulate various types of raster and vector images. It enables developers to easily create, load, manipulate, convert, and compress images or photos in the supported file formats.
Please download the package or install the API from PyPI using the following pip command in the console:
PM> pip install aspose-imaging-python-net
Resize JPG Images in Python
We can easily resize a JPG image in Python by following the steps below:
- Load the image file using the Image class.
- After that, resize the image using the Image.resize(int, int) method.
- Finally, call the Image.save(string) method to save the resized image at the specified file path.
The following code sample shows how to resize an image in Python.
Resize JPG Images with a Resize Type
We can also specify resize type while resizing JPG images. For this purpose, we just need to specify ResizeType as an argument to the Image.resize(int, int, ResizeType) method. The ResizeType enum specifies resizing techniques that we can use as per the requirements. The complete list of supported resizing types is available here.
The following code sample shows how to resize an image with a resize type in Python.
Proportional Image Resizing in Python
Resizing images by specifying height and width can lead to a shrunken or scaled image. Consequently, manual calculation of the aspect ratio is necessary. However, any errors in the aspect ratio calculation may result in a stretched image. To circumvent such issues, proportional resizing can be employed. The following steps outline how to achieve this:
- Load the image file using the Image class.
- Specify the new height and width of the image.
- Calculate the proportion values using the Image.resize_width_proportionally(int) and Image.resize_height_proportionally(int) methods.
- Finally, call the Image.save(string) method to save the resized image at the specified file path.
The following code sample shows how to resize an image proportionally in Python.
In case you want to use any particular resize type while resizing images proportionally, use the overload methods Image.resize_width_proportionally(int, ResizeType) and Image.resize_height_proportionally(int, ResizeType).
Resize Images with a Free License
You can get a free temporary license to resize images without evaluation limitations.
Resize JPG Image Online
You may also resize JPG images online using this free image resizer web app, which was developed using this API.
JPG Image Resizer – Learning Resources
Besides resizing JPG images, learn more about creating, manipulating, and converting images and photos, and explore various other features using the resources below:
Conclusion
In this article, we have learned how to resize JPG images programmatically in Python. The code samples provided in this article have demonstrated how to perform simple resizing of images or resizing using a particular type. Also, you can download the complete package of source code samples from GitHub. In case of any queries, you can contact us on our free support forum.