Skip to content

jobevers/diagonal-crop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

diagonal-crop

Diagonally crop an image using python and pillow

Usage

Alt Text

The black box is the area that we want to crop. It is a 150x200 rectangle rotated 45 degrees. The red dot is the base, at (200, 250).

This code

import math
from PIL import Image
import diagonal_crop

im = Image.open('media/lenna.png')
angle = math.pi / 4
base = (200, 250)
height = 150
width = 200
cropped_im = diagonal_crop.crop(im, base, angle, height, width)

produces this image:

Alt Text

Methodology

Making the crop is relatively simple; at a high level the image is rotated and then cropped.

For large images rotation can be expensive. So first, the image is cropped down to the bounding box of the target area. The bounding box is shown in white:

Alt Text

And here is the result:

Alt Text

The new crop area is calculated:

Alt Text

The image is rotated:

Alt Text

And, the crop area recalculated:

Alt Text

This can then be cropped, giving the final result:

Alt Text

About

Diagonally crop an image using python and pillow

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages