Reputation: 302
I have been working one a Django project and I need to save an image file manually. So, I tried using PIL. Here's the code in my Django view that breaks-
import PIL.Image as PilImage
image_file = request.FILES.get("pic").file
image = PilImage.open(io.BytesIO(image_file))
I don't understand it because io.BytesIO is a byte-like object, right? I couldn't find any solution anywhere so I'd appreciate any help.
The full error is below -
Traceback (most recent call last):
File "G:\Workspace\Rabo\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "G:\Workspace\Rabo\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "G:\Workspace\Rabo\venv\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "G:\Workspace\Rabo\venv\lib\site-packages\django\views\generic\base.py", line 98, in dispatch
return handler(request, *args, **kwargs)
File "G:\Workspace\Rabo\App\utils\login_required.py", line 25, in wrapper
return f(self, request, user_id)
File "G:\Workspace\Rabo\App\views\user_views.py", line 125, in post
image = PilImage.open(io.BytesIO(image_file))
TypeError: a bytes-like object is required, not '_io.BytesIO'
EDIT: I used this method to save the image for now, but I'd still like to know why PIL gave the that error. I'd appreciate if someone could shed some light on this.
Upvotes: 0
Views: 4843
Reputation: 461
You didn't read the data from io.BytesIO(image_file)
. io.BytesIO(image_file).read()
should do the job
Upvotes: 3