-
Notifications
You must be signed in to change notification settings - Fork 0
/
specs.py
87 lines (73 loc) · 2.19 KB
/
specs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# myapp/specs.py
from imagekit.specs import ImageSpec
from imagekit import processors
import os
from PIL import Image
class ExifRotate(processors.ImageProcessor):
"""Autorotates an image based on exif orientation"""
method={1:[],
2:[Image.FLIP_LEFT_RIGHT],
3:[Image.ROTATE_180],
4:[Image.FLIP_TOP_BOTTOM],
5:[Image.ROTATE_270, Image.FLIP_LEFT_RIGHT],
6:[Image.ROTATE_270],
7:[Image.ROTATE_90, Image.FLIP_LEFT_RIGHT],
8:[Image.ROTATE_90],
}
@classmethod
def process(cls, img, fmt, obj):
try:
if obj is not None:
orient = int(obj.EXIF['Image Orientation'].values[0])
else:
orient = img._getexif()[274]#Orientation tag
for step in cls.method[orient]:
img=img.transpose(step)
except AttributeError:
pass
return img, fmt
# first we define our thumbnail resize processor
class ResizeThumb(processors.Resize):
width = 120
height = 120
crop = True
class ResizeSmallThumb(processors.Resize):
width = 75
height = 75
crop = True
# now we define a display size resize processor
class ResizeDisplay(processors.Resize):
width = 600
height = 400
crop = False
# now lets create an adjustment processor to enhance the image at small sizes
class EnchanceThumb(processors.Adjustment):
contrast = 1.2
sharpness = 1.1
class Gray(processors.Adjustment):
color = 0.
class Preprocess(ImageSpec):
quality=90
processors = [ExifRotate]
class SmallThumb(ImageSpec):
pre_cache = True
processors = [ResizeSmallThumb]
quality = 90
class PrivateSmallThumb(ImageSpec):
pre_cache=False
processors = [ResizeSmallThumb, Gray]
# now we can define our thumbnail spec
class Thumbnail(ImageSpec):
access_as = 'thumbnail'
pre_cache = True
processors = [ResizeThumb]
quality = 90
class PrivateThumbnail(ImageSpec):
pre_cache = False
processors = [ResizeThumb, Gray]
# and our display spec
class Display(ImageSpec):
increment_count = True
pre_cache=True
processors = [ResizeDisplay]
quality = 90