syntax in a loop

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
zemlik
Posts: 38
Joined: 2014-03-05T06:12:01-07:00
Authentication code: 6789

syntax in a loop

Post by zemlik »

hello I want to crop an image and make lots of separate images.

Code: Select all

     20 for ((i=1; i<=$f; i++))
     21 do
     26 convert test.jpg -crop 200x200 $i.jpg
     27 done
I get output like this
1-0.jpg 1-7.jpg 2-5.jpg 3-3.jpg 4-1.jpg 4-8.jpg 5-6.jpg
1-1.jpg 1-8.jpg 2-6.jpg 3-4.jpg 4-2.jpg 5-0.jpg 5-7.jpg
1-2.jpg 2-0.jpg 2-7.jpg 3-5.jpg 4-3.jpg 5-1.jpg 5-8.jpg
1-3.jpg 2-1.jpg 2-8.jpg 3-6.jpg 4-4.jpg 5-2.jpg resize
1-4.jpg 2-2.jpg 3-0.jpg 3-7.jpg 4-5.jpg 5-3.jpg test.jpg
1-5.jpg 2-3.jpg 3-1.jpg 3-8.jpg 4-6.jpg 5-4.jpg
1-6.jpg 2-4.jpg 3-2.jpg 4-0.jpg 4-7.jpg 5-5.jpg

what is the correct syntax ?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: syntax in a loop

Post by snibgo »

-crop 200x200
You probably mean to put:
-crop 200x200+0+0
snibgo's IM pages: im.snibgo.com
zemlik
Posts: 38
Joined: 2014-03-05T06:12:01-07:00
Authentication code: 6789

Re: syntax in a loop

Post by zemlik »

Ah! ok, cheers
zemlik
Posts: 38
Joined: 2014-03-05T06:12:01-07:00
Authentication code: 6789

Re: syntax in a loop

Post by zemlik »

Ok, I'm probably not doing this the right way but I made the frames from a single image as a test which is like a zoom when I assemble the frames in moviemaker.
there is a wobble in the output of the .mov
Is this a known issue ?
I cannot see why there would be a wobble.
[edit] Is it possible to assemble pics into a .mov on command line with imagemagick to see where wobble comes from ?

Code: Select all

#/bin/bash
clear

echo "enter start pixel width:"
read x
echo "enter end pixel width:"
read y
echo "enter frames:"
read f

exp=`echo "1/$f" | bc -l`
num=`echo "$y^2/$x^2" | bc -l`
pr=`echo "1-(e($exp * l($num)))" | bc -l`
echo "this is the proportion: $pr"

for ((i=1; i<=$f; i++))
do
nf=`echo "sqrt(($x^2)*((1-$pr)^$i))" | bc -l`
printf -v chop "%.0f" "$nf"
echo "$chop"
b=`echo "($x-$chop)/2" | bc -l`

convert test.png -crop "$chop"x"$chop"+$b+$b $i.jpg
convert $i.jpg -resize 500x500 500-$i.jpg
done
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: syntax in a loop

Post by snibgo »

You can put your MOV somewhere like dropbox.com and paste the URL here.

For zooming etc I use "-distort SRT". This avoids the inevitable quantizing from "-crop", which always works in integers of pixels.

IM can generate MOVs by using ffmpeg as a delegate. But it would need to have all the frames in memory at the same time.
snibgo's IM pages: im.snibgo.com
zemlik
Posts: 38
Joined: 2014-03-05T06:12:01-07:00
Authentication code: 6789

Re: syntax in a loop

Post by zemlik »

I chop the decimals off the pixels in the code ? it seems more than a pixel wobble, will convert accept fractions of pixels ? any tips appreciated. I only understand this stuff for as long as it takes to do something then I seem to forget it.
http://mickiwiki.com/images/ginger.mp4
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: syntax in a loop

Post by snibgo »

I don't do Unix so I can't trace your code. You resize, so pixel quantization might be magnified.
snibgo's IM pages: im.snibgo.com
zemlik
Posts: 38
Joined: 2014-03-05T06:12:01-07:00
Authentication code: 6789

Re: syntax in a loop

Post by zemlik »

Ah I see! I chop it small and then enlarge to original size that might explain it. I think I will do everything at a much bigger size and then reduce everything.

cheers
zemlik
Posts: 38
Joined: 2014-03-05T06:12:01-07:00
Authentication code: 6789

Re: syntax in a loop

Post by zemlik »

would you happen to be able to post the line for
"convert inputfile -distort SRT options output file"
showing the options to zoom in on inputfile by a percentage keeping the same image/canvas size ?
and also options for an offset for the co-ordinates of a zoom.
I am reading but I am a bit doddery.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: syntax in a loop

Post by snibgo »

To just zoom in, use two arguments: scale and angle, with "0" for the angle. Scale=1 will make no change, Scale=2 will double the size but give you a crop from the centre so the number of pixels is unchanged. 0<scale<1 will zoom out. For example:

Code: Select all

convert in.png -distort SRT 1.23456,0 out.png
If you want to zoom in towards a point that isn't the centre, then give the coords of the point as the first two arguments. Eg to zoom towards (10,20):

Code: Select all

convert in.png -distort SRT 10,20,1.23456,0 out.png
This syntax is okay for Windows. For Unix etc maybe you need quotes:

Code: Select all

convert in.png -distort SRT '10,20,1.23456,0' out.png
See http://www.imagemagick.org/script/comma ... hp#distort
snibgo's IM pages: im.snibgo.com
zemlik
Posts: 38
Joined: 2014-03-05T06:12:01-07:00
Authentication code: 6789

Re: syntax in a loop

Post by zemlik »

thanks again
zemlik
Posts: 38
Joined: 2014-03-05T06:12:01-07:00
Authentication code: 6789

Re: syntax in a loop

Post by zemlik »

I'm having a bit of trouble understanding the geometry for the different expressions, the results sometimes don't seem to go where I think they should go.

If I have a loop that zooms to a location for some reason "convert" doesn't go straight to the location but helpfully progressively moves there over the itterations and I can't see why it should do that.

Is "-crop" geometry different to "-distort" geometry ?.
Is there an easy to read guide ? there are so many options with image magick it is difficult to comprehend.

cheers

zem
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: syntax in a loop

Post by fmw42 »

-crop syntax and -distort syntax are quite different. They were designed with different concepts in mind. -crop is just for cropping. -distort is for scaling, rotation and translation, though it crops automatically to the same size output as the input. However you can add a -define to -distort SRT 0 to do viewport cropping.

see
http://www.imagemagick.org/Usage/crop/
http://www.imagemagick.org/Usage/distorts/#srt
http://www.imagemagick.org/Usage/distor ... t_viewport
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: syntax in a loop

Post by snibgo »

The references given by fmw42 are good. Here is a simple explanation of zooming with "-distort SRT".

1. Translates to put the first coord (X,Y) at the origin. (Default: move the centre to the origin.)
2. Scales and rotates about the origin.
3. Translates to put the origin at the second coord (NewX,NewY). (Default: second coord equals first coord.)

So if you want to smoothly zoom in (by varying the scale, increasing from 1.0), you can keep a feature of the source image at a constant location in the resulting image. To do this, give its coordinate as the first (X,Y) and don't give (NewX,NewY).

Example:

Code: Select all

convert logo: -background red -virtual-pixel background -distort SRT 238,110,%SCALE%,0 out.png
where %SCALE% is a number >= 1

Image

Or you might want to smoothly zoom in by putting a feature of the image in the centre of the result. To do this, give its coordinate as the first (X,Y) and give the semi-width and semi-height for (NewX,NewY).

Example:

Code: Select all

convert logo: -background red -virtual-pixel background -distort SRT 238,110,%SCALE%,0,320,240 out.png
Image
snibgo's IM pages: im.snibgo.com
Post Reply