Page 1 of 1
[SOLVED] squaring cropped image nightmare
Posted: 2012-09-28T09:44:56-07:00
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

squared:
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

squared:
I am at my wits' end!
Re: squaring cropped image nightmare
Posted: 2012-09-28T09:53:00-07:00
by Draoidh
After all this, I had one last attempt:
after the crop.
And that fixed it!
Re: squaring cropped image nightmare
Posted: 2012-09-28T10:33:19-07:00
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
Re: squaring cropped image nightmare
Posted: 2012-09-28T17:46:42-07:00
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).
Re: squaring cropped image nightmare
Posted: 2012-09-28T18:04:33-07:00
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.
Re: squaring cropped image nightmare
Posted: 2012-09-28T18:57:42-07:00
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.
Re: squaring cropped image nightmare
Posted: 2012-09-28T19:56:26-07:00
by fmw42
Sometimes I am so focused on the code, I miss the important facts. "Can't see the forest for the trees"
Re: squaring cropped image nightmare
Posted: 2012-09-29T05:53:32-07:00
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.
Re: squaring cropped image nightmare
Posted: 2012-09-30T08:00:55-07:00
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.
Re: [SOLVED] squaring cropped image nightmare
Posted: 2012-09-30T10:50:45-07:00
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