[SOLVED] squaring cropped image nightmare

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
User avatar
Draoidh
Posts: 69
Joined: 2012-07-03T11:29:44-07:00
Authentication code: 13
Location: soon to be independent Scotland

[SOLVED] squaring cropped image nightmare

Post by Draoidh »

I have successfully been using the following code to square images:

Code: Select all

convert rect.png  \( +clone -rotate 90 +clone -mosaic \) +swap -gravity center -compose Src -composite square.png
working example

Input image:

Code: Select all

 convert  \( \( +size xc:blue xc:green xc:red  -append \) -rotate -90 -scale 100x50!  \) rect2.png
Image
squared:
Image

However, this doesn't work for images that I have cropped as follows: although the rectangular image that I am creating by upsizing and cropping has the correct size (100x50), the squared image is not 100x100 as it should be but 105x110.

problem example

Input image:

Code: Select all

 convert  \( \( +size xc:blue xc:yellow xc:red  -append \) -rotate -90 -scale 100x50! \
    -resize 110%  -gravity center -crop 100x50+0+0  \) rect1.png
Image
squared:
Image


I am at my wits' end!
Last edited by Draoidh on 2012-09-29T05:54:02-07:00, edited 1 time in total.
User avatar
Draoidh
Posts: 69
Joined: 2012-07-03T11:29:44-07:00
Authentication code: 13
Location: soon to be independent Scotland

Re: squaring cropped image nightmare

Post by Draoidh »

After all this, I had one last attempt:

Code: Select all

+repage
after the crop.

And that fixed it!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: squaring cropped image nightmare

Post by fmw42 »

Your use of a rotated version and mosaic (canvas filling composite) is a rather clever method.

But, why not just use -background black -gravity center -extent ...

see
http://www.imagemagick.org/script/comma ... php#extent
http://www.imagemagick.org/Usage/crop/#extent


max=`convert rect1.png -format "%[fx:max(w,h)]" info:`
convert rect1.png -gravity center -background black -extent ${max}x${max} rect1_tmp1.png


In IM7 one will be able to extract the dimensions and use them in one command line, as I understand it.

In IM 6, this is another way to do it in one command line:


convert rect1.png +repage \
-define option:distort:viewport="%[fx:max(w,h)]x%[fx:max(w,h)]-%[fx:(max(w,h)-w)/2]-%[fx:(max(w,h)-h)/2]" \
-virtual-pixel black -filter point -distort SRT 0 +repage rect1_tmp2.png

see
http://www.imagemagick.org/Usage/distor ... red_square
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: squaring cropped image nightmare

Post by anthony »

Fred... The -define should not require option... -define also can NOT use percent escapes. -- SORRY

-set REQUIRES an option; prefix to set a per-image artifact, rather than a global variable, BUT can use percent escapes.

IMv7 allows the -distort function to use either a per-image define, or a global setting to set control defines.

Here is the fixed version of your solution...

Code: Select all

convert rect1.png +repage \
    -set option:distort:viewport '%[fx:max(w,h)]x%[fx:max(w,h)]-%[fx:(max(w,h)-w)/2]-%[fx:(max(w,h)-h)/2]' \
    -virtual-pixel black -filter point -distort SRT 0 +repage rect1_tmp2.png
See centered square crop examples
http://www.imagemagick.org/Usage/distor ... red_square

Code: Select all

  size='%[fx: w>h ? h : w ]'
  offset_x='%[fx: w>h ? (w-h)/2 : 0 ]'
  offset_y='%[fx: w>h ? 0 : (h-w)/2 ]'
  viewport="${size}x${size}+${offset_x}+${offset_y}"

  convert worldmap_sm.jpg  -set option:distort:viewport "$viewport" \
          -filter point -distort SRT 0  +repage   viewport_square.gif
The example uses shell variables perly as a string macro, to make the LONG line just a bit easier to follow, but it is really only one single command. The use of max(..) function is also acceptable and achieves the same results.

In IMv7 you can use percent escapes directly in operators, for example crop, or extent, or in other global -set variables (allowing you to break up the calculations in smaller steps).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: squaring cropped image nightmare

Post by fmw42 »

Anthony wrote:Fred... The -define should not require option... -define also can NOT use percent escapes. -- SORRY

-set REQUIRES an option; prefix to set a per-image artifact, rather than a global variable, BUT can use percent escapes.
Thanks for the clarification. Perhaps add that to your notes on the distort page for virtual canvas.

I tried your solution earlier and it worked. So I tried adding option to the -defined and I would have sworn that it worked. But trying it again, I see I am wrong. Thanks for correcting this. I will have to remember about -set allowing % escapes and -define not. That is an important point for IM 6.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: squaring cropped image nightmare

Post by anthony »

It linked from that examples.

The specific example also starts with the sentence....
If you use the "-set" option to set the 'viewport' of the resulting image, you can include Percent Escapes in the assigned value.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: squaring cropped image nightmare

Post by fmw42 »

Sometimes I am so focused on the code, I miss the important facts. "Can't see the forest for the trees"
User avatar
Draoidh
Posts: 69
Joined: 2012-07-03T11:29:44-07:00
Authentication code: 13
Location: soon to be independent Scotland

Re: squaring cropped image nightmare

Post by Draoidh »

fmw42 wrote:Your use of a rotated version and mosaic (canvas filling composite) is a rather clever method.
Much as I would like to pat myself on the shoulder, but I can't take the credit for this.

It'll be a while yet until I get the all singing and dancing IM 7 on Debian.
User avatar
Draoidh
Posts: 69
Joined: 2012-07-03T11:29:44-07:00
Authentication code: 13
Location: soon to be independent Scotland

Re: squaring cropped image nightmare

Post by Draoidh »

fmw42 wrote:Your use of a rotated version and mosaic (canvas filling composite) is a rather clever method.
But, why not just use -background black -gravity center -extent ...
[...]
In IM7 one will be able to extract the dimensions and use them in one command line, as I understand it.
I just had to remind myself why I chose this option (admittedly without knowing how it does what it does).

The example that I posted here is simplified. In reality I am first creating picture by stitching two together and applying borders. I find your -extend solution more elegant and readable but it requires specification of the square geometry, which my solution doesn't.

As I am still on IM 6.7 I would have to create my image, determine maximum dimension and then square the image in a separate step.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [SOLVED] squaring cropped image nightmare

Post by fmw42 »

Anthony's viewport method allows it to be calculated in the same command line. You just need to use parenthesis processing (and possibly clones) to separate the components of your command
Post Reply