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?".
coloring
Posts: 82
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Replace placeholder area with different image

Post 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:
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 »

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.
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 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.
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 »

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/
coloring
Posts: 82
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Replace placeholder area with different image

Post 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.
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 »

You need a shell script.
snibgo's IM pages: im.snibgo.com
coloring
Posts: 82
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Replace placeholder area with different image

Post 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.
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 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.
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 »

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
coloring
Posts: 82
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Replace placeholder area with different image

Post 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.
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 »

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.
snibgo's IM pages: im.snibgo.com
coloring
Posts: 82
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Replace placeholder area with different image

Post 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.
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 »

So, you are not searching for red squares. Okay.
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 »

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.
coloring
Posts: 82
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Replace placeholder area with different image

Post 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.
Post Reply