Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Wand&;将此命令转换为python代码;ImageMagick_Python_Imagemagick_Wand_C - Fatal编程技术网

如何使用Wand&;将此命令转换为python代码;ImageMagick

如何使用Wand&;将此命令转换为python代码;ImageMagick,python,imagemagick,wand,c,Python,Imagemagick,Wand,C,我想转换一幅图像,以便使用pyocr和tesseract更好地阅读它。 我要转换为python的命令行是: convert pic.png -background white -flatten -resize 300% pic_2.png 使用python魔杖,我成功地调整了它的大小,但我不知道如何处理平坦和白色背景 我的尝试: from wand.image import Image with Image(filename='pic.png') as image: image.res

我想转换一幅图像,以便使用pyocr和tesseract更好地阅读它。 我要转换为python的命令行是:

convert pic.png -background white -flatten -resize 300% pic_2.png
使用python魔杖,我成功地调整了它的大小,但我不知道如何处理平坦和白色背景 我的尝试:

from wand.image import Image
with Image(filename='pic.png') as image:
    image.resize(270, 33)  #Can I use 300% directly ?
    image.save(filename='pic2.png')
请帮忙
编辑,以下是要进行测试的图像:

用于调整大小和背景。使用以下公式,并注意您需要自己计算300%

从wand.image导入图像
从wand.color导入颜色
将图像(filename=“pic.png”)作为img:
#-调整大小300%
定标器=3
img.resize(img.width*缩放器,img.height*缩放器)
#-背景白色
img.background\u color=颜色(“白色”)
保存(filename=“pic2.png”)
不幸的是,该方法尚未实现。您应该提交一个增强请求

更新 如果要删除透明度,只需禁用alpha通道

从wand.image导入图像
将图像(filename=“pic.png”)作为img:
#移除阿尔法
img.alpha_通道=假
保存(filename=“pic2.png”)
另一种方式

创建与第一个图像尺寸相同的新图像可能更容易,只需在新图像上合成源图像即可

从wand.image导入图像
从wand.color导入颜色
将图像(filename=“pic.png”)作为img:
将图像(宽度=img.width,高度=img.height,背景=颜色(“白色”)作为背景:
bg.复合材料(img,0,0)
#-调整大小300%
定标器=3
调整背景大小(img.width*缩放器,img.height*缩放器)
保存(filename=“pic2.png”)

我已经添加了我正在处理的图像,你能测试一下你的代码,看看我如何在调整大小后使背景变白吗?@Sekai更新了答案,有两个选项可以使背景变白让我试试,我会回来找你的