title: How to return a list of PIL image files from fastapi response?
body: <p>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.</p>
  <pre><code>from docx2pdf import convert_to    
  from fastapi import FastAPI, File, UploadFile
  
  app = FastAPI()
  
  @app.post(&quot;/file/convert&quot;)
  async def convert(doc: UploadFile = File(...)):
      if doc.filename.endswith(&quot;.pdf&quot;):
          # convert pdf to image
          with tempfile.TemporaryDirectory() as path:
              doc_results = convert_from_bytes(
                  doc.file.read(), output_folder=path, dpi=350, thread_count=4
              )
  
              print(doc_results)
  
          return doc_results if doc_results else None
  </code></pre>
  <p>This is the output of <code>doc_results</code>, basically a list of PIL image files</p>
  <pre><code>[&lt;PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2975x3850 at 0x7F5AB4C9F9D0&gt;, &lt;PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2975x3850 at 0x7F5AB4C9FB80&gt;]
  </code></pre>
  <p>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.</p>
  <p>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.</p>
  <p>Also, if you know any improvements I can make in the above code to speed up is also helpful.</p>
  <p>Any help is appreciated.</p>