繁体   English   中英

将openCV numpy数组转换为Wand Image格式

[英]Convert a openCV numpy array to Wand Image format

如何转换从binarize_image函数获得的numpy数组并保存该图像。 我正在对图像进行预处理。 将图像转换为灰度图像后,我使用了将其转换为二进制图像的方法。

def binarize_image(img):
    ret1, th1 = cv2.threshold(img, BINARY_THREHOLD, 255, cv2.THRESH_BINARY)
    return th1 # numpy.ndarray

我在这里保存图像

   img.format = 'jpeg'
img_buffer = np.asarray(bytearray(img.make_blob()), dtype=np.uint8)
img = binarize_image(img_buffer)

# ..... Code to convert the ndarray back to Wand Image format .......

img.save(filename=os.path.join(pdf_folder,image_folder,outputFileName))

您正在将图像文件像素数据缓冲区混淆。 只需将JPEG blob解码为Mat,然后再编码回去即可。

def binarize_image(img):
    mat = cv2.imdecode(img, cv2.IMREAD_UNCHANGED)
    ret1, th1 = cv2.threshold(mat, 127, 255, cv2.THRESH_BINARY)
    return cv2.imencode(".jpg", th1)

with Image(filename="wizard:") as img:
    img_buffer = np.asarray(bytearray(img.make_blob("JPEG")), dtype=np.uint8)
    ret, mat = binarize_image(img_buffer)
    with Image(blob=mat) as timg:
        timg.save(filename="output.jpg")

output.jpg

尽管您可以使用Image.thresholdImage.contrast_stretchImage.levelImage.quantize方法直接使用完成相同的任务。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM