-1

I have a form where users can upload up to 1,000 images at a time. I have changed my FILE_UPLOAD_MAX_MEMORY_SIZE in Django settings to 0 so all files uploaded via a form are written to a temp directory in my root folder.

I am then trying to process the images with OpenCV.

original_img = cv2.imread(temp_image.temporary_file_path())
gray = cv2.cvtColor(original_img,cv2.COLOR_BGR2GRAY)
temp_image.temporary_file_path()

This returns the absolute file path of the temp image in a string format

So I put that in the cv2.imread, however, it creates a NoneType instead of a numpy array like it should and then my program cannot run when it reaches

gray = cv2.cvtColor(original_img,cv2.COLOR_BGR2GRAY)

How do I read the temporary file into OpenCV?

Any help is much appreciated.

pycode81
  • 124
  • 1
  • 9
  • show **literally the path** you are given please. then check that the file actually contains something. use `os.stat` to check on the file. – Christoph Rackwitz Dec 18 '21 at 11:36

1 Answers1

-1

in your case save that file to another path in your system first

import os

os.rename(temp_image.temporary_file_path(), another_file_path_in_your_system)
original_img = cv2.imread(another_file_path_in_your_system)
gray = cv2.cvtColor(original_img,cv2.COLOR_BGR2GRAY)
  • So there is no way to read it from the file that Django already wrote to the disk? – pycode81 Dec 17 '21 at 17:50
  • a tempfile is just a tempfile that you should save somewhere in your system if not you will loose it at some time...os.rename just move the file to another persistent location in your system that's not a problem – Narcisse DOUDIEU SIEWE Dec 17 '21 at 17:53
  • this doesn't explain why accessing the file would fail, but moving it would. "it's a tempfile" is not an explanation. I think this solution should fail because the os.rename is subject to pretty much the same environment as cv2.imread. – Christoph Rackwitz Dec 18 '21 at 11:36