Search code examples
imagemagickbackground-imagecropbounding-box

ImageMagick - change the background color of the selected bounding box


I already have the normalized vertices of my selected bounding box (e.g xmin: 0.68, ymin: 0.47, xmax: 0.94, ymax: 0.82) and I want to save this box in an other .jpg file. Furthermore, in the original image I want to make this highlighted box all white. Is this possible using Imagemagick?


Solution

  • Starting with this:

    enter image description here

    and knowing the top-left corner of the monument is at 400,10 and the bottom-right is at 500,200, you can extract the monument to a file with:

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

    enter image description here

    and overpaint in white with:

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

    enter image description here

    Or, for extra fun, overpaint in semi-transparent white with:

    magick photo.jpg -fill "rgba(255,255,255,0.5)" -draw "rectangle 400,10 500,200" overpainted.jpg
    

    enter image description here


    You can do both operations in one go with:

    magick photo.jpg \( +clone -fill white -draw "rectangle 400,10 500,200" -write overpainted.jpg +delete \) -crop 100x190+400+10 extract.jpg