How do I prevent interpolation when rotating an image.

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
rmcooke
Posts: 2
Joined: 2011-03-23T03:15:28-07:00
Authentication code: 8675308

How do I prevent interpolation when rotating an image.

Post by rmcooke »

I am something of an Imagemagick noob and have been attempting to resize and rotate images with ImageMagick in the command line using this command:

Code: Select all

convert -resize 230x330 -rotate 15 -transparent white laugh.png laugh_skewed.png

At the same time, I want to make the resulting white background transparent.

The whole thing works pretty well, except I seem to end up with an interpolated border merging the white background produced by the rotation into the original image.

Is there any way to turn off this effect so that only the background and the transposed pixels from the original image are produced?

Here are the before and after images.

Image Image

The effect is difficult to see on a light background, but the site design requires the image to be overlaid on a black background so I am attaching a screenshot clip below.

Image


It occurred to me that first adding a black border to the original image would alleviate the problem, but it did not. The jagged light border still appeared outside the black border.

Many thanks for any help you can give.


Richard
rmcooke
Posts: 2
Joined: 2011-03-23T03:15:28-07:00
Authentication code: 8675308

Re: How do I prevent interpolation when rotating an image.

Post by rmcooke »

Apologies for answering my own post so soon after asking it.

The solution I found was this:

Code: Select all

convert -background black -bordercolor black -border 2x2 -resize 230x330 -rotate 15 -transparent black laugh.png laugh_skewed.png
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How do I prevent interpolation when rotating an image.

Post by anthony »

The actual answer for, what you asked, and not, what you wanted.
(for others who are looking at this topic).

Your solution is not turning off interpolation in rotate....

Rotate is a very old operator in IM, and is based on a method involving shearing the image three times.
A technique known as 'Rotate by Shear' (RBS) and first publish in research papers by Alan Paeth (Paeth Rotate), in Graphics Gems, p. 179, edited by Andrew Glassner, published by Academic Press, 1990.
See Leptonica Rotation for more details.

See IM examples, Rotate which explains the above (with an example of a thin line)
http://www.imagemagick.org/Usage/warping/#rotate

Shears however involve sliding rows and columns of pixels, and this has hardcoded interpolation of the translated pixels.


If you want true no-interpolated rotation. You will need to use Distort with settings that turn off its Area Resampling, and the its Interpolated Lookup (set that to Interger, or NearestNeighbour) You also need to set how you want Virtual Pixels (pixel that are outside the original image) to be interpreted. You cna then rotate using SRT distortion.

Code: Select all

   convert input.png -alpha set \
               -filter point -interpolate NearestNeighbor  -virtual-pixel transparent \
               -distort SRT 30     output.png
See IM Exmaples Image Distortion (in general for effects of 'mapped distortions')
http://www.imagemagick.org/Usage/distorts/
and more specifically SRT (Scale-Rotate-Translate) distortion
http://www.imagemagick.org/Usage/distorts/#srt
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply