Page 1 of 3

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

Posted: 2019-04-04T14:49:32-07:00
by kimmyk
Hi all,

Let's say we have a square image of 500x500. We want to use the bottom 500x200 for loyalty stamps.

Suppose we know the max number of images (different for each use), could we lookup that number (we can, let's say it is 10 stamps), divide the width by that number and distribute evenly somhow so it will oook nice?

For example, if
n=5, we get 1 row of 5 stamps (50 by 50px).
n=10, 2 rows of 5 columns,
n=12, 2x6 (33x33px),
n=20 => 2x10 (25x25px)

I can see we can pre-calculate some of the options before writing the stamps on the image in the set locations, but is there a programmatic way of doing this so it always calculates the most economic/best filling amount of space/images?

It's so very versatile that I figured I ask some pros here before doing it the dumb way ;-)

Thanks a ton.

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

Posted: 2019-04-04T19:54:04-07:00
by fmw42
Suppose we know the max number of images (different for each use), could we lookup that number (we can, let's say it is 10 stamps), divide the width by that number and distribute evenly somhow so it will oook nice?
This is not very clear to me. Could you explain in more detail or differently and perhaps provide a diagram of what you are trying to do.

Divide the width of what? Distribute in what way to "look nice"? What does "nice" mean?

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

Posted: 2019-04-05T00:01:12-07:00
by kimmyk
Hi,

The width of the original image (the stamp card).
"Nice" means evenly distributed:-).


Do these images help at all? The smileys would be the amount of total stamps, but those are not always the same. It should become a setting/PHP variable that we give later.

Image
Image
Image
Image

Thanks for taking time to read the post and give possible pointers.

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

Posted: 2019-04-05T10:11:45-07:00
by fmw42
Still a bit confused. Do you have separate images for the "Some Stuff" and for each smiley image? And do you want to resize the width of any smiley image to same width as the "Some Stuff" image? If so, what do you need to do with the height of the smiley images? If you do not care, then just compute the width of the "Some Stuff" image once. Then loop over each smiley image and resize it and -append the two.

Do I misunderstand what you are trying to do?

In IM 7, you can do

Image

Image

(unix syntax)

Code: Select all

magick some_stuff.png -set option:wd "%w" \( smileys.png -resize "%[wd]x" \) -append result.png
Image

In IM 6, you will have to do

Code: Select all

convert some_stuff.png smileys.png \
-set option:distort:viewport "%[fx:u.w]x%[fx:u.h]-%[fx:(u.w-v.w)/2]-%[fx:(u.h-v.h)/2]" \
-distort SRT "%[fx:(t==1)?(u.w/v.w):1] 0" \
-append some_stuff_smileys.png
For Windows change the end of line \ to ^ and change \( ... \) to ( ... )

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

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

Posted: 2019-04-05T13:51:56-07:00
by kimmyk
The Some Stuff is the upper half of 1 image, the white part where the stamps/smileys are on, will be a section for the stamps and in the end result will of course be more integrated in the whole square image.

The smileys are the stamps, it is 1 smiley image (the stamp, which can be a coffee bean/sushi roll or whatever) which will be repeated just like a regular stamp/loyalty in-store card.

We keep track of the amount of stamps a person has and add to/reset to zero accordingly.

The height of the smiley doesn't really matter as long as the stamps (smileys) keep their aspect ratio (and maybe a max width/height to keep huge stamps from showing up on the image).

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

Posted: 2019-04-05T14:36:06-07:00
by snibgo
What is the minimum and maximum number of stamps in an image?

If the maximum is reasonable, eg n=20, a possible approach is to have a lookup table that specifies the number of columns and rows for each n, and how many are to be removed from the last row. Eg for n=14, perhaps we want a grid of 5x3, with one removed.

Then an IM command would tile this number of stamps (including a border around each stamp), and remove the surplus, and resize down to 500x200.

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

Posted: 2019-04-05T15:19:39-07:00
by fmw42
You can montage all the stamp images together (using -geometry +0+0 so the touch) and then use my command above to resize the montaged stamps to the same width as the blue image.

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

Posted: 2019-04-06T01:51:02-07:00
by kimmyk
Thanks for the replies. I'll have a go at it this week.

@Fred, we updated to v7 after last year's tips and tricks you provided (you rock!).

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

Posted: 2019-04-06T01:55:10-07:00
by kimmyk
snibgo wrote: 2019-04-05T14:36:06-07:00 What is the minimum and maximum number of stamps in an image?

If the maximum is reasonable, eg n=20, a possible approach is to have a lookup table that specifies the number of columns and rows for each n, and how many are to be removed from the last row. Eg for n=14, perhaps we want a grid of 5x3, with one removed.

Then an IM command would tile this number of stamps (including a border around each stamp), and remove the surplus, and resize down to 500x200.
That's about right. We don't know the number of stamps an end user would choose though, but it should be safe to pick a certain number for sure.

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

Posted: 2019-04-06T02:42:17-07:00
by kimmyk
fmw42 wrote: 2019-04-05T15:19:39-07:00 You can montage all the stamp images together (using -geometry +0+0 so the touch) and then use my command above to resize the montaged stamps to the same width as the blue image.
I guess it is safe to assume I can also use a cool -rotate [php]rand(0,360)[/php] command in there to give them a little "hand stamped" feeling, unless they are all created in one go.

I've been checking out your montage idea, I have a question about that.

Code: Select all

montage smiley[1-7].png -tile x3 -geometry +1+1  loyalycard.jpg
^^ That code for example, it could work for me, as it will always use 3 rows. I can play around with that, but here the [1,x] reference the array of numbers there as image names, how do i just reapeat the same image X times? A loop would allow me to rotate each onbe, but I assume that won't work with tile as the whole point there is to tile them together by calculating how many there are.

Last but not least, here's a more accurate picture of a 'real' idea of where stamps would eventually go. Just a quick demo but it makes a better sample of why not just append the stamps under a header image.

Image

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

Posted: 2019-04-06T05:53:19-07:00
by snibgo
kimmyk wrote:how do i just reapeat the same image X times? ... I can also use a cool -rotate

Code: Select all

magick rose: -bordercolor None -border 10 -duplicate 9 -background None -rotate %[fx:rand*10-5] +append out.png

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

Posted: 2019-04-06T06:06:19-07:00
by kimmyk
snibgo wrote: 2019-04-06T05:53:19-07:00
kimmyk wrote:how do i just reapeat the same image X times? ... I can also use a cool -rotate

Code: Select all

magick rose: -bordercolor None -border 10 -duplicate 9 -background None -rotate %[fx:rand*10-5] +append out.png
That looks like a plan ;-) Although it leaves out the tile feature now.

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

Posted: 2019-04-07T05:36:33-07:00
by kimmyk
Okay, I finally got it exactly as I need it.

Just 2 more questions of minor importance.

I have two command working, and I merge the stamps from cmd1 into the image with cmd2.

These are just some variables I will use later:

Code: Select all

$input         = "loyalty-card-lips.png";
$stamps        = "stamp.png";
$points        = 21 - 1;
$output_stamps = "your_stamps.png";
$output        = "loyalycard.png";
$gravity       = "South";
This is the First cmd which create the stamps:

Code: Select all

"/usr/bin/magick montage " . $stamps . " -bordercolor None -border 2 -duplicate " . $points . " -tile 5x  -background None -geometry +15+1 " . $output_stamps;
This is the second cmd which adds the stamps onto my image:

Code: Select all

"/usr/bin/magick " . $input . " -resize 500 \( " . $output_stamps . " -resize x240 -bordercolor none -alpha set -background none -rotate -1 \)  -gravity " . $gravity . " -geometry -0-0 -composite -quality 50 " . $output;
The questions left open are:
1. The rotate command posted above did not work for me. Is there any other way to display the stamps somewhat random, a bit offset and rotated, like it was really stamped manually?
2. I tried to combine the two command into 1, but i failed with the several variations. Is there a way that also is better for the resources, or is this best pactice as is?

Here's some of my final samples for 5, 12 and 21 stamps. I'll add some logic to the mix later so when there are many stamps (or very little) the tiles are increased or decreased.

Thanks for the help Fred and snibgo.

Image
Image
Image

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

Posted: 2019-04-07T07:44:07-07:00
by snibgo
Your "-resize -1" does work, as we can see. Change this to "resize -10" to make it more obvious.

But that rotates the group. To rotate the individual stamps, you need to put that in the montage. Unfortunately, that can't take a "%[fx:...]" expression.

This works:

Code: Select all

magick montage rose: -border 5 -duplicate 4 -rotate -10 x.png
But this fails:

Code: Select all

magick montage rose: -border 5 -duplicate 4 -rotate %[fx:rand*10-5] x.png

montage: invalid argument for option '-rotate': %[fx:rand*10-5] @ error/montage.c/MontageImageCommand/1400.
Perhaps montage can be enhanced to allow this.

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

Posted: 2019-04-07T12:34:45-07:00
by fmw42
You can use magick to rotate the image randomly and then pipe to magick montage. Here I alternate the rotation direction for each copy.

Code: Select all

magick rose: -duplicate 4 -background none -rotate "%[fx:sign(mod(t,2)-1)*30*rand()]" miff:- | magick montage - -tile 3x -geometry +5+5 -background none result.png