New to Imagemagick, need something to be clarified

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?".
Post Reply
plakar
Posts: 8
Joined: 2019-06-08T14:52:43-07:00
Authentication code: 1152

New to Imagemagick, need something to be clarified

Post by plakar »

Hello,

I am very new to Imagemagick, trying to understand how the command lines are working, and i am having some trouble.

First, i don't understand what the keyword convert does.
Second, i don't understand how the inputs are handled. How do you transform an image, and put it as an input of another command line ?

For context, if needed, i was trying to merge two pictures with an offset, my simple idea was to crop the one that will be overlayed, and append the result with the other image i need to merge with. So the cropped command is like this
magick convert -crop image1.png 1500x534+0x0 crop.png (crop from 995 height to 534)
and what i tried when i wanted to append
magick convert -crop image1.png 1500x534+0x0 image2.png -append append.png

This crops image2 as well tho. I tried putting parenthesis, since it seems intuitive, but it doesn't seem to work like that.

On a side note i can recrop image2 to its original size and i get the expected result, but that looks silly ^^
On a side side note, i was also wondering if you could crop an image by x pixels, without having to know the initial resolution of the image

Thanks !


Edit : Well, funny thing, i litteraly found out the smush command is basically exactly what i'm trying to achieve. I Could still use some guidance on the question i raised tho ^^
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: New to Imagemagick, need something to be clarified

Post by snibgo »

plakar wrote:magick convert -crop image1.png 1500x534+0x0 crop.png
I suggest you remove "convert", because that makes your v7 IM behave like v6, which is usually not wanted.

The normal sequence is: read the image, process it, and write it. Your processing is "-crop", and that needs the numbers "1500x534+0+0" to follow immediately. Note the format is "WxH+X+Y" where WHXY are numbers, and the last two are prefixed with "+" or "-". So:

Code: Select all

magick image1.png -crop 1500x534+0+0 crop.png
snibgo's IM pages: im.snibgo.com
plakar
Posts: 8
Joined: 2019-06-08T14:52:43-07:00
Authentication code: 1152

Re: New to Imagemagick, need something to be clarified

Post by plakar »

Thanks, it works when i read the image first indeed. Is reading the image after the process for when you want to apply the process to several images then ?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: New to Imagemagick, need something to be clarified

Post by snibgo »

IM works with lists of images. Most operations ("-crop" etc) operate on all the images in the current list. Open parenthesis "(" starts a new list; close parenthesis ")" ends the list and merges resulting images into the outer list.

So this will crop both images:

Code: Select all

magick convert image1.png image2.png -crop 1500x534+0+0 +append out.png
If you just want to crop the first image, this will work because "-crop" occurs before the second image is read...

Code: Select all

magick convert image1.png -crop 1500x534+0+0 image2.png +append out.png
... but if you only want to crop the second image then...

Code: Select all

magick convert image1.png ( image2.png -crop 1500x534+0+0 ) +append out.png
EDIT: corrected typo.
snibgo's IM pages: im.snibgo.com
plakar
Posts: 8
Joined: 2019-06-08T14:52:43-07:00
Authentication code: 1152

Re: New to Imagemagick, need something to be clarified

Post by plakar »

snibgo wrote: 2019-06-08T16:10:07-07:00 IM works with lists of images. Most operations ("-crop" etc) operate on all the images in the current list. Open parenthesis "(" starts a new list; close parenthesis ")" ends the list and merges resulting images into the outer list.

So this will crop both images:

Code: Select all

magick convert image1.png image2.png -crop 1500x534+0+0 +append out.png
If you just want to crop the first image, this will work because "-crop" occurs before the second image is read...

Code: Select all

magick convert image1.png -crop 1500x534+0+0 image2.png +append out.png
... but if you only want to crop the second image then...

Code: Select all

magick convert image1.png ( image2.png -crop 1500x534+0+0 ) +append out.png
EDIT: corrected typo.
Oh ok, i get where my problem was from. I thought parenthesis weren't a thing because i tried to use them with and without escaping them with \ but you actually escape parenthesis with ` in powershell.
Thanks for the explanation, and the clue, now i actually understand how all that works ! Much clearer now

Thanks, i will be sure to check those links out (first one is dead tho)
plakar
Posts: 8
Joined: 2019-06-08T14:52:43-07:00
Authentication code: 1152

Re: New to Imagemagick, need something to be clarified

Post by plakar »

Well i have another question if i may (sorry for the double post, i don't really see where rules are listed).

Isn't an offset of the form +x+y ? The second part (+y) doesn't seem to have any influence on the result of smush, even if it does seem to be grammatically correct
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: New to Imagemagick, need something to be clarified

Post by snibgo »

The offset in "-smush" should be a simple number, like 4 or -6. It could be floating-point but that will be rounded to an integer.
snibgo's IM pages: im.snibgo.com
plakar
Posts: 8
Joined: 2019-06-08T14:52:43-07:00
Authentication code: 1152

Re: New to Imagemagick, need something to be clarified

Post by plakar »

Offset is always a single number ? Isn't there a simple (inbuilt?) way to overlay an image on top of another with just an x and y axis value then (with pixel as a measure) ?

Edit : i'm guessing i should try and search for something with layers

Edit 2 : Ok, seems like what i'm trying to use is actually composite with -displace or -geometry option, but i'm not able to understand how it works.

Tried

Code: Select all

magick 1.png 2.png composite -displace +100+100 out.png

Code: Select all

magick 1.png 2.png composite -displace 0x0+100+100 out.png
On a side note, the documentation https://imagemagick.org/script/composite.php put the process action composite before reading the images, like that :

Code: Select all

magick composite -gravity center smile.gif  rose: rose-over.png

isn't the reading supposed to be before ?
plakar
Posts: 8
Joined: 2019-06-08T14:52:43-07:00
Authentication code: 1152

Re: New to Imagemagick, need something to be clarified

Post by plakar »

deleted (can't find how to delete)

Edit : Finally found a way to achieve it

Code: Select all

magick `( `( -size <xFinal>x<yFinal> xc:none `) 1.png -composite `) 2.png -geometry +<xOffset>+<yOffset> -composite out.png
I create a transparent canvas of the size of rectangle surrounding the overlapping images, put the first image (with a possible offset if i don't want it to be attached to the top left corner, which would come just after the "1.png"), compose it, and add the second image with the offset needed and compose it with the first composition.

Would be cool if there was a command that does that automatically tho, something like

Code: Select all

magick overlap source.png destination.png -geometry +x+y output.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: New to Imagemagick, need something to be clarified

Post by snibgo »

plakar wrote:magick composite -gravity center smile.gif rose: rose-over.png
Note that is the "magick composite" command (no hyphen). Don't confuse that with the "magick" command with a "-composite" (with hyphen) operation.

The syntax for "magick composite" is different to simple "magick". "magick composite" is rather old, and not as powerful as "magick", and is only supplied for backwards compatability. My advice: don't use it.

For placing one image over another, I would usually:

Code: Select all

magick 1.png 2.png -geometry +X+Y -composite out.png
Note that "-geometry" comes before "-composite".
snibgo's IM pages: im.snibgo.com
plakar
Posts: 8
Joined: 2019-06-08T14:52:43-07:00
Authentication code: 1152

Re: New to Imagemagick, need something to be clarified

Post by plakar »

Yeah, the fact that -geometry is before -composite is what confused me for a bit, it's not really an option to the command -composite, but an option to the arguments of the command -composite.

And yeah, your way works for when the output is the same size as 1.png (or when 2.png is inside 1.png), which is not my case, so i have to create a bigger background on which i can then place 1.png and 2.png.

Well, glad i could find a working solution, thanks a lot for the guidance !
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: New to Imagemagick, need something to be clarified

Post by snibgo »

In your "final way", the parentheses are harmless but useless, so you can simplify:

Code: Select all

magick -size <xFinal>x<yFinal> xc:none 1.png -composite 2.png -geometry +<xOffset>+<yOffset> -composite out.png
snibgo's IM pages: im.snibgo.com
plakar
Posts: 8
Joined: 2019-06-08T14:52:43-07:00
Authentication code: 1152

Re: New to Imagemagick, need something to be clarified

Post by plakar »

I see, you have to actually kinda read it in reverse to understand more easily what the output is supposed to... output ^^
Well, good to know :D, thanks
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: New to Imagemagick, need something to be clarified

Post by fmw42 »

(first one is dead tho)
Yes, it was lost some weeks ago and cannot be retrieved. So I will take that out of my list. It was advice how to ask intelligent questions on this forum.
Post Reply