Python 将openCV numpy数组转换为Wand图像格式

Python 将openCV numpy数组转换为Wand图像格式,python,opencv,wand,imagemagick,Python,Opencv,Wand,Imagemagick,如何转换从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_bu

如何转换从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二值化图像IMG: mat=cv2.IMDECOTIMG,cv2.IMREAD\U不变 ret1,th1=cv2.thresholdmat,127255,cv2.threshold_二进制 返回cv2.imencode.jpg,th1 使用Imagefilename=向导:作为img: img\u buffer=np.asarraybyteraryimg.make\u blobJPEG,dtype=np.uint8 ret,mat=二值化\u图像img\u缓冲区 Imageblob=mat作为timg时: timg.savefilename=output.jpg
虽然您可以使用、或方法直接完成相同的任务。

您需要问一个问题,但我能看到的只是一些问题code@GPPK我已经更新了问题