/usr/lib/python2.7/dist-packages/TileStache/PixelEffects.py is in tilestache 1.51.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | """ Different effects that can be applied to tiles.
Options are:
- blackwhite:
"effect":
{
"name": "blackwhite"
}
- greyscale:
"effect":
{
"name": "greyscale"
}
- desaturate:
Has an optional parameter "factor" that defines the saturation of the image.
Defaults to 0.85.
"effect":
{
"name": "desaturate",
"factor": 0.85
}
- pixelate:
Has an optional parameter "reduction" that defines how pixelated the image
will be (size of pixel). Defaults to 5.
"effect":
{
"name": "pixelate",
"factor": 5
}
- halftone:
"effect":
{
"name": "halftone"
}
- blur:
Has an optional parameter "radius" that defines the blurriness of an image.
Larger radius means more blurry. Defaults to 5.
"effect":
{
"name": "blur",
"radius": 5
}
"""
from PIL import Image, ImageFilter
def put_original_alpha(original_image, new_image):
""" Put alpha channel of original image (if any) in the new image.
"""
try:
alpha_idx = original_image.mode.index('A')
alpha_channel = original_image.split()[alpha_idx]
new_image.putalpha(alpha_channel)
except ValueError:
pass
return new_image
class PixelEffect:
""" Base class for all pixel effects.
Subclasses must implement method `apply_effect`.
"""
def __init__(self):
pass
def apply(self, image):
try:
image = image.image() # Handle Providers.Verbatim tiles
except (AttributeError, TypeError):
pass
return self.apply_effect(image)
def apply_effect(self, image):
raise NotImplementedError(
'PixelEffect subclasses must implement method `apply_effect`.'
)
class Blackwhite(PixelEffect):
""" Returns a black and white version of the original image.
"""
def apply_effect(self, image):
new_image = image.convert('1').convert(image.mode)
return put_original_alpha(image, new_image)
class Greyscale(PixelEffect):
""" Returns a grescale version of the original image.
"""
def apply_effect(self, image):
return image.convert('LA').convert(image.mode)
class Desaturate(PixelEffect):
""" Returns a desaturated version of the original image.
`factor` is a number between 0 and 1, where 1 results in a
greyscale image (no color), and 0 results in the original image.
"""
def __init__(self, factor=0.85):
self.factor = min(max(factor, 0.0), 1.0) # 0.0 <= factor <= 1.0
def apply_effect(self, image):
avg = image.convert('LA').convert(image.mode)
return Image.blend(image, avg, self.factor)
class Pixelate(PixelEffect):
""" Returns a pixelated version of the original image.
`reduction` defines how pixelated the image will be (size of pixels).
"""
def __init__(self, reduction=5):
self.reduction = max(reduction, 1) # 1 <= reduction
def apply_effect(self, image):
tmp_size = (int(image.size[0] / self.reduction),
int(image.size[1] / self.reduction))
pixelated = image.resize(tmp_size, Image.NEAREST)
return pixelated.resize(image.size, Image.NEAREST)
class Halftone(PixelEffect):
""" Returns a halftone version of the original image.
"""
def apply_effect(self, image):
cmyk = []
for band in image.convert('CMYK').split():
cmyk.append(band.convert('1').convert('L'))
new_image = Image.merge('CMYK', cmyk).convert(image.mode)
return put_original_alpha(image, new_image)
class Blur(PixelEffect):
""" Returns a blurred version of the original image.
`radius` defines the blurriness of an image. Larger radius means more
blurry.
"""
def __init__(self, radius=5):
self.radius = max(radius, 0) # 0 <= radius
def apply_effect(self, image):
return image.filter(ImageFilter.GaussianBlur(self.radius))
all = {
'blackwhite': Blackwhite,
'greyscale': Greyscale,
'desaturate': Desaturate,
'pixelate': Pixelate,
'halftone': Halftone,
'blur': Blur,
}
|