Re: send PIL.Image to django server side and get it back
Thanks, I solved it finally stackoverflow.com/a/51453785/1485853 -- https://mail.python.org/mailman/listinfo/python-list
Re: send PIL.Image to django server side and get it back
On Mon, Jul 16, 2018 at 06:40:45AM -0700, Christos Georgiou - ΤΖΩΤΖΙΟΥ wrote: > 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() > >>> PILImage.save(file_like_object, 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. If you definitely need to send the data as a string, because you want to use a JSON object or similar, I've done this in the past using base64 encoding. https://docs.python.org/2/library/base64.html Cheers, -- José María (Chema) Mateos https://rinzewind.org/blog-es || https://rinzewind.org/blog-en -- https://mail.python.org/mailman/listinfo/python-list
send PIL.Image to django server side and get it back
I also posted the question here https://stackoverflow.com/questions/51355926/send-pil-image-to-django-server-side-and-get-it-back 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 = requests.post(SERVER_URL, data={ 'image': PILimage,#PILimage is of type PIL.Image 'wordPos':(86,23) }, ) then I just got a str object with value 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! -- https://mail.python.org/mailman/listinfo/python-list
Re: send PIL.Image to django server side and get it back
On Monday, July 16, 2018 at 3:52:09 PM UTC+3, iMath wrote: > I also posted the question here > https://stackoverflow.com/questions/51355926/send-pil-image-to-django-server-side-and-get-it-back > > 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 = requests.post(SERVER_URL, > data={ > 'image': PILimage,#PILimage is of type PIL.Image > 'wordPos':(86,23) > }, > ) > then I just got a str object with value 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() >>> PILImage.save(file_like_object, 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. -- https://mail.python.org/mailman/listinfo/python-list