Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write to buffer or return bytes #28

Open
flekschas opened this issue Mar 12, 2018 · 4 comments
Open

Write to buffer or return bytes #28

flekschas opened this issue Mar 12, 2018 · 4 comments

Comments

@flekschas
Copy link

This is more a question or feature request / idea rather than an issue.

If I am not mistaken it's currently not possible to write to a buffer or return the written bytes somehow right? What do you think about expanding the interface to support something like:

buf = BytesIO()
imsave(buf, my_fancy_array, formatstr='png')

If this is out of scope never mind. I am just curious.

@luispedro
Copy link
Owner

This would be a good feature, but it would require more effort than I am currently willing to invest.

@flekschas
Copy link
Author

I understand. I'll see if I can get something to work when I have some free time.

@luispedro
Copy link
Owner

Here are some pointers:

  1. You should probably do a derived class from byte_sink (see

    class byte_sink : virtual public seekable {
    ) which writes to a buffer in memory.

  2. Then, in the imsave_may_multi function, (

    const int fd = ::open(filename, O_CREAT|O_RDWR|O_BINARY|O_TRUNC, 0644);
    ), you use it instead of the current fd_source_sink (depending on the input, naturally).

  3. Finally, convert it to a memory object and return it to Python.

A few things to watch out for:

  • some writers need to seek in the output they've written
  • writing happens without the GIL so avoid calling Python API functions.

HTH
Luis

@fish2000
Copy link
Contributor

@flekschas as per the pointers offered by @luispedro – I have some things that may help you:

… if you’re curious, I also have an example of a method returning a PyObject* containing the bytes that result from calling ImageFormat::write(…), and a Python C-API module method in which it gets used.

I, too, HTH. Salud,

-Alex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants