current canvas size

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?".
Locked
user001
Posts: 42
Joined: 2015-11-02T01:21:03-07:00
Authentication code: 1151

current canvas size

Post by user001 »

Is it possible to specify the size of an element based on the current canvas size? For example:

Code: Select all

convert 'xc:white[500x300]' \( -size 100x300 xc:red \) -gravity east -composite x:
Without prior knowledge that the current canvas is 500x300, could one create the red stripe with width=100 and height to match that of the canvas on which it will be placed (i.e., 300)? I'm looking for something like `-size 100x%[height]` where %[height] is the height of the canvas.

Thank you.
Last edited by user001 on 2019-12-10T11:51:00-07:00, edited 1 time in total.

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: current canvas size

Post by snibgo »

What version of IM? I suggest you use v7, and "magick".

100 * height would be very large. Perhaps you mean something like 0.2 * height.

Code: Select all

magick xc:white[500x300!] -size %[fx:w*0.2]x%[fx:h] xc:red +write x.png -gravity east -composite x.png
snibgo's IM pages: im.snibgo.com

user001
Posts: 42
Joined: 2015-11-02T01:21:03-07:00
Authentication code: 1151

Re: current canvas size

Post by user001 »

Thanks snigbo. In writing 100x%[height], I meant width=100 and height=%[fx:height]. Sorry for being unclear.

My system has IM 6.9.10-23, but I've also installed 7.0.9-8 alongside. Is it necessary to write the intermediate image? If I remove the `+write`, your command no longer works:

Code: Select all

magick 'xc:white[500x300!]' \( -size '%[fx:w*0.2]x%[fx:h]' xc:red \) -gravity east x:
Thanks.

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: current canvas size

Post by snibgo »

Sorry, my mistake, remove the "+write x.png".

But you have added parentheses to my command. That is wrong. Remove them.

(Why is it wrong? Because open parenthesis start a new image list. That list is empty, it has no images, so w and h are not defined.)
snibgo's IM pages: im.snibgo.com

user001
Posts: 42
Joined: 2015-11-02T01:21:03-07:00
Authentication code: 1151

Re: current canvas size

Post by user001 »

Interesting, thanks for clarifying. If I then want to make further manipulations only to the red part, can I add it to a new image list after creation?

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: current canvas size

Post by snibgo »

If you want to manipulate just the red part, you will need parentheses. You can set the size before then, so w and h are defined:

Code: Select all

magick xc:white[500x300!] -size %[fx:w*0.2]x%[fx:h] ( xc:red -evaluate Multiply 0.5 ) -gravity east -composite x.png
This is Windows syntax. For bash, escape the parenthese \( and \).
snibgo's IM pages: im.snibgo.com

user001
Posts: 42
Joined: 2015-11-02T01:21:03-07:00
Authentication code: 1151

Re: current canvas size

Post by user001 »

Thanks again!

Locked