Using -distort vs -rotate with a pattern:

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?".
Locked
User avatar
GeeMack
Posts: 757
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Using -distort vs -rotate with a pattern:

Post by GeeMack »

Using IM 7.0.4 on Windows 10 64: I'd like to use one of the IM patterns described at THIS link as a composite mask. I want to fill a rectangle with a tiled "pattern:gray50" by using a "-virtual-pixel tile" setting, and doing a no-op "-distort SRT 0". Unfortunately that changes a perfectly black and white pattern into a dark and light gray pattern. This command creates a pure black and pure white checkerboard of 4x4 squares...

Code: Select all

magick -size 50x50 pattern:gray50 -rotate 0 -scale 400% test_rotate.png
This is what I get by running that command...

Image

The next command, on the other hand, makes a checkerboard of #3E3E3E and #C0C0C0 (approximately 25% and 75%)...

Code: Select all

magick -size 50x50 pattern:gray50 -virtual-pixel tile -distort SRT 0 -scale 400% test_distort.png
This is the result of running that command...

Image

I know there are several other ways to tile that pattern onto an image but... I want my "distort" operation to handle some other images in a stack in certain ways while only manipulating that "pattern:gray50" according to the "virtual-pixel" and "viewport" settings.

I guess the real question is, is this maybe a bug, or is it expected behavior and I just need to avoid "distort"ing that pattern if I want it to stay pure black and white?

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Using -distort vs -rotate with a pattern:

Post by snibgo »

For this case, "-filter point" or "-filter box" before the distort does what you want.
snibgo's IM pages: im.snibgo.com

User avatar
GeeMack
Posts: 757
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Using -distort vs -rotate with a pattern:

Post by GeeMack »

snibgo wrote:For this case, "-filter point" or "-filter box" before the distort does what you want.
Thanks much. I was overlooking the "-filter". Below is a working piece of the code I'm working on. It generates a random plaid pattern, symmetrical and tilable. The "-distort SRT %[fx:t==0?90:0]" operation applies a 90° rotation to just the first image in the stack, and a no-op distort to everything else.

Code: Select all

magick ^
   -size 12x2 ^
   xc: ^
   +noise random ^
   -colors %[fx:rand()*4+6] ^
   -crop %[fx:rand()*8+8]x1+0+0 ^
   -crop %[w]x1@ ^
   +repage ^
   -scale %[fx:rand()*9+1]x2! ^
   +append ^
   -virtual-pixel mirror ^
   -set option:distort:viewport %[fx:w*2]x%[fx:w*2] ^
   -distort SRT 0 ^
   -duplicate 1 ^
   pattern:gray50 ^
   -filter box ^
   -virtual-pixel tile ^
   -set option:distort:viewport %[w]x%[w] ^
   -distort SRT %[fx:t==0?90:0] ^
   -composite ^
   -scale 300% ^
   -normalize ^
      plaid.png
The finished script will read a small text file to get specific thread counts and colors and feed them to the IM command. I also have a notion about using the working machinery from this in a more ambitious project. That's probably the biggest reason I don't want to isolate parts of the "-distort" in parentheses or build a tiled mask by another method.

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Using -distort vs -rotate with a pattern:

Post by snibgo »

GeeMack wrote:... plaid.png
Cool, pretty!
snibgo's IM pages: im.snibgo.com

User avatar
anthony
Posts: 8884
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Using -distort vs -rotate with a pattern:

Post by anthony »

For the reasons why Distort in a non-rotation 'NO-OP' (which is what you did) produces a change see..
IM Examples, Distort, No-Op Distortions
http://www.imagemagick.org/Usage/distorts/#distort_noop

Now -rotate actually uses distort to do the rotations! But it also will short-circuit on a NO-OP (or use Simple Image Warps for 90 degree rotation cases) so as to preserve the image exactly.

At one point rotate did its rotations based on the use of shears (using a 3 step operation developed by Alan Paeth)
But using 3 shearing steps (making it slow) and the limitations of shear (no sub-pixel alignment) produced inconsistent rotations. The rotate-shear algorithm is still present in the ImageMagick API library, but is no longer available from command line.

Hmmm... "convert" lists -rotate incorrectly!
convert
...
-rotate degrees apply Paeth rotation to the image
...

My recommendation is to turn off EWA cylindrical/elliptical filtering for the distort color lookups, (-filter point) and then set an interpolation that you are happy with for the scaling.
See IM Examples, Miscellaneous, Interpolate
http://www.imagemagick.org/Usage/misc/#interpolate

NOTE: for rotations I would normally recommend -interpolate mesh which was designed specifically for rotations of images with sharp boundaries. Unfortunately for a 'pixel hash' that you are using as a test, it will interpret it as a set of diagonal lines, rather than a grid of 'dots'. So I selected 'Catrom' to produce a sharper result, instead of the more normal 'Bilinear' interpolation.

I also recommend you do the scaling as part of the SRT rotation! It saves time, and magnifies just well as most resize operators.

Code: Select all

magick -size 50x50 pattern:gray50 \
          -virtual-pixel white  -filter point -interpolate catrom \
          +distort SRT "4 0"    +repage rotate_mesh_0.png
magick -size 50x50 pattern:gray50 \
          -virtual-pixel white   -filter point -interpolate catrom  \
          +distort SRT "4 17"    +repage rotate_mesh_17.png
Image Image

A 17 degree angle is about the worst possible angle for rotation. All interpolations except Spline will generally produce a perfect NO-OP for 90 degree rotations, The blurring effects in the result is the scaling aspect.

For a 'box' like filter use -interpolate nearest.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

Locked