flet icon indicating copy to clipboard operation
flet copied to clipboard

Add Image from File and Memory ( from bytes )

Open fabio56 opened this issue 2 years ago • 7 comments

  • Image for file f = open("path) page.add(Image(src=f)

  • Add also binary image example base64 image

fabio56 avatar Aug 15 '22 10:08 fabio56

Do I understand correctly that you would like the convenience of not having to do the base64 encoding yourself if Image is given a file-like object?

mikaelho avatar Aug 18 '22 13:08 mikaelho

@FeodorFitsner, I can take this is you are happy with the approach that src accepts either a str or a file-like object?

mikaelho avatar Aug 18 '22 13:08 mikaelho

File-like object sounds like a great idea. The question is should we save it to "assets" under temp name and use that URL or convert to base64 and push through WebSocket? What would be more effective?

FeodorFitsner avatar Aug 18 '22 13:08 FeodorFitsner

My quick take is that converting it to base64 means that the file only comes from disk and then goes over the WebSocket ("2 transfers"), but with the asset approach we might take it from disk, save it to disk, and send it over the network ("3 transfers"). Of course the first also has the base64 encoding, but I have grown up thinking that that kind of CPU operation is always faster than going over the network. 🤷🏻‍♂️

Well, maybe that is splitting hairs a bit too much, and my main argument would be just against saving temp files to assets, as that seems "not neat".

mikaelho avatar Aug 18 '22 15:08 mikaelho

My main concern here is the size of WebSocket message. Flet icons browser app takes that to "extrem hights" by allowing messages for up to 8 MB :), but my observation is that some messages aroung 500 Kb are quite "acceptable". So, it will work for images of ~500 KB size in base64-encoded form.

FeodorFitsner avatar Aug 18 '22 15:08 FeodorFitsner

Ok, so how about: src accepts str or file-like, and saves the file-like to assets, src_base64 accepts bytes or file-like to be base64 encoded, with warning in the docs about the file size (if not already there). Or is this getting too fancy?

Alternative could be e.g. src_file, which could decide which method to use based on the file size (or just use assets for everything).

mikaelho avatar Aug 18 '22 16:08 mikaelho

I say that how to display bit image

fabio56 avatar Aug 18 '22 16:08 fabio56