Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Creating an image mask?
-
In my UI I have an image, loaded from a URL. It's a JPEG square. I want to create an image mask programmatically, so that the image is displayed in a circle.
I use:
imageView1.load_from_url(partnerPictureURL)I then reference that image view from the UI.
As a sidenote I had trouble displaying the image in the view using the other methods so I went with this method.Anyhow - any advice on doing this? Assuming the image is unique every time it's loaded.
-
See @0942v8653 's
composite()
function in http://omz-forums.appspot.com/pythonista/post/5035536628580352 -
Thank you. That looks like a great function. Before I saw this however, I clunked together code from a few different sources to get a dynamically generating, smoothly anti-aliased image mask.
The only problem I have now, and it's a problem which I haven't been able to get around irrespective of the image mask, is that I can't get anything to display inside of a UI imageview except using the load_from_url() method.
Here is the code I am using in a manner which seems right to me. It runs with no errors, but the imageview remains empty.
import ui import cStringIO import urllib2 from PIL import Image, ImageOps, ImageDraw url= "http://vignette2.wikia.nocookie.net/jamesbond/images/3/31/Vesper_Lynd_(Eva_Green)_-_Profile.jpg/revision/latest?cb=20130506215331" v= ui.load_view('imageviewtest.pyui') imageView1=v["imageview1"] #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() #change image to string form to be able to insert into a ui image view...hopefully imgString = img.tostring() imageView1.image = ui.Image.from_data(imgString) v.present()
-
I believe that you get the result that you want with:
imageView1.image = pil2ui(img)
If you get @jmv38 's
pil2ui()
from http://omz-forums.appspot.com/pythonista/post/5520750224080896 -
Thank you CCC, that did the trick.
Here is an updated function that takes a url and shoots out a UI image.
import ui import webbrowser import cStringIO import urllib2 from PIL import Image, ImageOps, ImageDraw import io url= "http://vignette2.wikia.nocookie.net/jamesbond/images/3/31/Vesper_Lynd_(Eva_Green)_-_Profile.jpg/revision/latest?cb=20130506215331" 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 imageView1.image=circleMaskViewFromURL(url)
-
This post is deleted!