send PIL.Image to django server side and get it back

Christos Georgiou - ΤΖΩΤΖΙΟΥ tzotzioy at
Mon Jul 16 09:40:45 EDT 2018

On Monday, July 16, 2018 at 3:52:09 PM UTC+3, iMath wrote:
> I also posted the question here
> I don't know what's under the hood of sending an image from client side to server side, so stuck by the following scenario.
> I want to send a PIL.Image object to django server side using the Python requests lib and get it back in order to use the PIL.Image object on server side. As I have tested , if sent the PIL.Image object without any conversion , that is
> r =,
>                 data={
>                     'image': PILimage,#PILimage is of type PIL.Image
>                     'wordPos':(86,23)
>                     },
>                 )
> then I just got a str object with value  <PIL.PngImagePlugin.PngImageFile image mode=RGB size=149x49 at 0x13F25D0> on server side, I guess it was caused by  requests, which converted the PIL.Image object to a str object before sending, so why  requestsdo the conversion ? why cannot we send the PIL.Image object without any conversion over the Internet ? please give some explanation here, thanks!

You need first to serialize the object to bytes that can go over the wire. There is no predefined way to do that, so you can:

>>> import io
>>> file_like_object = io.BytesIO()
>>>, format='png')

and then in your POST request send file_like_object.getvalue() as the image data.  You will most probably need to add a Content-Type: image/png as a header.

More information about the Python-list mailing list