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 »

fmw42 wrote: 2019-04-07T12:34:45-07:00 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
Thanks, not what I am really looking for due to my conditional tile numbers. I may give it a shot though.

Any word on combining my 2 cmds? Can I just use the pipe delimiter and attach my second command to the first one?


Thanks again

Edit:
This actually seems to work, not sure if it is good code though:

Code: Select all

/usr/bin/magick montage stamp.png -bordercolor None -border 2 -duplicate 12 -tile x2 -background None -geometry +10+1 miff:- | /usr/bin/magick loyalty-card-lips.png -resize 500x -set option:offset "%[fx:h/2]" \( - -resize 480x -bordercolor none -alpha set -background none -rotate -1 \) -gravity North -geometry -0+%[offset]+15 -composite -quality 50 loyalycard.png
The "-" in miff:-, is that always referred later with the "-", or can you use something less confusing, like a word? I tried a T but then it wrote a huge file named T.bmp.
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 »

In the montage, "miff:-" at the end means the output will be written in miff format to stdout, so it goes through the pipe. In magick, "-" in an input position means the input will be read from stdin, which here is a pipe.
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-04-08T02:50:23-07:00 In the montage, "miff:-" at the end means the output will be written in miff format to stdout, so it goes through the pipe. In magick, "-" in an input position means the input will be read from stdin, which here is a pipe.
Thanks
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'm back, I can't get it to work 100% as I intend to and maybe you guys know some way to get it right.

1. I make a template dynamically with the placeholders for stamp, for example according to the following structure:

Code: Select all

switch ($nr_of_stamps) {
    case ($nr_of_stamps == 10):
        $tile = "5x";
        break;
    case ($nr_of_stamps == 12):
        $tile = "4x3";
        break;
    case ($nr_of_stamps == 15):
        $tile = "5x";
        break;
    case ($nr_of_stamps == 18):
        $tile = "6x3";
        break;   
    default:
        $tile = "5x2";
        break;
}
After that base image is created which all works fine, we use the same code structure to fill in the stamps.

Base image:
Image

But that is where it goed wrong. Our rows arent adding up correctly. Here's a sample of the code for 4 stamps, as well as some of the results for various :

Code: Select all

/usr/bin/magick montage stamp004.png \ -bordercolor None -border 2 -duplicate 3 -tile 5x -background None -geometry +10+15 miff:- | \ loyalty-card-botox_9991001.jpg -resize 500x -set option:offset "%[fx:h/2]" \( - -resize 480x%[offset] -bordercolor none -alpha set -background none -rotate -0 \) -gravity North -geometry -0+%[offset]+15 -composite \ \( -size 420x100 -background none -fill white -font "./lib/hand2.ttf" -gravity center label:"First Lastname" \) -gravity North -geometry +0+110 -composite \ -quality 80 3_loyalty-card-botox_9991001.jpg
I do see a pattern, I just lack enough knowledge to know what to exactly change. It looks like the resize section in the magick command screws things up for me. This part

Code: Select all

... -set option:offset "%[fx:h/2]" \( - -resize 480x%[offset]  ...
When the placeholders are filled from border to border it is fine, when resized and there's space left and right i get these issues.
Image

I hope it makes sense.

Edit: I have deleted the height offset resize parameter but it is there because we have dynamic creationof stamps which should always be on the lower half of the image. When left out, you get this for example (which looks nicer stretched all left and right, but doesn't add up at the bottom

Image


Thanks.
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 show a switch command that sets $tile, but no command uses $tile.

You show a montage command that ends with "miff:- | ", so this writes in miff format to stdout, piped to another command. But that other command starts with "loyalty-card-botox_9991001.jpg", not "magick" or any other executable.

We can only check your code if you show all the input images used by that code.
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 »

Ok.

I use these to create a stampcard, people can choose how many stamps/placeholders they want to use:

Image

and the blank placeholder:
Image


That works fine, as intended (10,12,15,18).

Then, we use a 'stamp' to fill the placeholders, e.g.
Image

That results in the images posted.


>> You show a switch command that sets $tile, but no command uses $tile. <<

Yes it does. It's a variable which is set in the code snippet i posted, here it is "-tile 5x":

Code: Select all

/usr/bin/magick montage stamp004.png -bordercolor None -border 2 -duplicate 3 -tile 5x -background None -geometry +10+15 miff:- |  loyalty-card-botox_9991001.jpg -resize 500x -set option:offset "%[fx:h/2]" \( - -resize 480x%[offset] -bordercolor none -alpha set -background none -rotate -0 \) -gravity North -geometry -0+%[offset]+15 -composite \( -size 420x100 -background none -fill white -font "./lib/hand2.ttf" -gravity center label:"First Lastname" \) -gravity North -geometry +0+110 -composite -quality 80 3_loyalty-card-botox_9991001.jpg
Does this clear things up?
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 »

Okay, but the piping "|" in your command goes nowhere, because what follows it is an image filename instead of an executable.

Code: Select all

loyalty.sh: line 1: loyalty-card-botox_9991001.jpg: command not found
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-09T12:40:08-07:00 Okay, but the piping "|" in your command goes nowhere, because what follows it is an image filename instead of an executable.

Code: Select all

loyalty.sh: line 1: loyalty-card-botox_9991001.jpg: command not found
Sorry, that is a paste error since I deleted the paths to my server and accidentally deleted that too. There's a normal /usr/bin/magick after that pipe, like this

Code: Select all

/usr/bin/magick montage stamp004.png -bordercolor None -border 2 -duplicate 3 -tile 5x -background None -geometry +10+15 miff:- | /usr/bin/magick loyalty-card-botox_9991001.jpg -resize 500x -set option:offset "%[fx:h/2]" \( - -resize 480x%[offset] -bordercolor none -alpha set -background none -rotate -0 \) -gravity North -geometry -0+%[offset]+15 -composite \( -size 420x100 -background none -fill white -font "./lib/hand2.ttf" -gravity center label:"First Lastname" \) -gravity North -geometry +0+110 -composite -quality 80 3_loyalty-card-botox_9991001.jpg
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 assume "loyalty-card-botox_9991001.jpg" in your script is the file you have linked as "loyalty-card-lips-backup.jpg".

Your script has...

Code: Select all

-geometry -0+%%[offset]+15
... which is bad syntax. There should be only two numbers prefixed by "+" or "-".
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-09T13:23:29-07:00 I assume "loyalty-card-botox_9991001.jpg" in your script is the file you have linked as "loyalty-card-lips-backup.jpg".

Your script has...

Code: Select all

-geometry -0+%%[offset]+15
... which is bad syntax. There should be only two numbers prefixed by "+" or "-".
The "-backup" file is the reusable source from which the "_991001" is created with placeholder fields for the stamps.

The offset +15 does work though, even though it may be bad syntax. It moves the block with stamps 15 pixel up (if Im not mistaken) so it doesn't go all the way down to the border.
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 thought you said something was wrong?

You should remove "-0" or "+%[offset]" or "+15".
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 »

Yes, the alignment of the stamps is wrong as in the 6 grid image shows the stamps don't match the placeholders on some occassions.

What if I need the +15 added to the offset (which is half the image size). I tried to add it inside the brackets weeks ago but that did not work, this is the only variation that does what I want. But this section isn't part of the problem, since that works just fine :-)
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 »

Then you need two offsets. The second could be:

Code: Select all

-set option:offset2 "%[fx:h/2+15]"
... then use:

Code: Select all

-geometry +0+%[offset2]
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-09T15:25:18-07:00 Then you need two offsets. The second could be:

Code: Select all

-set option:offset2 "%[fx:h/2+15]"
... then use:

Code: Select all

-geometry +0+%[offset2]
I really appreciate your patience here snibgo, however that is still not the issue. I'll try to explain once more, step by step, otherwise I will have to settle for full with stampcard option variables.

Image

A fully written out code (line by line), would look like this then, I post calculated variables here so it is a "working" command, which does not fill it up the way i intended, BUT does show my issue (hopefully):

Base Image size = 500x500:

/usr/bin/magick montage /path/stamp003.png \
-bordercolor None -border 2 -duplicate 3 -tile 4x3 -background None -geometry +10+15 miff:- | \
/usr/bin/magick /path/loyalty-card-botox_9991001.jpg -resize 500x -set option:offset "%[fx:h/2]" \
\( - -resize 480x%[offset] -bordercolor none -alpha set -background none -rotate -0 \) -gravity North -geometry -0+%[offset]+15 -composite \
\( -size 420x100 -background none -fill white -font "./lib/hand2.ttf" -gravity center label:"Name" \) -gravity North -geometry +0+110 -composite \
-quality 80 /path/1557475336_3_loyalty-card-botox_9991001.jpg

1. Sets the stamps to echo for each current value

Code: Select all

/usr/bin/magick montage /path/stamp003.png \
2. Duplicates it X times, based on points, 4 point in this case

Code: Select all

-bordercolor None -border 2 -duplicate 3 -tile 4x3 -background None -geometry +10+15 miff:- | \ 
3. Gets original loyalty card template with circles to put stamps on, resizes it to 500 pixels wide

Code: Select all

/usr/bin/magick /path/loyalty-card-botox_9991001.jpg -resize 500x -set option:offset "%[fx:h/2]" \
4. This step resizes the duplicated stamp (part 1) of the image, stamp 1-n, to a little smaller than the 500 width of the base image, center aligns to the top and moves it 15 pixels below the fold with offset+15.

Code: Select all

\( - -resize 480x%[offset] -bordercolor none -alpha set -background none -rotate -0 \) -gravity North -geometry -0+%[offset]+15 -composite \
^^ This is where it goes wrong in calculating the width to match the placeholders as in the image. I'm trying to wrap my head around where. Removing the +15 at offset does not solve the problem.

5. Add the Name tag (no issues from here on)

Code: Select all

\( -size 420x100 -background none -fill white -font "./lib/hand2.ttf" -gravity center label:"Name" \) -gravity North -geometry +0+110 -composite \ 
6. Write final image

Code: Select all

-quality 80 /path/1557475336_3_loyalty-card-botox_9991001.jpg   

Can I please get an A for effort in trying to explain, lol. Took ages ;-)

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

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
snibgo's IM pages: im.snibgo.com
Post Reply