Page 1 of 2

Squaring images.

Posted: 2012-03-23T08:49:29-07:00
by RQuadling
Hello.

I am trying to create square jpg images which come to us from multiple/various sources. We have no control of the sources and I don't have any dimension data.

Currently we can process around 4,000 images a day. Downloading the images takes the majority of the time.

The following command will take their images, remove all whitespace and add a small border. This is working perfectly.

Code: Select all

convert "%%~A" -fuzz 1% -trim -bordercolor white -border 1%x1% -border 4x4 "%%~nA_trim.jpg"
I now want to produce a square image so that any additional padding is filled with a white background.

I can script this (I'm using PHP, and can use getimagesize() on the trimmed image), but is there a mechanism to do this within convert without having multiple calls to convert.

Any suggestions?

Regards,

Richard Quadling.

Re: Squaring images.

Posted: 2012-03-23T10:40:03-07:00
by fmw42
If you know or can compute how big you want the square image to be, you can use -extent. See http://www.imagemagick.org/Usage/crop/#extent

If you have no way of knowing the dimensions of your processed image, then I don't think there is a way without first computing that size and getting the max dimension to use in -extent. However I will defer to the IM developers to correct me.

I expect that this will become possible in IM 7, but that is still quite a ways off, though under development now.

Re: Squaring images.

Posted: 2012-03-23T10:45:43-07:00
by Bonzo
I thought you showed me a way to "ping" an image to get the size into a variable fmw42 which was fast; could this value then be passed to extent?

Re: Squaring images.

Posted: 2012-03-23T11:06:45-07:00
by fmw42
Bonzo wrote:I thought you showed me a way to "ping" an image to get the size into a variable fmw42 which was fast; could this value then be passed to extent?
Yes, but that means you have to create his image as above, then get the size into a variable, and then do another convert to use -extent with size coming from the variable. So it cannot all be done in one convert, which is what I thought he needed to do for speed of processing.

This kind of thing has been an issue that has come up a number of times for IM 6 with regard to geometry arguments and asked even by myself. So I believe that Anthony has this in mind for IM 7. The ideal for IM 6 would have been to be able to use -set to specify the dimensions and then have -extent use those from the -set parameter. But I do not think this is possible with the current scheme of arguments for geometry.

P.S. See solution in following post.

Re: Squaring images.

Posted: 2012-03-23T11:11:18-07:00
by fmw42
Correction:

Anthony has built into the -distort viewport the ability to do fx computations.

So here is a way to do it whether landscape or portrait.

rose: is landscape

convert rose: -bordercolor white -border 1 \
-set option:distort:viewport "%[fx:max(w,h)]x%[fx:max(w,h)]-%[fx:max((h-w)/2,0)]-%[fx:max((w-h)/2,0)]" \
-virtual-pixel edge -distort SRT 0 rose_tmp1.png

90 degree rose: is portrait

convert rose: -rotate -bordercolor white -border 1 \
-set option:distort:viewport "%[fx:max(w,h)]x%[fx:max(w,h)]-%[fx:max((h-w)/2,0)]-%[fx:max((w-h)/2,0)]" \
-virtual-pixel edge -distort SRT 0 rose_tmp.png

Note that -virtual-pixel edge is the default, so it probably can be left out of the above commands.

see
http://www.imagemagick.org/Usage/distor ... t_viewport

Re: Squaring images.

Posted: 2012-03-25T17:20:31-07:00
by anthony
fmw42 wrote:Anthony has built into the -distort viewport the ability to do fx computations.
Add to the above -filter point

This ensures that only fast and exact single point lookups are performed for that for a NO-OP distort (like -distort SRT 0).

NOTE that the 2-dimentional EWA resampling while producing the best posible result we can get for general distortions, does mix a pixels color with its neighbours in the NO-OP case. It is minimal but default, but using -filter point will turn of EWA resampling, switching to faster interpolative sampling. This have been the cause of some intensive study and discussion in the Digital Image Processing forum.

Also note that -interpolate nearest-neighbour interpolation should be fastest lookup, though at that level it should not make a great difference, and I have not don't any speed tests on interpolation, or even against area resampling. That may be a good new topic for the Digital Image Processing forum.


For a special color surrounding the 'distorted image' result, use one of the other -virtual-pixel settings (edge is the default!). See IM examples...
http://www.imagemagick.org/Usage/misc/#virtual
I have added an actual example of doing this in...
http://www.imagemagick.org/Usage/distor ... t_viewport



The use of Percent Escapes to 'geometry' style arguments will be added to IMv7 (in alpha development).It is one of the major new features planned for IMv7 Shell API, and one that should be of great benefit. This means eventually you can do this image bounds resize directly using IMv7 -extent, and avoid the distort resampling issue.

Re: Squaring images.

Posted: 2012-03-25T17:55:39-07:00
by fmw42
Add to the above -filter point
Good point. I was not sure whether you had made a no-op for any exactly -distort SRT 0 case. So I guess not. But it could be useful to have such a trap.

Fred

Re: Squaring images.

Posted: 2012-03-25T19:07:22-07:00
by anthony
fmw42 wrote:
Add to the above -filter point
Good point. I was not sure whether you had made a no-op for any exactly -distort SRT 0 case. So I guess not. But it could be useful to have such a trap.

Fred
No it is not trapped (not like resize no-op is) as it has been in heavy use in EWA discussions.

Re: Squaring images.

Posted: 2012-03-26T07:34:40-07:00
by stupid
I'm not sure this is what you're after but....

convert INPUT.JPG -write mpr:0 /
-gamma -1 /
( mpr:0 -gamma 0 ( +clone -gamma -1 -rotate 90 ) -compose Over -composite -bordercolor BLACK -border 1 -trim +repage mpr:0 -compose Src -gravity East -composite ) /
-compose Copy -gravity Center -composite /
OUTPUT.JPG


Select the section of your image with the -gravity setting after Src.
Reposition your image on the background canvas with the -gravity setting after Copy.

Messy code!


s.

Re: Squaring images.

Posted: 2012-03-26T10:54:29-07:00
by fmw42
stupid wrote:I'm not sure this is what you're after but....

convert INPUT.JPG -write mpr:0 /
-gamma -1 /
( mpr:0 -gamma 0 ( +clone -gamma -1 -rotate 90 ) -compose Over -composite -bordercolor BLACK -border 1 -trim +repage mpr:0 -compose Src -gravity East -composite ) /
-compose Copy -gravity Center -composite /
OUTPUT.JPG


Select the section of your image with the -gravity setting after Src.
Reposition your image on the background canvas with the -gravity setting after Copy.

Messy code!


s.

I think you have posted this to the wrong topic. Did you mean to post here viewtopic.php?f=1&t=20600

Re: Squaring images.

Posted: 2012-03-26T11:10:37-07:00
by stupid
Nope. I might have misunderstood his intention, it's not entirely clear - to me - from the initial post, but this is where I meant my response to go.

s.

Re: Squaring images.

Posted: 2012-03-26T11:14:12-07:00
by fmw42
stupid wrote:Nope. I might have misunderstood his intention, it's not entirely clear - to me - from the initial post, but this is where I meant my response to go.

s.

What he wanted to do was take an image that did not have width=height and pad it out with some color so that it had width=height with the image centered in the padded area, but do it all in one command.

So I am not sure what you are trying to do with your command, but I certainly don't see where -gamma comes in. Are you trying to make each pixel value the square of the original image's pixel value. That is a different meaning of square from what he wanted, though a valid misunderstanding. For that, the simplest way is to use convert image \( +clone \) -compose multiply -composite resultimage. Can you explain the goal of your command or explain what each step is meant to do? Perhaps I misunderstand your command.

Re: Squaring images.

Posted: 2012-03-26T18:29:37-07:00
by anthony
Actually the simplest way to get the square root of pixels is -gamma 2
for squaring use -gamma 0.5 or -evaluate pow 2

Rememnet these operators are applied to normalised values for squaring a value (from 0 to 1) makes it smaller!

See IM Examples, transforms, Evaluate POW
http://www.imagemagick.org/Usage/transf ... aluate_pow

But as Fred stated this is getting off the original topic.

Does anyone else have anything to add in how to "make the area of an image square".
wether it is 'padding' or 'cropping' the image square.

Re: Squaring images.

Posted: 2012-03-26T21:44:16-07:00
by stupid
The -gamma -1 blanks an canvas to white, -gamma 0 blanks a canvas to black. Why not run my code on an image to see if works. It does.

If the misuse of gamma is a worry to you then replace -gamma 0 with -fill BLACK -colorize 100% and -gamma -1 with -fill WHITE -colorize 100%


s.

Re: Squaring images.

Posted: 2012-03-26T22:36:02-07:00
by anthony
The use of -gamma -1 and -gamma 0 is not common and easilly missunderstood.
Other similar methods includes...

for white canvas...

Code: Select all

   -evaluate set 100%
   -threshold -1
   -level -1,-1
   -alpha opaque -alpha extract 
for black canvas

Code: Select all

   -evaluate set 0
   -threshold 100%
   -level 100%,100%
   -alpha transparent -alpha extract
More general method are

Code: Select all

   -fill {color} -draw 'color 0,0 reset'
   -fill {color} -colorize 100%
   +level-colors {color}
   -sparse-color Voronoi '0,0 {color}'
I rather like +level-color myself as it is a vert clean method of clearing a image with any color wanted :-)

IM Examples, Canvas, Overlay a Specific Color
http://www.imagemagick.org/Usage/canvas/#specific