forked from lqs/PIL-WebP
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWebPImagePlugin.py
57 lines (43 loc) · 1.73 KB
/
WebPImagePlugin.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
from PIL import Image
from PIL import ImageFile
import StringIO
from _webp_wrapper import *
def _accept(prefix):
return prefix[:4] == "RIFF" and prefix[8:12] == "WEBP"
class WebPImageFile(ImageFile.ImageFile):
format = "WEBP"
format_description = "WebP image"
def _open(self):
raw = self.fp.read()
fwidth, fheight, has_alpha = WebPGetFeatures(raw)
if has_alpha:
self.mode = "RGBA"
data, width, height = WebPDecodeRGBA(raw)
else :
self.mode = "RGB"
data, width, height = WebPDecodeRGB(raw)
assert (fwidth == width) and (fheight == height)
self.size = width, height
self.fp = StringIO.StringIO(data)
self.tile = [("raw", (0, 0) + self.size, 0, self.mode)]
def _save(im, fp, filename):
quality = im.encoderinfo.get("quality", 80)
if im.mode == "RGB":
data = WebPEncodeRGB(im.tobytes(), im.size[0], im.size[1], im.size[0] * 3, float(quality))
elif im.mode == "RGBA":
data = WebPEncodeRGBA(im.tobytes(), im.size[0], im.size[1], im.size[0] * 4, float(quality))
else:
raise IOError("cannot write mode %s as WEBP" % im.mode)
fp.write(data)
def explicitRegisterOnPIL(ImageClass):
"""
Instead of monkeypatching on import, this function lets you send the PIL.Image class and explicitly register on it
"""
ImageClass.register_open("WEBP", WebPImageFile, _accept)
ImageClass.register_save("WEBP", _save)
ImageClass.register_extension("WEBP", ".webp")
ImageClass.register_mime("WEBP", "image/webp")
Image.register_open("WEBP", WebPImageFile, _accept)
Image.register_save("WEBP", _save)
Image.register_extension("WEBP", ".webp")
Image.register_mime("WEBP", "image/webp")