Replace placeholder area with different image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace placeholder area with different image

Post by fmw42 »

What do you want to place there? Can you show us the image or the text you want?

If you want to quickly find some colored region that is of a color not in your image (such as a transparent area or red in an image that has no red), the quickest way is to use connected components processing after converting the image to binary. Find the region in the binary image, get its crop coordinates and then crop the original image. See

https://imagemagick.org/script/connected-components.php

If you provide the image or text you want to put there I can show you the commands
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Replace placeholder area with different image

Post by snibgo »

I think the goal is to change the given "from" image to the given "to" image. For example, the rasterized text "Hamburg (HAM)" should be changed to the rasterized text "Bergen (BGO)". Then I guess the goal is to do the same substitutions for many other images.

First step is to make two subimages, one for each of the rasterized texts. These can be made from crops of the samples. Call these, say, "from.png" and "to.png".

Then a script seaches the input image for "from.png". Eg "magick compare -subimage-search -metric RMSE mainimage.png from.png NULL:". If it is found, we get those coordinates. The script then pastes "to.png" in the same location: "magick mainimage.png to.png -geometry +XX+YY main_changed.png".

Those commands can be put in a loop to process any number of files.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace placeholder area with different image

Post by fmw42 »

The problem is that compare is too slow. That is why I suggested using CCL to extract the one color regions crop box. Then the other image can be placed at the same offsets. That would be faster. The proviso is that each color must be unique in the image. So if you have multiple images, you would be best to preprocess your image so that all the regions have different colors that you know and the rest of the image was black or transparent.

If compare is too slow and you cannot make unique colors in the image, then try speeding up compare using different resolution sizes and limit the search for the better resolutions. Often just one reduce resolution will speed things up quite a bit. Alternately, if on Unix-like system, try my bash unix Imagemagick script, normcrosscorr, to do FFT based normalized cross correlation. You need to install the FFTW delegate to use that script.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Replace placeholder area with different image

Post by snibgo »

Yes, compare may be too slow. I have BAT scripts and process modules that are much faster ("multi-scale").

But I'm not sure I understand the problem yet.
snibgo's IM pages: im.snibgo.com
Post Reply