convert -crop and shifting canvas viewport

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
Pyx

convert -crop and shifting canvas viewport

Post by Pyx »

Hello !
First off, thank you Anthony for the tremendous amount of dedications you put into ImageMagick. Its really appreciated.

I am currently using ImageMagick 6.2.4.5.dfsg1-0.14 on a stock Debian Linux system.
I am using ImageMagick with bash shell scripts and ffmpeg to create "movies" from still photos.
I have image pans working in both directions. For the past several days I have been working on ZOOMING in and out of still images.

I have been using a bash shell script with a for loop to increment a "zoom" into a still image. I have been using;

convert Starter.ppm -resize $((100+$i))% -crop 700x200+18+400 z00$i.ppm

within the for loop to zoom INTO a still image and it does indeed zoom INTO the image.
The problem seems to be that, as the resize percentage increases, the center point, x value and y value DECREASES. The result is
the movie made from images into frames, moves UPWARDS TO THE LEFT. I want the center point to remain stationary or to move
when I want it to. I have read the examples and documents (including playing with +repage but, the result seems to still be the same.

Please help me correct this problem. It seems obvious, I am missing something within the convert statement.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: convert -crop and shifting canvas viewport

Post by anthony »

the -crop you are using has a fixed location for the top left corner.

If you have a more modern IM and I was complete with my filter handling in distort
I would suggest using a SRT distortion. You can specify the 'center' toward which you are zooming, and even roll you image (or not), or pan (translate) at the same time.

However all my work of late has been with trying to bring the quality of distort scaling up to about the same as that of resize, but it is a lot of learning for me in the process.

However if quality is not vital (and it usally isn't for individual frames of a video) you cal just use -distort in IM 6.3.6 with say -filter point (turn of area resampling)
and -interpolate cubic, for a reasonable result.

If upgrading is not an option, then I would use -crop and the rough handling of
virtual canvas offsets by -resize to do the job. For example if X,Y is the center of the zoom try...

Code: Select all

convert image.png -repage -X-Y\! -resize $((100+$i))% -crop 700x200-350-100 \
            +repage  z00$i.ppm
that should to a crop centered on pixel X,Y in the original image.

However only an integer pixel can be used and resize may jitter the image around a little because of quantization of the image offset. The distort method is more accurate.

Now if I can get the resampling filters figured out you can get resize perfect zooming and panning. (and you won't need the -filter,-interpolate settings suggested) Actually it will probably do well right now as you are magnifying the image, which is purely a interpolation distort :-)

Code: Select all

convert image.png -interpolate cubic -distort SRT "X,Y  $((1+$i/100)) 0" z00$i.ppm
there are options to set the 'viewport' of the distort too. See IM example, last example in 'Viewing Distant Horizons'.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply