mirror of
https://github.com/Hopiu/django-imagekit.git
synced 2026-03-17 05:40:25 +00:00
178 lines
6.1 KiB
Python
178 lines
6.1 KiB
Python
""" Imagekit Image "ImageProcessors"
|
|
|
|
A processor defines a set of class variables (optional) and a class method
|
|
named "process" which processes the supplied image using the class properties
|
|
as settings. The process method can be overridden as well allowing user to
|
|
define their own effects/processes entirely.
|
|
|
|
"""
|
|
from imagekit.lib import Image, ImageEnhance, ImageColor
|
|
|
|
|
|
class ImageProcessor(object):
|
|
""" Base image processor class """
|
|
|
|
@classmethod
|
|
def process(cls, img, fmt, obj):
|
|
return img, fmt
|
|
|
|
|
|
class Adjustment(ImageProcessor):
|
|
color = 1.0
|
|
brightness = 1.0
|
|
contrast = 1.0
|
|
sharpness = 1.0
|
|
|
|
@classmethod
|
|
def process(cls, img, fmt, obj):
|
|
img = img.convert('RGB')
|
|
for name in ['Color', 'Brightness', 'Contrast', 'Sharpness']:
|
|
factor = getattr(cls, name.lower())
|
|
if factor != 1.0:
|
|
try:
|
|
img = getattr(ImageEnhance, name)(img).enhance(factor)
|
|
except ValueError:
|
|
pass
|
|
return img, fmt
|
|
|
|
|
|
class Format(ImageProcessor):
|
|
format = 'JPEG'
|
|
extension = 'jpg'
|
|
|
|
@classmethod
|
|
def process(cls, img, fmt, obj):
|
|
return img, cls.format
|
|
|
|
|
|
class Reflection(ImageProcessor):
|
|
background_color = '#FFFFFF'
|
|
size = 0.0
|
|
opacity = 0.6
|
|
|
|
@classmethod
|
|
def process(cls, img, fmt, obj):
|
|
# convert bgcolor string to rgb value
|
|
background_color = ImageColor.getrgb(cls.background_color)
|
|
# handle palleted images
|
|
img = img.convert('RGB')
|
|
# copy orignial image and flip the orientation
|
|
reflection = img.copy().transpose(Image.FLIP_TOP_BOTTOM)
|
|
# create a new image filled with the bgcolor the same size
|
|
background = Image.new("RGB", img.size, background_color)
|
|
# calculate our alpha mask
|
|
start = int(255 - (255 * cls.opacity)) # The start of our gradient
|
|
steps = int(255 * cls.size) # The number of intermedite values
|
|
increment = (255 - start) / float(steps)
|
|
mask = Image.new('L', (1, 255))
|
|
for y in range(255):
|
|
if y < steps:
|
|
val = int(y * increment + start)
|
|
else:
|
|
val = 255
|
|
mask.putpixel((0, y), val)
|
|
alpha_mask = mask.resize(img.size)
|
|
# merge the reflection onto our background color using the alpha mask
|
|
reflection = Image.composite(background, reflection, alpha_mask)
|
|
# crop the reflection
|
|
reflection_height = int(img.size[1] * cls.size)
|
|
reflection = reflection.crop((0, 0, img.size[0], reflection_height))
|
|
# create new image sized to hold both the original image and the reflection
|
|
composite = Image.new("RGB", (img.size[0], img.size[1]+reflection_height), background_color)
|
|
# paste the orignal image and the reflection into the composite image
|
|
composite.paste(img, (0, 0))
|
|
composite.paste(reflection, (0, img.size[1]))
|
|
# Save the file as a JPEG
|
|
fmt = 'JPEG'
|
|
# return the image complete with reflection effect
|
|
return composite, fmt
|
|
|
|
|
|
class Resize(ImageProcessor):
|
|
width = None
|
|
height = None
|
|
crop = False
|
|
upscale = False
|
|
|
|
@classmethod
|
|
def process(cls, img, fmt, obj):
|
|
cur_width, cur_height = img.size
|
|
if cls.crop:
|
|
crop_horz = getattr(obj, obj._ik.crop_horz_field, 1)
|
|
crop_vert = getattr(obj, obj._ik.crop_vert_field, 1)
|
|
ratio = max(float(cls.width) / cur_width, float(cls.height) / cur_height)
|
|
resize_x, resize_y = ((cur_width * ratio), (cur_height * ratio))
|
|
crop_x, crop_y = (abs(cls.width - resize_x), abs(cls.height - resize_y))
|
|
x_diff, y_diff = (int(crop_x / 2), int(crop_y / 2))
|
|
box_left, box_right = {
|
|
0: (0, cls.width),
|
|
1: (int(x_diff), int(x_diff + cls.width)),
|
|
2: (int(crop_x), int(resize_x)),
|
|
}[crop_horz]
|
|
box_upper, box_lower = {
|
|
0: (0, cls.height),
|
|
1: (int(y_diff), int(y_diff + cls.height)),
|
|
2: (int(crop_y), int(resize_y)),
|
|
}[crop_vert]
|
|
box = (box_left, box_upper, box_right, box_lower)
|
|
img = img.resize((int(resize_x), int(resize_y)), Image.ANTIALIAS).crop(box)
|
|
else:
|
|
if not cls.width is None and not cls.height is None:
|
|
ratio = min(float(cls.width) / cur_width,
|
|
float(cls.height) / cur_height)
|
|
else:
|
|
if cls.width is None:
|
|
ratio = float(cls.height) / cur_height
|
|
else:
|
|
ratio = float(cls.width) / cur_width
|
|
new_dimensions = (int(round(cur_width * ratio)),
|
|
int(round(cur_height * ratio)))
|
|
if new_dimensions[0] > cur_width or \
|
|
new_dimensions[1] > cur_height:
|
|
if not cls.upscale:
|
|
return img, fmt
|
|
img = img.resize(new_dimensions, Image.ANTIALIAS)
|
|
return img, fmt
|
|
|
|
|
|
class Transpose(ImageProcessor):
|
|
""" Rotates or flips the image
|
|
|
|
Method should be one of the following strings:
|
|
- FLIP_LEFT RIGHT
|
|
- FLIP_TOP_BOTTOM
|
|
- ROTATE_90
|
|
- ROTATE_270
|
|
- ROTATE_180
|
|
- auto
|
|
|
|
If method is set to 'auto' the processor will attempt to rotate the image
|
|
according to the EXIF Orientation data.
|
|
|
|
"""
|
|
EXIF_ORIENTATION_STEPS = {
|
|
1: [],
|
|
2: ['FLIP_LEFT_RIGHT'],
|
|
3: ['ROTATE_180'],
|
|
4: ['FLIP_TOP_BOTTOM'],
|
|
5: ['ROTATE_270', 'FLIP_LEFT_RIGHT'],
|
|
6: ['ROTATE_270'],
|
|
7: ['ROTATE_90', 'FLIP_LEFT_RIGHT'],
|
|
8: ['ROTATE_90'],
|
|
}
|
|
|
|
method = 'auto'
|
|
|
|
@classmethod
|
|
def process(cls, img, fmt, obj):
|
|
if cls.method == 'auto':
|
|
try:
|
|
orientation = Image.open(obj._imgfield.file)._getexif()[0x0112]
|
|
ops = cls.EXIF_ORIENTATION_STEPS[orientation]
|
|
except:
|
|
ops = []
|
|
else:
|
|
ops = [cls.method]
|
|
for method in ops:
|
|
img = img.transpose(getattr(Image, method))
|
|
return img, fmt
|