Replace placeholder area with different image
Replace placeholder area with different image
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: █
- The coordinates of Y within X are not known and can vary.
- X and Z are the same size.
X: ██████████
Y: █
Z: █
- 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
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
Is this what you are trying to do? If not please explain in more detail.
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
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Replace placeholder area with different image
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.
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
- 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
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:
Red:
Blue:
NewImage:
See
https://imagemagick.org/script/compare.php
https://imagemagick.org/Usage/compare/
Image:
Red:
Blue:
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
See
https://imagemagick.org/script/compare.php
https://imagemagick.org/Usage/compare/
Re: Replace placeholder area with different image
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.
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.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Replace placeholder area with different image
You need a shell script.
snibgo's IM pages: im.snibgo.com
Re: Replace placeholder area with different image
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.
Also, I forgot to mention that there can be more than one red rectangle in the source image.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Replace placeholder area with different image
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.
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
- 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
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:
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
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.
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.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Replace placeholder area with different image
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.
Without actual examples, we can't give good advice.
snibgo's IM pages: im.snibgo.com
Re: Replace placeholder area with different image
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.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Replace placeholder area with different image
So, you are not searching for red squares. Okay.
snibgo's IM pages: im.snibgo.com
- 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
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.
Please always provide your IM version and platform, since syntax and scripting may differ.