ImageMagick-更改选定边框的背景颜色

时间:2019-05-23 12:43:47

标签: imagemagick background-image crop bounding-box

我已经具有所选边界框的规格化顶点(例如xmin:0.68,ymin:0.47,xmax:0.94,ymax:0.82),并且我想将此框保存在其他.jpg文件中。此外,在原始图像中,我想使此突出显示的框为全白色。使用Imagemagick是否可以?

3 个答案:

答案 0 :(得分:3)

使用ImageMagick版本6,下面的命令将创建两个输出图像。 (ImageMagick版本7的示例命令在回复中更进一步。)

将使用边界框从输入图像裁剪第一张输出图像,该边界框从w * 0.68xh * 0.47开始,在w * 0.94xh * 0.82结束。

第二个输出将是带有白色部分的输入,该部分与裁剪出的第一张子图像相对应。

convert input.png \
   -set option:distort:viewport "%[fx:(w*0.94)-(w*0.68)]x%[fx:(h*0.82)-(h*0.47)]" \
   \( +clone -distort affine "0,0 -%[fx:w*0.68],-%[fx:h*0.47]" \
   -write result1.png -fill white -colorize 100 \) \
   -set page "%[fx:u.w]x%[fx:u.h]+%[fx:t*(u.w*0.68)]+%[fx:t*(u.h*0.47)]" \
   -flatten result2.png

首先,根据您提供的边界框尺寸,读取输入图像并计算视口以及要裁剪的子图像的尺寸。

然后在括号内创建一个克隆,并进行“扭曲仿射”,实际上是裁剪图像并将其正确放置在该视口中。它将结果写入第一个输出图像“ result1.png”。然后,仍在括号内,用白色填充该裁剪的块。

此后,它将设置分页几何形状,以便最终可以将白色块重新组合到其在输入图像上的原始位置。

通过将白色块展平到输入图像上来完成,然后将第二个输出图像写入“ result2.png”。

使用ImageMagick版本7可以使用稍微简单一些的命令来完成同一件事...

magick input.png \
   \( +clone \
   -crop "%[fx:(w*0.94)-(w*0.68)]x%[fx:(h*0.82)-(h*0.47)]+%[fx:w*0.68]+%[fx:h*0.47]" \
   -write result1.png -fill white -colorize 100 \) \
   -flatten result2.png

直接在“ -crop”操作中进行计算,将分页几何体保存在裁切后的裁片中,以便可以将其展平回到其原始位置而无需重置几何体。

这些都是* nix语法。要使其在Windows中工作,请将连续的反斜杠“ \”更改为插入符号“ ^”,并消除那些转义括号“ \(... \)”的反斜杠。

答案 1 :(得分:2)

从此开始:

enter image description here

,知道纪念碑的左上角在override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "showDetails" { let destination = segue.destination as! DetailsViewController let indexPath = tableView.indexPathForSelectedRow! destination.rideIndex = indexPath.row } } 处,而右下角在func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let destination = mainStoryboard.instantiateViewController(withIdentifier: "DetailsViewController") as! DetailsViewController let indexPath = tableView.indexPathForSelectedRow! destination.rideIndex = indexPath.row self.present(destination, animated: true, completion: nil) } ,则可以使用以下命令将纪念碑提取到文件中:

400,10

enter image description here

并用以下颜色涂上白色:

500,200

enter image description here

或者,为了获得额外的乐趣,使用以下半透明的白色覆盖:

magick photo.jpg -crop 100x190+400+10 extract.jpg

enter image description here


您可以一次完成两项操作:

magick photo.jpg -fill white -draw "rectangle 400,10 500,200" overpainted.jpg

答案 2 :(得分:1)

这是ImageMagick 6中的另一种变体。它将裁切图像并保存,然后将其删除。然后,它使用-region将白色写入该边界框。

这是Unix语法。对于Windows,请从括号中删除\,然后将\行的结尾更改为^。

输入:

enter image description here

convert img.jpg \
\( +clone -crop 100x190+400+10 +repage +write result1.jpg +delete \) \
-region 100x190+400+10 -fill white -colorize 100 +region result2.jpg


enter image description here

enter image description here