Non-Linear thumbnail resize?

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
User avatar
Marsu42
Posts: 75
Joined: 2014-06-12T03:17:45-07:00
Authentication code: 6789
Location: Berlin

Non-Linear thumbnail resize?

Post by Marsu42 »

I'm currently batch-resizing my 2:3 pictures to 1:1 thumbnails, thus cutting off a bit off the sides. What I'd like to do is do a non-linear squeeze by compressing the sides a bit, thus giving a bit more of an impression of the whole image w/o distorting the center.

I see IM has the -distort parameter (http://www.imagemagick.org/script/comma ... hp#distort) but the amount of options is completely beyond my comprehension. Could anyone give me a hint on how to achieve this non-linear compression? I remember AviSynth having something like this so you can watch a bit more of 16:9 videos on a 4:3 screen.

Thanks for any help!
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Non-Linear thumbnail resize?

Post by Bonzo »

What you want is -liquid-rescale although last time I looked the options were very limited. It would be nice to define with a mask ( like Gimp ) which areas can or canot be modified.
User avatar
Marsu42
Posts: 75
Joined: 2014-06-12T03:17:45-07:00
Authentication code: 6789
Location: Berlin

Re: Non-Linear thumbnail resize?

Post by Marsu42 »

Bonzo wrote:What you want is -liquid-rescale although last time I looked the options were very limited. It would be nice to define with a mask ( like Gimp ) which areas can or canot be modified.
Hmmyes, that's about it, but as you've said it's not very configurable. At least some parameter for the curve would be required for me, i.e. being able to specify how much stronger the edges are to compressed vs. the center.

Is some dev reading this or should I re-post a feature request to the developers subforum?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Non-Linear thumbnail resize?

Post by Bonzo »

More options were discussed when -liquid-rescale was first introduced and I am not sure anything happened. I doubt anything much will happen now for a while as the developers are working hard on version 7.

Anthony has some notes on it here http://www.imagemagick.org/Usage/resize/#liquid-rescale with some other resize methods
User avatar
Marsu42
Posts: 75
Joined: 2014-06-12T03:17:45-07:00
Authentication code: 6789
Location: Berlin

Re: Non-Linear thumbnail resize?

Post by Marsu42 »

Bonzo wrote:More options were discussed when -liquid-rescale was first introduced and I am not sure anything happened. I doubt anything much will happen now for a while as the developers are working hard on version 7
Ok, I do hope v7 is progressing, seemed to be a bit suck when I last looked with all changes happening in v6.

In the meantime, I managed to make -liquid-rescale working for me but cropping the thumbnails nearly to 1:1 and then doing the rest in a non-linear way ... thus, the distortion isn't as visible and I get a tiny bit more preview of the 2:3 original.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Non-Linear thumbnail resize?

Post by snibgo »

Suppose the input is 600x400 pixels, so the output is 400x400.

Suppose you want the centre 50% of the 1:1 image undistorted, so that is 200 pixels wide, or 33.3% of the input. The left-hand 100 pixels of the output will be 200 pixels (33.3%) of the input width. Likewise the right-hand side. We can do this in three chunks, with a simple resize of the two sides. Windows BAT script:

Code: Select all

call %PICTBAT%grid 600 400 8 4 1 Black None

%IM%convert ^
  -size 600x400 ^
  -background yellow -fill Blue ^
  -gravity Center ^
  caption:Hello ^
  grid.png -composite ^
  squishIn.png

%IM%convert ^
  squishIn.png ^
  ( -clone 0 -gravity West ^
    -crop 200x0+0+0 +repage ^
    -resize 50%%x100%% ) ^
  ( -clone 0 -gravity Center ^
    -crop 200x0+0+0 +repage ) ^
  ( -clone 0 -gravity East ^
    -crop 200x0+0+0 +repage ^
    -resize 50%%x100%% ) ^
  -delete 0 ^
  +append ^
  squishOut.png
squishIn.png:
Image
squishOut.png
Image

A more flexible method would use displacement maps. This could give a smooth change of scale, so the diagonal lines wouldn't be "broken", but would be curved at the sides.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Non-Linear thumbnail resize?

Post by fmw42 »

I did some test with liquid-rescale a while back, which you can find at http://www.fmwconcepts.com/misc_tests/l ... index.html.

Alternately, you create a displacement map that protects the center area and compresses the left and right sides some. See http://www.imagemagick.org/Usage/mappin ... ement_maps
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Non-Linear thumbnail resize?

Post by Bonzo »

So using a displacement map the command would look something like this?

Code: Select all

  convert image.jpg  displace_map.jpg  -liquid-rescale 75x100%! output.jpg
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Non-Linear thumbnail resize?

Post by snibgo »

I don't use or know much about "-liquid-rescale". Here's an example of a displacement map:

Code: Select all

%IM%convert ^
  -size 1x100 xc:black ^
  -size 1x400 gradient:black-white ^
  -size 1x100 xc:white ^
  -append ^
  +sigmoidal-contrast 5x50%% ^
  ( -size 1x600 gradient:black-white ) ^
  -rotate -90 ^
  -compose Mathematics ^
    -define compose:args=0,-0.5,0.5,0.5 ^
    -composite ^
  -scale "600x400^!" ^
  squishMap.png

%IM%convert ^
  squishIn.png ^
  squishMap.png ^
  -compose Displace -set option:compose:args 600x0 -composite ^
  -gravity Center -crop 400x400+0+0 +repage ^
  squishOut2.png
squishMap.png:
Image

squishOut2.png:
Image
snibgo's IM pages: im.snibgo.com
User avatar
Marsu42
Posts: 75
Joined: 2014-06-12T03:17:45-07:00
Authentication code: 6789
Location: Berlin

Re: Non-Linear thumbnail resize?

Post by Marsu42 »

The displacement idea looks great, but if I apply the above sample in a real world photo it shows strange artifacts (using latest im6)?

EDIT: solved.
Last edited by Marsu42 on 2015-04-12T10:35:38-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Non-Linear thumbnail resize?

Post by snibgo »

My result from your horse is fine. I'm using "Version: ImageMagick 6.9.0-10 Q16 x64". What is your version?

My map is 16 bits/channel/pixel. Yours is only 8, so you have quantization (rounding) errors.
snibgo's IM pages: im.snibgo.com
User avatar
Marsu42
Posts: 75
Joined: 2014-06-12T03:17:45-07:00
Authentication code: 6789
Location: Berlin

Re: Non-Linear thumbnail resize?

Post by Marsu42 »

snibgo wrote:My map is 16 bits/channel/pixel. Yours is only 8, so you have quantization (rounding) errors.
Thanks, that's it - I was indeed using the Q8 version. I know it doesn't make much of a difference, but I don't want to use Q16 for everything because when processing thousands of images on a slow box everything helps.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Non-Linear thumbnail resize?

Post by snibgo »

You only need to make the mask once, for all photos of that size. But the displacement also needs Q16.

When pixels are slightly the wrong colour, it doesn't matter much. But pixels in slightly the wrong position is far more noticeable.
snibgo's IM pages: im.snibgo.com
User avatar
Marsu42
Posts: 75
Joined: 2014-06-12T03:17:45-07:00
Authentication code: 6789
Location: Berlin

Re: Non-Linear thumbnail resize?

Post by Marsu42 »

snibgo wrote:You only need to make the mask once, for all photos of that size. But the displacement also needs Q16.
Yeah, I'm already caching all masks I can when doing bulk operations - but a complicated convert still takes a lot of time. But it's worth it, IM gives me great results (though I am not up to complicated command lines yet) and this displacement method definitely beats every thumbnail generation I've seen yet!
Post Reply