1

Suppose I have a variable "profile_image" of image file type. I have created the variable like below:

profile_image = request.FILES.get('profile_image', False) 

Now I need to convert this image variable to bytes array. How can I do this without saving it to the local drive? I found that file open/read do not work here. I tried like below:

with open(profile_image.name, "rb") as imageFile:
       file_stream = imageFile.read()

Please help me. By the way, by converting into bytes array I want to save it to google cloud.

Thank you.

4
  • try bytearray(profile_image) Commented Jun 21, 2018 at 10:05
  • Have you checked this stackoverflow.com/questions/22351254/…? Commented Jun 21, 2018 at 10:06
  • @ChandanKumar bytearray() is not useful. Commented Jun 22, 2018 at 3:12
  • @GauravPaliwal thanks. I checked that, but could not find the silution that I need. Commented Jun 22, 2018 at 3:13

1 Answer 1

5

After googling for a long time I found the solution Here.

Actually "profile_image" is an "InMemoryUploadedFile" object in python. We can read the file like below:

profile_image.file.read()
Sign up to request clarification or add additional context in comments.

1 Comment

If I do that, I get b'', even though I do have the correct InMemoryUploadedFile from Django.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.