How to resize an image

How to resize an image

Resize with resizing type

         To resize an image, load your image with method Load of the Python class Image. Then you can apply the Resize method with specifying height and width (`300x300` px) and selecting the resizing type `LANCZOS_RESAMPLE`.

Example Python code:

from aspose.imaging import Image, ResizeType
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
with Image.load(os.path.join(data_dir, "template.jpg")) as image:
image.resize(300, 300, ResizeType.LANCZOS_RESAMPLE)
image.save(os.path.join(data_dir, "result.jpg"))
if delete_output:
os.remove(os.path.join(data_dir, "result.jpg"))

         Several resize types are available for resizing photos. Please refer to the table with descriptions of Resize type parameters.

Proportional resizing

         Also, you can use proportional resizing and specify only desired picture width or height:

from aspose.imaging import Image
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
# Load an image from disk
with Image.load(os.path.join(data_dir, "template.jpg")) as image:
if not image.is_cached:
image.cache_data()
# Specifying width and height
new_width = image.width // 2
image.resize_width_proportionally(new_width)
new_height = image.height // 2
image.resize_height_proportionally(new_height)
image.save(os.path.join(data_dir, "result.png"))
if delete_output:
os.remove(os.path.join(data_dir, "result.png"))