How to rotate an image

How to rotate an image

Rotate and flip an image

         You can rotate an image to a fixed angle of 90/180/270-degree with or without a flip. Use RotateFlip method and specify `ROTATE_270_FLIP_NONE` type parameter to flip on 270 degrees without flip. Python code example:

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

         Full list with all available rotate types you can find in the Rotate and flip types parameters descriptions.

Rotate an image to the arbitrary angle

         If you want to rotate an image to arbitrary choice angle, you can use the rotate method with specific rotation angle. The angle value can be positive if a rotation is clockwise or negative for the opposite direction. The size of the original image might be changed after rotating the image, so you have to specify `Resize proportionally` boolean parameter to indicate should the resulting image resized proportionally or not. In case you want to preserve the image size after rotating, you need to specify the background color to fill the empty corners of the rotated image.

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