Crop, Rotate and Resize Images

Cropping Raster Images

Image cropping usually refers to the removal of the outer parts of an image to help improve the framing. Cropping may also be used for to cut out some portion of an image to increase the focus on a particular area. From Aspose.Imaging 2.3.0, the API supports two different approaches to image cropping: by shifts and rectangle.

Cropping by Shifts

The RasterImage class provides an overloaded version of the crop method that accepts 4 integer values denoting Left, Right, Top & Bottom. Based on these four values, the crop method moves the image boundaries toward the center of the image while discarding the outer portion. The code snippet below demonstrates how to crop an image by shifts.

import aspose.pycore as aspycore
from aspose.imaging import RasterImage, Image, Rectangle
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 existing image into an instance of RasterImage class
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.jpg")), RasterImage) as raster_image:
# Before cropping, the image should be cached for better performance
if not raster_image.is_cached:
raster_image.cache_data()
# Define shift values for all four sides
left_shift = 10
right_shift = 10
top_shift = 10
bottom_shift = 10
# Based on the shift values, apply the cropping on image Crop method will shift the image bounds toward the center of image and save the results to disk
raster_image.crop(left_shift, right_shift, top_shift, bottom_shift)
raster_image.save(os.path.join(data_dir, "result.jpg"))
if delete_output:
os.remove(os.path.join(data_dir, "result.jpg"))

Cropping by Rectangle

The RasterImage class provides another overloaded version of the crop method that accepts an instance of the Rectangle class. You can cut out any portion of an image by providing the desired boundaries to the Rectangle object. The code snippet below demonstrates how to Crop any image by Rectangle.

import aspose.pycore as aspycore
from aspose.imaging import RasterImage, Image, Rectangle
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 existing image into an instance of RasterImage class
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.jpg")), RasterImage) as raster_image:
if not raster_image.is_cached:
raster_image.cache_data()
# Create an instance of Rectangle class with desired size, Perform the crop operation on object of Rectangle class and Save the results to disk
rectangle = Rectangle(20, 20, 20, 20)
raster_image.crop(rectangle)
raster_image.save(os.path.join(data_dir, "result.jpg"))
if delete_output:
os.remove(os.path.join(data_dir, "result.jpg"))

Cropping Vector Images

Image cropping usually refers to the removal of the outer parts of an image to help improve the framing. Aspose.Imaging supports cropping of vector images. Below we demonstrate cropping of svg image.

Crop svg Image

import aspose.pycore as aspycore
from aspose.imaging import Image, Rectangle
from aspose.imaging.fileformats.png import PngImage
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.svg")) as image:
image.save(os.path.join(data_dir, "result.png"))
with aspycore.as_of(Image.load(os.path.join(data_dir, "result.png")), PngImage) as image:
image.crop(Rectangle(10, 10, 10, 15))
print(image.width)
print(image.height)
image.save()
if delete_output:
os.remove(os.path.join(data_dir, "result.png"))

Rotate and Flip an Image

Aspose.Imaging for Python via .NET is an easy to use library because it provides simple methods to perform complex operations while encapsulating all ugly details. For instance, Aspose.Imaging for Python via .NET has provided RotateFlip method for it’s base class Image if an application requires rotating an image. Irrespective of the image format, the library can perform specific Rotate & Flip procedure on it.

Rotating an Image

The Image.RotateFlip method can be used to rotate the image by 90/180/270-degrees and flip the image horizontally or vertically. Image.RotateFlip method accepts a parameter of RotateFlipType that specifies the type of rotation and flip to apply to the image. The steps to perform Rotate & Flip are as simple as below,

  1. Load in image using the factory method load exposed by Image class.
  2. Call the Image.RotateFlip method while specifying the appropriate RotateFlipType.
  3. Save the results.

The following code example demonstrates how to set the RotateFlip property of an Image and the RotateFlipType enumeration.

from aspose.imaging import Image, RotateFlipType
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.rotate_flip(RotateFlipType.ROTATE_270_FLIP_NONE)
image.save(os.path.join(data_dir, "result.jpg"))
if delete_output:
os.remove(os.path.join(data_dir, "result.jpg"))
view raw rotate-image.py hosted with ❤ by GitHub

RotateFlipType Enumeration

Members of RotateFlipType enumeration and their details are available here.

Rotating an Image on a Specific Angle

From version 2.5.0, the Aspose.Imaging API has exposed the RasterImage.rotate method to facilitate its users who wish to rotate an image on a specific angle. Unlike the RasterImage.RotateFlip method, the RasterImage.rotate method accepts three parameters:

  1. Rotation angle: A float type parameter that specifies the rotation angle to which the image has to be rotated. A positive value rotates the image clockwise; a negative value performs an anticlockwise rotation.
  2. Resize proportionally: A Boolean type parameter specifies if the image size has to changed according to the rotated rectangle (corner points) projections. If set to false, the image dimensions would be untouched and only internal image contents are rotated.
  3. Background color: A Color type parameter specifies the color to be filled in the background of the rotated image.

The code snippet below demonstrates the usage of RasterImage.Rotate method.

import aspose.pycore as aspycore
from aspose.imaging import Image, Color, RasterImage
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 aspycore.as_of(Image.load(os.path.join(data_dir, "template.jpg")), RasterImage) as image:
# Before rotation, the image should be cached for better performance
if not image.is_cached:
image.cache_data()
# Perform the rotation on 20 degree while keeping the image size proportional with red background color and Save the result to a new file
image.rotate(20.0, True, Color.red)
image.save(os.path.join(data_dir, "result.jpg"))
if delete_output:
os.remove(os.path.join(data_dir, "result.jpg"))

From version 2.5.0, the Aspose.Imaging API has exposed the RasterImage.rotate method to facilitate its users who wish to rotate an image on a specific angle.

Resizing raster Images

This article demonstrates the usage of Aspose.Imaging for Python via .NET to perform Resize operation on raster image. Aspose.Imaging APIs have exposed efficient & easy to use methods to achieve this goal. Aspose.Imaging for Python via .NET has exposed the Resize method for the Image class that can be used to re-size existing images on the fly. There are two overloads of the resize method to suit the application needs.

Resizing Images : Simple Resizing

The steps to perform Resize are as simple as below,

  1. Load in image using the factory method load exposed by Image class.
  2. Call the Image.Resize method while specifying new Height & Width.
  3. Save the results.

The following code example demonstrates how to Resize an image.

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
with Image.load(os.path.join(data_dir, "template.jpg")) as image:
image.resize(300, 300)
image.save(os.path.join(data_dir, "result.jpg"))
if delete_output:
os.remove(os.path.join(data_dir, "result.jpg"))

Resizing Images : Resizing with ResizeType Enumeration

Aspose.Imaging API has exposed ResizeType enumeration that can be used with Image.resize to achieve desired results. Below provided code snippet demonstrates the usage of ResizeType enumeration, whereas the details of ResizeType enumeration members can be found at the bottom of this page.

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"))

If you intend to get quality result after applying the re-size then it is suggested that you should always use ResizeType.LANCZOS_RESAMPLE because it will output highly qualitative results but may work slower than ResizeType.NEAREST_NEIGHBOUR_RESAMPLE. On the other hand, ResizeType.NEAREST_NEIGHBOUR_RESAMPLE algorithm is specifically used for fast re-sizing while compromising on the image quality. This method may be useful for thumbnail generation in real time or similar processes where performance is required.

Resizing Images : ResizeType Enumeration

ResizeType determines the type of re-sizing to be performed on the image based on the selected filter.

Members of ResizeType Enumeration

Member Name Value Description
LEFT_TOP_TO_LEFT_TOP 0 Left top point of the new image will coincide with the left top point of the original image. Crop will occur if required.
RIGHT_TOP_TO_RIGHT_TOP 1 Right top point of the new image will coincide with the right top point of the original image. Crop will occur if required.
RIGHT_BOTTOM_TO_RIGHT_BOTTOM 2 Right bottom point of the new image will coincide with the right bottom point of the original image. Crop will occur if required.
LEFT_BOTTOM_TO_LEFT_BOTTOM 3 Left bottom point of the new image will coincide with the left bottom point of the original image. Crop will occur if required.
CENTER_TO_CENTER 4 Center of the new image will coincide with the center of the original image. Crop will occur if required.
LANCZOS_RESAMPLE 5 Resample using lancros algorithm using 7x7 mask.
NEAREST_NEIGHBOUR_RESAMPLE 6 Resample using nearest neighbour algorithm.

Resize webp image

Using Aspose.Imaging you can easizy resize webp image. Below we provide example of such convertion.

import aspose.pycore as aspycore
from aspose.imaging import Image, ResizeType
from aspose.imaging.fileformats.webp import WebPImage
from aspose.imaging.imageoptions import WebPOptions
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 aspycore.as_of(Image.load(os.path.join(data_dir, "template.webp")), WebPImage) as image:
image.resize(300, 450, ResizeType.HIGH_QUALITY_RESAMPLE)
options = WebPOptions()
options.quality = 50
options.lossless = False
image.save(os.path.join(data_dir, "result.webp"), options)
if delete_output:
os.remove(os.path.join(data_dir, "result.webp"))

Resizing vector Images

This article demonstrates the usage of Aspose.Imaging for Python via .NET to perform Resize operation on vector image. As example svg image resize provided.

Resize svg Image

from aspose.imaging import Image
from aspose.imaging.imageoptions import PngOptions, SvgRasterizationOptions
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.svg")) as image:
image.resize(image.width + 10, image.height + 15)
options = PngOptions()
options.vector_rasterization_options = SvgRasterizationOptions()
image.save(os.path.join(data_dir, "result.png"), options)
if delete_output:
os.remove(os.path.join(data_dir, "result.png"))

Resize Image Proportionally

You can resize images by passing new height & width values as parameters to the Image.Resize method but in that case you have to calculate the aspect ratio yourself. This is because when the width or height of an image is altered, the image is either scaled or shrinked to fill the new size . If the changes to the width and height of an image are not in proportion this can lead to stretched and distorted result. This article demonstrates the use of Aspose.Imaging for Python via .NET API to resize images by passing either new height or width while allowing the API to automatically calculate the other proportional value.

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"))

Resize Image Proportionally

Aspose.Imaging for Python via .NET has exposed the resize_width_proportionally and resize_height_proportionally methods for the Image class that can be used to re-size existing images on the fly while keeping the aspect ratio.

Simple Resizing

The following code example demonstrates the use of resize_width_proportionally and resize_height_proportionally methods.

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"))

Resizing with ResizeType Enumeration

Aspose.Imaging for Python via .NET API has also exposed overload versions of the resize_width_proportionally and resize_height_proportionally methods that can accept ResizeType as second parameter to achieve desired results. Below provided code snippet demonstrates the usage of ResizeType enumeration, whereas the details of ResizeType enumeration members can be found at the bottom of this page.

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
# 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 only height, width and ResizeType
new_width = image.width // 2
image.resize_width_proportionally(new_width, ResizeType.LANCZOS_RESAMPLE)
new_height = image.height // 2
image.resize_height_proportionally(new_height, ResizeType.NEAREST_NEIGHBOUR_RESAMPLE)
image.save(os.path.join(data_dir, "result.png"))
if delete_output:
os.remove(os.path.join(data_dir, "result.png"))

ResizeType Enumeration

ResizeType determines the type of re-sizing to be performed on the image based on the selected filter.

Members of ResizeType Enumeration

Member Name Value Description
LEFT_TOP_TO_LEFT_TOP 0 Left top point of the new image will coincide with the left top point of the original image. Crop will occur if required.
RIGHT_TOP_TO_RIGHT_TOP 1 Right top point of the new image will coincide with the right top point of the original image. Crop will occur if required.
RIGHT_BOTTOM_TO_RIGHT_BOTTOM 2 Right bottom point of the new image will coincide with the right bottom point of the original image. Crop will occur if required.
LEFT_BOTTOM_TO_LEFT_BOTTOM 3 Left bottom point of the new image will coincide with the left bottom point of the original image. Crop will occur if required.
CENTER_TO_CENTER 4 Center of the new image will coincide with the center of the original image. Crop will occur if required.
LANCZOS_RESAMPLE 5 Resample using lancros algorithm using 7x7 mask.
NEAREST_NEIGHBOUR_RESAMPLE 6 Resample using nearest neighbor algorithm.
If you intend to get quality result after applying the re-size then it is suggested that you should always use ResizeType.LanczosResample because it will output highly qualitative results but may work slower than ResizeType.NEAREST_NEIGHBOUR_RESAMPLE. On the other hand, ResizeType.NEAREST_NEIGHBOUR_RESAMPLE algorithm is specifically used for fast re-sizing while compromising on the image quality. This method may be useful for thumbnail generation in real time or similar processes where performance is required.