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.
Clipboard.get_image Crash in Pythonista Keyboard
-
@cvp wow, works perfectly.thank you! Actually, last night I was thinking whether Objectivec can be used to replace the function of cliboard.get_image. But it was too hard for me to do that.π
Do you mind if I ask a few more questions about Objectivec? when I get the object of UIPasteboard.image(), how can I change it to a python object (like PIL image or binary data)? and can UIPasteboard.image() handle with gif files? so much Thanks!
-
@mieq said:
how can I change it to a python object
I think you can't but, often, tou can access to Objectivec object of a Python(ista) object by its ObjInstance.
-
@mieq said:
can UIPasteboard.image() handle with gif files?
Yes, you can paste a gif but up to you to display it as gif
-
Pasteboard should produce a byted object i belive and using a context manager probably with BytesIO then u can convert that to ui.image with from_data. make sure you pass a 2.0 for second param to acount for your 2:1 retina.
@cvp outstanding!
i was so stuck on the image i didnt even think of UIPasteBoard good lookin out!
-
-
heres how i would of π€
import keyboard import ui import io from objc_util import * if __name__ == '__main__': v = ui.ImageView() UIPasteboard = ObjCClass('UIPasteboard').generalPasteboard() img_from_pasteboard = UIPasteboard.image() png_raw_bytes=uiimage_to_png(img_from_pasteboard) with io.BytesIO(png_raw_bytes) as bIO: new_ui_img = ui.Image.from_data(bIO.getvalue(), 2) v.image = new_ui_img if keyboard.is_keyboard(): keyboard.set_view(v) else: v.present('sheet')
-
@stephen did you try with a gif, is it animated? I had read that with PasteBoard image, we get only the first frame of the gif, reason why I tried to get the entire file as data. And with my 3 lines, I get data with the length of the entire file.
-
-
-
@stephen Your
UIPasteboard = ObjCClass('UIPasteboard').generalPasteboard() img_from_pasteboard = UIPasteboard.image() png_raw_bytes=uiimage_to_png(img_from_pasteboard) with io.BytesIO(png_raw_bytes) as bIO: new_ui_img = ui.Image.from_data(bIO.getvalue(), 2) v.image = new_ui_img
or mine
UIPasteboard = ObjCClass('UIPasteboard').generalPasteboard() im = UIPasteboard.image() ObjCInstance(v).setImage_(im)
-
@cvp yours is beter not only cleaner but it is ALOT faster too. plus as u said the python ui.image only gets first frame π€π great job !
-
@mieq and @stephen this works for gif πΎ
import keyboard import ui import io from objc_util import * from PIL import Image class Gif(ui.View): def __init__(self,pil,duration): self.duration = duration self.ImageView = ui.ImageView() self.add_subview(self.ImageView) self.pil = pil self.update_interval = self.duration / self.pil.n_frames self.frame_id = 0 def pil2ui(self,imgIn): with io.BytesIO() as bIO: imgIn.save(bIO, 'PNG') imgOut = ui.Image.from_data(bIO.getvalue()) del bIO return imgOut def update(self): # Display individual frames from the loaded animated GIF file self.pil.seek(self.frame_id) self.ImageView.image = self.pil2ui(self.pil) self.frame_id = self.frame_id + 1 if self.frame_id >= self.pil.n_frames: self.frame_id = 0 def touch_ended(self,touch):j if self.update_interval == 0: self.update_interval = self.duration / self.pil.n_frames else: self.update_interval = 0 if __name__ == '__main__': UIPasteboard = ObjCClass('UIPasteboard').generalPasteboard() data = UIPasteboard.dataForPasteboardType_("com.compuserve.gif") b = nsdata_to_bytes(ObjCInstance(data)) pil = Image.open(io.BytesIO(b)) v = Gif(pil,1) v.present('sheet')#,hide_title_bar=True)
-
@cvp this should be added to examples or The Github collection πΊπ»
-
@stephen said:
this should be added to examples or The Github collection
I don't even know what it is π
-
@cvp collection of scripts for pythonista and this one i think is most popular public repo..
-
@stephen you know, there are only 4 interesting lines:
UIPasteboard = ObjCClass('UIPasteboard').generalPasteboard() data = UIPasteboard.dataForPasteboardType_("com.compuserve.gif") b = nsdata_to_bytes(ObjCInstance(data)) pil = Image.open(io.BytesIO(b))
-
@mieq I've tested, it is also ok for keyboard view.
-
@cvp yes but the amount of research i did earlier and still failed lol makes those few lines outstanding ππ
-
@cvp Perfect, I'm not surprised anymore, I already feel that no problem can stump you.π
-