如何将imagemagick与Python结合使用?

如何将imagemagick与Python结合使用?,python,ubuntu,imagemagick,python-tesseract,wand,Python,Ubuntu,Imagemagick,Python Tesseract,Wand,我想用Python实现以下两个命令。我是Imagemagick的新手,所以有人能告诉我如何在Python中使用以下命令吗?我想我得用魔杖了 convert input.png -crop 762x41+32+100 -units pixelsperinch -density 300 image.png convert image.png -auto-level -negate -threshold 70% crop_processed.png tesseract crop-processed

我想用Python实现以下两个命令。我是Imagemagick的新手,所以有人能告诉我如何在Python中使用以下命令吗?我想我得用魔杖了

convert input.png -crop 762x41+32+100 -units pixelsperinch -density 300 image.png

convert image.png -auto-level -negate -threshold 70% crop_processed.png

tesseract crop-processed.png stdout
输入图像:


以下是如何在Python Wand中实现这一点。

输入:


结果:



或使用Python/OpenCV

import cv2
import numpy as np

# read image as grayscale
img = cv2.imread('stockholm.jpg', cv2.IMREAD_GRAYSCALE)

# crop image
img = img[99:99+37, 34:34+755]

# threshold image
img_thresh = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY)[1]

# view result
cv2.imshow("threshold", img_thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

# save result
cv2.imwrite("stockholm_crop_threshold.png", img_thresh)

结果:


-单位和-密度对光栅png图像输出没有影响。它们仅在打印时有用。但是,如果你在图像中设置阈值,它可能不会有帮助。因此,您需要做的就是
convert input.png-裁剪755x37+34+99-自动级别-否定-阈值70%裁剪已处理。png
请发布您的输入图像,以便我们可以处理它。我已编辑了我的原始帖子,添加了输入图像并更新了尺寸。对不起,我使用了您的原始尺寸。但是现在你可以改变代码了,你知道怎么做了。太棒了!如何在python中将结果传递给tesseract?对不起,我没有使用tesseract。
import cv2
import numpy as np

# read image as grayscale
img = cv2.imread('stockholm.jpg', cv2.IMREAD_GRAYSCALE)

# crop image
img = img[99:99+37, 34:34+755]

# threshold image
img_thresh = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY)[1]

# view result
cv2.imshow("threshold", img_thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

# save result
cv2.imwrite("stockholm_crop_threshold.png", img_thresh)