forked from tdamdouni/Pythonista
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCroppedRoundImage.py
60 lines (47 loc) · 1.29 KB
/
CroppedRoundImage.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
# coding: utf-8
# https://forum.omz-software.com/topic/2902/circle-view-for-ui/4
from __future__ import print_function
import ui
import webbrowser
#these are fillers for phhton 3 changes
try:
import cStringIO
print("old cString")
except ImportError:
from io import StringIO as cStringIO
print("new stringIO")
try:
import urllib2
print(urllib2)
except ImportError:
import urllib3 as urllib2
print((urllib3,' as urllib2'))
from PIL import Image, ImageOps, ImageDraw
import io
import Image
def circleMaskViewFromURL(url):
url=url
#load image from url and show it
file=cStringIO.StringIO(urllib2.urlopen(url).read())
img = Image.open(file)
#begin mask creation
bigsize = (img.size[0] * 3, img.size[1] * 3)
mask = Image.new('L', bigsize, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0) + bigsize, fill=255)
mask = mask.resize(img.size, Image.ANTIALIAS)
img.putalpha(mask)
#show final masked image
img.show()
img=pil2ui(img)
return img
# pil <=> ui
def pil2ui(imgIn):
with io.BytesIO() as bIO:
imgIn.save(bIO, 'PNG')
imgOut = ui.Image.from_data(bIO.getvalue())
del bIO
return imgOut
if __name__=="__main__":
#tests
circleMaskViewFromURL("http://vignette2.wikia.nocookie.net/jamesbond/images/3/31/Vesper_Lynd_(Eva_Green)_-_Profile.jpg/revision/latest?cb=20130506215331")