Is it possible to calculate width and paste min/max images in a row/column?

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?".
kimmyk
Posts: 40
Joined: 2018-11-01T13:07:06-07:00
Authentication code: 1152

Re: Is it possible to calculate width and paste min/max images in a row/column?

Post by kimmyk »

snibgo wrote: 2019-05-10T04:22:54-07:00 loyalty-card-botox_9991001.jpg is the image of lips, with white circles as placeholders for the stamps, correct? Then the heart-stamps should be placed on the placeholders. I can't find where you have posted loyalty-card-botox_9991001.jpg, or code that makes it. That means I can't test your code, or my code.

You are making an image of 4 heart stamps, and resizing that to width 480. I can't see why. If you have 5x3 circle placeholders, then resizing to four-fifths of something seems more logical.

I think the fundamental problem is that you are creating the circle placeholders with some code, then resizing and creating the heart stamps with some different code, and trying to get them to match.

It seems cleaner to create 15 objects. 4 of those have hearts over circles, and the rest are circles only. Then montage those 15 objects and composite the result over the main mage. Windows BAT script:

Code: Select all

%IMG7%magick ^
  stamp_circ.png stamp_heart.png ^
  -compose Over -composite ^
  -duplicate 3 ^
  ( stamp_circ.png -duplicate 10 ) ^
  miff:- | %IMG7%magick montage - ^
  -bordercolor None -border 2 -tile 5x3 -background None -geometry +10+15 ^
  miff:- | %IMG7%magick ^
  loyalty-card-lips-backup.jpg ^
  -set option:NEWWIDTH "%%[fx:w*0.8]" ^
  ( - -resize %%[NEWWIDTH]x ^
  ) ^
  -gravity South ^
  -compose Over -composite ^
  stamp_out.png
Image
Correct, the blank card is created first, with the same code though, just with the amount of stamps a store owner would like to use (sets a number from a pull down).

Of course this
It seems cleaner to create 15 objects. 4 of those have hearts over circles, and the rest are circles only. Then montage those 15 objects and composite the result over the main mage
is the ideal situation, I could get that working due to lack of skillz;-).

I had the stamp with background circle at first, but i got empty space in stead of empty circles.

Would your code also work when somebody fills in 6 stamps slots, or 12, 18, 24 etc? Is it flexible in filling up the lower half of the image?

Thanks for taking time to dig in. You rock!
Last edited by kimmyk on 2019-05-10T04:54:30-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Is it possible to calculate width and paste min/max images in a row/column?

Post by snibgo »

I should add: perhaps the first "magick" and second "magick montage" can be combined.
kimmyk wrote:Would your code also work when somebody fills in 6 stamps slots, or 12, 18, 24 etc?
Yes. My code has essentially three things you would vary:
- the duplications of heart-over-circle, "3" in the code, making 4 in total;
- the duplications of circle only, "10" in the code, making 11 in total;
- the "-tile" setting, "5x3" in the code.

Clearly, 5 * 3 = 4 + 11 = 15. Your script would set these numbers in variables.

If you want an image with zero heart-over-circle images, remove the first three lines from the first "magick" command, or "-delete" those images.
kimmyk wrote:Is it flexible in filling up the lower half of the image?
I resize the montage of 15 objects to NEWWIDTH, which is 80% of the width of the main image, "%%[fx:w*0.8]". You might prefer 90% or whatever. But the resulting height of the montage might exceed 50% of the height of the main image (or even 100% of that height!). You can impose both limits like this:

Code: Select all

  -set option:NEWWIDTH "%%[fx:w*0.8]" ^
  -set option:NEWHEIGHT "%%[fx:h*0.3]" ^
  ( - -resize %%[NEWWIDTH]x%%[NEWHEIGHT] ^
  ) ^
This resizes to fit in a box that is 80% of the main width and 30% of the main height.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Is it possible to calculate width and paste min/max images in a row/column?

Post by snibgo »

You might want some jitter, randomness of the hearts over the circles, while keeping the circles in a fixed grid. My code above duplicates heart-over-circle, so can't provide jitter.

A solution is to first duplicate the circles and the hearts, then "-layers composite" with jitter. The hearts and circles are the same size, so we add a border to the circles so the jittered hearts won't go beyond the edges.

Code: Select all

%IMG7%magick ^
  stamp_circ.png -gravity Center -bordercolor None -border 10 ^
  +write mpr:CIRC ^
  -duplicate 3 ^
  NULL: ^
  ( stamp_heart.png -duplicate 3 ^
    -background None ^
    -rotate %%[fx:rand()*30-15] ^
    -repage +%%[fx:rand()*20-10]+%%[fx:rand()*20-10] ^
  ) ^
  -gravity Center ^
  -compose Over -layers composite ^
  ( mpr:CIRC -duplicate 10 ) ^
  miff:- | %IMG7%magick montage - ^
  -bordercolor None -border 2 -tile 5x3 -background None -geometry +10+15 ^
  miff:- | %IMG7%magick ^
  loyalty-card-lips-backup.jpg ^
  -set option:NEWWIDTH "%%[fx:w*0.8]" ^
  -set option:NEWHEIGHT "%%[fx:h*0.5]" ^
  ( - -resize %%[NEWWIDTH]x%%[NEWHEIGHT] ^
  ) ^
  -gravity South ^
  -compose Over -composite ^
  stamp_jitter.png
Image
snibgo's IM pages: im.snibgo.com
kimmyk
Posts: 40
Joined: 2018-11-01T13:07:06-07:00
Authentication code: 1152

Re: Is it possible to calculate width and paste min/max images in a row/column?

Post by kimmyk »

snibgo wrote: 2019-05-10T06:24:08-07:00 You might want some jitter, randomness of the hearts over the circles, while keeping the circles in a fixed grid. My code above duplicates heart-over-circle, so can't provide jitter.

A solution is to first duplicate the circles and the hearts, then "-layers composite" with jitter. The hearts and circles are the same size, so we add a border to the circles so the jittered hearts won't go beyond the edges.

Code: Select all

%IMG7%magick ^
  stamp_circ.png -gravity Center -bordercolor None -border 10 ^
  +write mpr:CIRC ^
  -duplicate 3 ^
  NULL: ^
  ( stamp_heart.png -duplicate 3 ^
    -background None ^
    -rotate %%[fx:rand()*30-15] ^
    -repage +%%[fx:rand()*20-10]+%%[fx:rand()*20-10] ^
  ) ^
  -gravity Center ^
  -compose Over -layers composite ^
  ( mpr:CIRC -duplicate 10 ) ^
  miff:- | %IMG7%magick montage - ^
  -bordercolor None -border 2 -tile 5x3 -background None -geometry +10+15 ^
  miff:- | %IMG7%magick ^
  loyalty-card-lips-backup.jpg ^
  -set option:NEWWIDTH "%%[fx:w*0.8]" ^
  -set option:NEWHEIGHT "%%[fx:h*0.5]" ^
  ( - -resize %%[NEWWIDTH]x%%[NEWHEIGHT] ^
  ) ^
  -gravity South ^
  -compose Over -composite ^
  stamp_jitter.png
Image
Awesome. If you read back this post, that was one of my (sub) questions as well. I called it rotate though but this is exactly what I wanted giving it the feel of a real stamp, which obviously isn't always symmetric.

I'll see if I can rewrite your code into my shell (PHP) code.

Thanks again.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Is it possible to calculate width and paste min/max images in a row/column?

Post by snibgo »

Yes, I remembered you wanted randomness. If you want only random rotation without translation, replace "-repage +%%[fx:rand()*20-10]+%%[fx:rand()*20-10]" with "+repage".

"rand()" is a random number between 0.0 and 1.0, so "%%[fx:rand()*30-15]" is a random number between -15 and +15.

For bash, change every ^ to \, and every %% with %, and probably escape parentheses \( and \), and some arguments may need quoting.
snibgo's IM pages: im.snibgo.com
kimmyk
Posts: 40
Joined: 2018-11-01T13:07:06-07:00
Authentication code: 1152

Re: Is it possible to calculate width and paste min/max images in a row/column?

Post by kimmyk »

I'll report back with some more test results when I have changed the scripts.
Cheers.
kimmyk
Posts: 40
Joined: 2018-11-01T13:07:06-07:00
Authentication code: 1152

Re: Is it possible to calculate width and paste min/max images in a row/column?

Post by kimmyk »

snibgo wrote: 2019-05-10T07:20:26-07:00 Yes, I remembered you wanted randomness. If you want only random rotation without translation, replace "-repage +%%[fx:rand()*20-10]+%%[fx:rand()*20-10]" with "+repage".

"rand()" is a random number between 0.0 and 1.0, so "%%[fx:rand()*30-15]" is a random number between -15 and +15.

For bash, change every ^ to \, and every %% with %, and probably escape parentheses \( and \), and some arguments may need quoting.
Fantastic! I tweaked it here and there a bit to increase the size and added the text section and it works exactly as I initially wanted now when i started this thread over a month ago.

Thanks a lot snibgo, I owe you a beer or two I would never have thought of the complete syntax you gave me on my own.
Post Reply