How should I return multiple images from fast API ? #4261
-
Fast API provides documentation on how to return a single image as a file, either by using Is there an example on what would be the best practice to return multiple images using fast API ? |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 3 replies
-
Hey, @yichong96. This may depend on your use-case. If you're looking to return N images in an archive, If you're looking to return N actual images, I think your best option is to serve one image at a time through N requests (per FastAPI documentation on how to return an image). A single HTTP response needs to define it's content type, and I'm not aware of any content type that represents more than one image. A potential third option could be to merge N images into a single image, and return that single image. This could be slow to process, and the client doesn't really have the option to easily chop the merged image back into N separate images. All this said, the middle option is common practice and likely your best bet. Best, |
Beta Was this translation helpful? Give feedback.
-
Hi @simondale00 Thanks for the help ! As I am trying to build an image search service, one request should return multiple images. I won't be able to send N requests on the client side. With that said, 1st option would be the best bet ? |
Beta Was this translation helpful? Give feedback.
-
Hey @yichong96, Is this an image search service akin to searching Google Images? Wherein a user searches a keyword in their browser, and the browser then displays search results as images in some kind of grid? If 'yes', I think the second option is still best with some caveats. You'd probably want an API endpoint to perform the query, the result of which returns a JSON response with an array of image URLs matching the query. You could do something like this:
The client can then parse this response and determine how it wants to fetch each result. I'd then recommend the images be served from as close to the edge as possible. Serving directly from a blob store (AWS S3?) or a CDN (AWS CloudFront, with S3 as an origin?) will be far more performant than having FastAPI fetch and serve each and every file. Best! |
Beta Was this translation helpful? Give feedback.
-
Hey @simondale00 thanks so much for your detailed explanation !!
Regarding this statement, the images would be in an object storage using s3 client to download the images. initially I had wanted the fast API server to download the images from the object store into file like objects and then send them over http back to the client. Would you mind explaining why it would be faster for the client to retrieve the images directly from the object store than for the server to send the images via http ? I am kind of learning as well. Thank you !! |
Beta Was this translation helpful? Give feedback.
-
Absolutely! Consider the two diagrams below illustrating how a client could fetch an image:
Both paths look similar, but they differ in a few ways:
Best! |
Beta Was this translation helpful? Give feedback.
-
@simondale00 Thank you for your explanation once again ! I was thinking along the lines that because the images would have to be downloaded from the object store by the web server (to process the query and do image comparisons), there would be duplicate work done if the server were to send the url for the search results hosted on the object store rather than just return the already downloaded images. Im not sure if allowing the client to download the images from object store would be faster as the bottleneck would arise from fast api downloading the images and processing the query ? I guess it really depends on other factors as well haha. |
Beta Was this translation helpful? Give feedback.
Hey @yichong96,
Is this an image search service akin to searching Google Images? Wherein a user searches a keyword in their browser, and the browser then displays search results as images in some kind of grid?
If 'yes', I think the second option is still best with some caveats. You'd probably want an API endpoint to perform the query, the result of which returns a JSON response with an array of image URLs matching the query. You could do something like this: