内存中的python OpenCV jpeg压缩 [英] python OpenCV jpeg compression in memory
问题描述
在OpenCV中,可以使用某种jpeg压缩将图像保存到磁盘.还有在内存中执行此操作的方法吗?还是应该使用cv2.imsave()
编写一个函数来加载文件并再次将其从磁盘中删除?如果有人知道更好的方法,那也很好.
In OpenCV it is possible to save an image to disk with a certain jpeg compression. Is there also a way to do this in memory? Or should I write a function using cv2.imsave()
that loads the file and removes it again from disk? If anyone knows a better way that is also fine.
用例是实时数据扩充.使用OpenCV以外的其他东西可能会导致不必要的开销.
The use case is real-time data augmentation. Using something else than OpenCV would cause possibly unnecessary overhead.
所需功能示例im = cv2.imjpgcompress(90)
推荐答案
您可以使用imencode
:
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
result, encimg = cv2.imencode('.jpg', img, encode_param)
( IMWRITE_JPEG_QUALITY
的默认值为95 )
(The default value for IMWRITE_JPEG_QUALITY
is 95.)
您可以使用以下方法将其解码回去:
You can decode it back with:
decimg = cv2.imdecode(encimg, 1)
来自此处 的代码段
Snippet from here
这篇关于内存中的python OpenCV jpeg压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!