如何将 imagemagick 与 Python 一起使用?
How do I use imagemagick with Python?
我有以下两个要使用 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 中的操作方法。
输入:
from wand.image import Image
from wand.display import display
with Image(filename='stockholm.jpg') as img:
img.crop(left=34, top=99, width=755, height=37)
img.auto_level()
img.negate()
img.threshold(threshold=0.70)
img.save(filename='stockhold_processed.png')
display(img)
结果:
或使用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)
结果:
我有以下两个要使用 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 中的操作方法。
输入:
from wand.image import Image
from wand.display import display
with Image(filename='stockholm.jpg') as img:
img.crop(left=34, top=99, width=755, height=37)
img.auto_level()
img.negate()
img.threshold(threshold=0.70)
img.save(filename='stockhold_processed.png')
display(img)
结果:
或使用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)
结果: