Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Python] In-memory save png
#1
Hello,

I don't know, whether such a feature is realizable without certain changes to the Gio, but let me just describe my intent:

In a plugin, I want to send an "copy" of current image to a service using urllib.request.Request. Currently, I save the png image as usual using Gimp.file_save(), reopen it to read the data as bytes and attach them to a boundarized body. Then I send this body using the content-type 'multipart/form-data; boundary=xxxx' to the web service. I get an answer, evaluate the response and add the retrieved image to a new created layer. This all is working well and stable.

But I just want to get rid of storing the image into a file and read it again to a byte buffer. Already tried Gio.File.new_for_uri() and point the uri to memory, but my current knowledge for this aspect of the Gio library is literally not existent  Big Grin

Maybe there is a more elegant way to implement a in-memory file storage, as the export to PNG format is crucial.

Does someone has an approach in mind and could give me some pointers to study?

Thanks
Reply
#2
Probably unnecessary optimization. Your file is still cached in the RAM buffers so it won't be reread from disk.

Otherwise I would see if perhaps GEGL can do a PNG encoding to a ram buffer.

Kudos for using urlllib. I thought us humans were better off using requests?
Reply
#3
Hi ofnuts,

thanks for your very fast response, highly appreciated Smile

And thank you for the pointer, I will try to find information about this GEGL topic. I've also a maybe-working way by implementing the Gio.File interface in plain python to utilize only memory storage and offer a method to return the bytes when appending it to request. But this could be some pain and may not fully working or instable, I think. What do you think about this approach?

Yes, urllib instead of request is used, because of the fact, that I implemented the plugin for Gimp 2.10 as well, which seems not to ship 'requests', but urllib, and I don't want users are forced to install additional libs if it is not necessary. Finally, I come from the Java world and like the feeling of Apache Http Client, where you can control every aspect of the connection by yourself.
Reply
#4
(5 hours ago)DunkleMaterie Wrote: And thank you for the pointer, I will try to find information about this GEGL topic. I've also a maybe-working way by implementing the Gio.File interface in plain python to utilize only memory storage and offer a method to return the bytes when appending it to request. But this could be some pain and may not fully working or instable, I think. What do you think about this approach?
Frankly? Waste of time. What problem are you trying to solve? How will a memory file be significantly better that a file mapped to a tmpfs (on Linux you can count on /run/user/{id}) or even a plain filesystem given the existence of file buffers all over the place?
Otherwise, Gio has MemoryInputStream and MemoryOutputStream.
Reply


Forum Jump: