-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathfrom_pil_image.py
57 lines (44 loc) · 1.57 KB
/
from_pil_image.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
# You need the following package for this
# Installation : pip install pillow
# Source : http://pillow.readthedocs.org/en/latest/index.html
import PIL.Image
import graphlab as gl
import urllib2 as urllib
import io
_format = {'JPG': 0, 'PNG': 1, 'RAW': 2, 'UNDEFINED': 3}
def from_pil_image(pil_img):
"""
Returns a graphlab.Image constructed from the passed PIL Image
Parameters
----------
pil_img : PIL.Image
A PIL Image that is to be converted to a graphlab.Image
Returns
--------
out: graphlab.Image
The input converted to a graphlab.Image
"""
# Read in PIL image data and meta data
height = pil_img.size[1]
width = pil_img.size[0]
if pil_img.mode == 'L':
image_data = bytearray([z for z in pil_img.getdata()])
channels = 1
elif pil_img.mode == 'RGB':
image_data = bytearray([z for l in pil_img.getdata() for z in l ])
channels = 3
else:
image_data = bytearray([z for l in pil_img.getdata() for z in l])
channels = 4
format_enum = _format['RAW']
image_data_size = len(image_data)
# Construct a graphlab.Image
img = gl.Image(_image_data=image_data, _width=width, _height=height, _channels=channels, _format_enum=format_enum, _image_data_size=image_data_size)
return img
# Sample conversion
# Retrive image from the web
fd = urllib.urlopen("http://s3.amazonaws.com/gl-testdata/images/sample.jpg")
image_file = io.BytesIO(fd.read())
# Convert from PIL to graphlab
pil_img = PIL.Image.open(image_file)
gl_img = from_pil_image(pil_img)