Page 1 of 2

Replace placeholder area with different image

Posted: 2018-12-12T10:44:43-07:00
by coloring
Given image X, how can I replace placeholder area Y with image Z using Imagemagick?

- The coordinates of Y within X are not known and can vary.
- X and Z are the same size.


X: █████████
Y:
Z:

Re: Replace placeholder area with different image

Posted: 2018-12-12T10:57:32-07:00
by fmw42
I do not understand what you are trying to do. Please clarify and also post your command line that you are using and your ImageMagick version and platform/OS.

Placeholder (blank) image are defined by null: in place of the image file name or by using xc: with no color or xc:none for transparent

Code: Select all

convert -size WxH xc:black xc:black xc:black xc: xc:black ... +append result.gif

Code: Select all

convert -size WxH xc:black xc:black xc:black null: xc:black ... +append result.gif
Is this what you are trying to do? If not please explain in more detail.

Re: Replace placeholder area with different image

Posted: 2018-12-12T11:03:10-07:00
by snibgo
I think the question is: Given image X that contains somewhere within it image Y, how can we replace Y in X with the same-size image Z?

The answer is: do a sub-image search for Y within X. That gives the coordinates of Y within X. Use those coordinates in "-geometry" for compositing Z over X.

Sub-image searching can be slow, but I have a number of methods to make it faster, if needed.

Re: Replace placeholder area with different image

Posted: 2018-12-12T11:07:52-07:00
by fmw42
snibgo -- yes, I think you are correct. I see your point now and would agree that is what he wants and how to do it.

Image:
Image

Red:
Image

Blue:
Image

Code: Select all

compare -metric rmse -subimage-search image.png red.png null:
0 (0) @ 60,0

Code: Select all

convert image.png blue.png -geometry +60+0 -compose over -composite newimage.png
NewImage:
Image

See
https://imagemagick.org/script/compare.php
https://imagemagick.org/Usage/compare/

Re: Replace placeholder area with different image

Posted: 2018-12-12T12:45:05-07:00
by coloring
Thanks snibgo and fmw42. This is exactly what I wanted to do.

Is it possible to use the output of `compare` (60,0) inside `convert` within ImageMagick, or will I have to resort to a shell/batch file to extract and format the output of `compare` before inputting it into `convert`?

I will be using the latest version ImageMagick on Windows and MacOS.

Re: Replace placeholder area with different image

Posted: 2018-12-12T13:08:26-07:00
by snibgo
You need a shell script.

Re: Replace placeholder area with different image

Posted: 2018-12-12T14:35:13-07:00
by coloring
Is there a faster solution? `compare` takes almost 30 seconds on medium-sized images (1024x768).

Also, I forgot to mention that there can be more than one red rectangle in the source image.

Re: Replace placeholder area with different image

Posted: 2018-12-12T14:52:12-07:00
by snibgo
I have faster methods in C that needs IM rebuilding, and in Windows BAT scripts.

For best advice, we need more info. What version of IM, on what platform? What sizes are the images and subimages? Are they photos or what? Sample images would help.

Re: Replace placeholder area with different image

Posted: 2018-12-12T16:59:48-07:00
by fmw42
I have a FFT based normalized cross correlation script, normcrosscorr, that is faster. Also I have a script, maxima, that would allow you to find multiple match points in the correlation (difference) output. See my scripts at my link below. They are bash unix shell scripts and only work on Unix-type systems (Linux, MacOSX, Windows 10 unix and Windows with Cygwin).

You can also do multi-resolution compare by integrating on reduced size version of your image starting with the smallest size and working larger. But that needs your own scripting.

In unix, you can get the match point from compare and put it into a variable as follows:

Code: Select all

var=$(compare -metric rmse -subimage-search image.png red.png null: 2>&1 | cut -d " " -f 4)
echo $var
60,0

Re: Replace placeholder area with different image

Posted: 2018-12-21T14:15:11-07:00
by coloring
Thanks guys. The example image I provided is very close to what I am working with, except that my images are up to 1024x760px in size. Another thing I should have mentioned is that there are multiple red squares that need to be replaced by a blue one.

What I essentially need is a `sed s//g` type of solution, but for images.

Also, although I have access a Unix-type system on a different machine, a BAT solution would be preferred in this case.

Re: Replace placeholder area with different image

Posted: 2018-12-21T14:51:09-07:00
by snibgo
Do you really want to replace red squares? If so, then my Searching an image aggressively might be most suitable. How large are the red squares?

Without actual examples, we can't give good advice.

Re: Replace placeholder area with different image

Posted: 2018-12-21T15:01:33-07:00
by coloring
The red squares represent digits, icons, and sometimes letters in screenshots. They are always smaller than 32x32 (usually about 16x16), and there can be up to 50 instances in a single source screenshot.

Re: Replace placeholder area with different image

Posted: 2018-12-21T15:22:38-07:00
by snibgo
So, you are not searching for red squares. Okay.

Re: Replace placeholder area with different image

Posted: 2018-12-21T17:00:10-07:00
by fmw42
Perhaps you should show a real example of what you want to do, so that we can suggest more relevant solutions and perhaps show you how they work?

Please always provide your IM version and platform, since syntax and scripting may differ.

Re: Replace placeholder area with different image

Posted: 2018-12-26T13:32:50-07:00
by coloring
From:
Image

To:
Image

Please note that fonts are not anti-aliased, so pixel-perfect matching should be possible.
Modifying the data before taking the screenshot is not an option given the amount of screenshots to process and the manual labor it would involve.

I hope this helps.