I need to write an OpenCV image that sits in memory to a BytesIO or Tempfile object for use elsewhere.
I am concerned this is a dead end question, because cv2.imwrite()
takes a filename as an argument, and then uses the file extension to infer the image type to write (.jpg
, .png
, .tiff
, etc.). cv2.imwrite()
does this at the C++ level, so I am concerned that there is no way to successfully pass a non-filename object to it.
The other possible solution is converting to PIL
through numpy
, which has the capacity to write to BytesIO
and Tempfile
objects, but I want to avoid needless copies.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…