I have a form that uploads an image to my view; the uploaded image type is:
<class 'django.core.files.uploadedfile.InMemoryUploadedFile'>
I want to process the image though; thus I need it in PIL or CV2 format.
How can I do that ?
Django File
actually implements all the required methods of a typical file object which PIL.Image.open()
needs. An InMemoryUploadedFile
is a Django File
.
You can reopen the File
in the correct mode
and then pass it to PIL.Image.open()
. This should suffice: [1]
someFile: InMemoryUploadedFile = # •••Already has a value•••
someFile = someFile.open("b+")
with PIL.Image.open(someFile) as img:
# •••Do something•••
[1] The solution doesn’t copy the underlying bytes of the file object but simply passes a handle of it from one routine to another. BEWARE OF DATA RACES.