Skip to content
Advertisement

How to return a list of PIL image files from fastapi response?

I have created an rest-api using fastapi, which takes a document (pdf) as input and return a jpeg image of it, I am using a library called docx2pdf for conversion.

JavaScript

This is the output of doc_results, basically a list of PIL image files

JavaScript

If I run my current code, it is returning the doc_results as json output and I am not being able to load those images in another API.

How can I return image files without saving them to local storage? So, I can make a request to this api and get the response and work on the image directly.

Also, if you know any improvements I can make in the above code to speed up is also helpful.

Any help is appreciated.

Advertisement

Answer

You can not return that unless you convert it to something universal.

<PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2975×3850 at 0x7F5AB4C9F9D0

This basically says, You have an object of PIL at your memory here is the location for it.

The best thing you can do is, convert them to bytes and return an array of bytes.


You can create a function that takes a PIL image and returns the byte value from it.

JavaScript

Then you can use this function when returning the response.

JavaScript
Advertisement