def pack_img(header, img, quality=80, img_fmt='.jpg'):
"""pack an image into MXImageRecord
Parameters
----------
header : IRHeader
header of the image record
img : numpy.ndarray
image to pack
quality : int
quality for JPEG encoding. 1-100, or compression for PNG encoding. 1-9.
img_fmt : str
Encoding of the image. .jpg for JPEG, .png for PNG.
Returns
-------
s : str
The packed string
"""
assert opencv_available
jpg_formats = set(['.jpg', '.jpeg', '.JPG', '.JPEG'])
png_formats = set(['.png', '.PNG'])
encode_params = None
if img_fmt in jpg_formats:
encode_params = [cv2.IMWRITE_JPEG_QUALITY, quality]
elif img_fmt in png_formats:
encode_params = [cv2.IMWRITE_PNG_COMPRESSION, quality]
ret, buf = cv2.imencode(img_fmt, img, encode_params)
assert ret, 'failed encoding image'
return pack(header, buf.tostring())
评论列表
文章目录