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.
Button image from URL
-
This post is deleted! -
Yes and it gives me and error that the value needs to be a ui.Image
# example url url = 'http://is4.mzstatic.com/image/thumb/Music7/v4/53/fc/a2/53fca253-84b1-f2cd-4e17-98be502ec53c/UMG_cvrart_00602547534873_01_RGB72_1500x1500_15UMGIM41882.jpg/55x55bb-85.jpg' i = urlopen(url) b= BytesIO(i.read()) img = Image.open(b) btn = ui.Button() btn.image = img #<<< TypeError: Expected a ui.Image
-
Try something like:
img_data = urlopen(url).read() img = ui.Image.from_data(img_data) # ...
-
So simple. But worked perfectly. Thanks
-
Thanks. I couldn't test that code when I wrote it,
-
I had to use
button.background_image
... https://github.com/cclauss/Pythonista_ui/blob/master/TopTenView.py -
@ccc That shouldn't be necessary, and you'll lose touch highlighting this way. You can use the
with_rendering_mode
method to make opaque/colored images usable as button images:# ... img = ui.Image.from_data(requests.get(url).content) img = img.with_rendering_mode(ui.RENDERING_MODE_ORIGINAL) button.background_image = img # ...
-
Ahhh. Yes. I forgot that trick.
Suggested API improvement: ui.ImageView has a slick
.load_from_url()
method. Would it be possible to give ui.Button a similar method which would be equivelent tobutton.image = ui.Image.from_data(requests.get(url).content).with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)
which can be a bit cumbersome to type? -
Also, why is it advantageous to have the default rendering mode be something different than ui.RENDERING_MODE_ORIGINAL?
-
Also, why is it advantageous to have the default rendering mode be something different than ui.RENDERING_MODE_ORIGINAL?
If you use typical "glyph" button icons (with transparency), you'd end up with black&white images, and the button would ignore its
tint_color
. Buttons (and a couple of other UI elements) only use their tint color when the image has a rendering mode of eitherRENDERING_MODE_TEMPLATE
orRENDERING_MODE_AUTOMATIC
(the default).