Skip to content

Instantly share code, notes, and snippets.

@robmurrer
Created June 9, 2011 16:28
Show Gist options
  • Select an option

  • Save robmurrer/1017113 to your computer and use it in GitHub Desktop.

Select an option

Save robmurrer/1017113 to your computer and use it in GitHub Desktop.
RotoZoom for Django ImageKit
import math
from imagekit.lib import *
class RotoZoom(processors.ImageProcessor):
"""
Rotates image with the ability to scale change and apply offsets
angle - degrees counter-clockwise.
scale - defaults to no zoom (1) >1 zooms-in <1 zooms-out
left - left offset in pixels >0 moves right <0 moves left
top - top offset in pixels >0 moves up <0 moves down
"""
#defaults
angle = 0
scale = 1
left = 0
top = 0
@classmethod
def process(cls, img, fmt, obj):
#convert angle of rotation to radians
rad = -cls.angle * math.pi / 180
#get original image dimensions
original = img.getbbox()
#rotate with a resize (expand=True) so original image is whole inside new image
img = img.rotate(cls.angle, resample=3, expand=True)
#figure out the Y Position of the left-top corner of the original image
top_crop = (original[0] - original[2]) * math.sin(rad)
#apply top_crop
#apply the left/top offset translation
#apply the scale change
img = img.crop((
int((-cls.left) / cls.scale),
int((top_crop - cls.top) / cls.scale),
int((original[2] - cls.left) / cls.scale),
int((original[3] + top_crop - cls.top) / cls.scale),
))
return img, fmt
class RotateThumb(RotoZoom):
#produces this: https://skitch.com/robmurrer/fdh9k/rob-murrer
angle = 15
scale = 1.4
left = -190
top = -90
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment