Transform portrait aspect to square aspect by adding blurred stripes

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
DanielDD
Posts: 17
Joined: 2013-10-28T01:57:10-07:00
Authentication code: 6789

Transform portrait aspect to square aspect by adding blurred stripes

Post by DanielDD »

Hallo,

I am looking for a simple solution:

The input image has a portrait aspect, i.e. the heigth is larger than the width.

I want to crop stripes from the left and the right side of the input image,
then I want to blur the stripes and montage the stripes left and right to
the input image. The result should be an image of square aspect.

See here for an example:
https://www.instagram.com/p/BK3j9yJBaPl/

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

Re: Transform portrait aspect to square aspect by adding blurred stripes

Post by snibgo »

What have you tried so far? The operation seems fairly simple:

1. Calculate how much extra width you need on the left and right.

2. Crop that much from the left and right of the input image.

3. Blur these how you want.

4. "+append" these three (the extra left, the input, and the extra right) together.

Step (1) will be slightly different according to your script language. But you haven't bothered to say what language you will use. Nor what version of IM you use.
snibgo's IM pages: im.snibgo.com
DanielDD
Posts: 17
Joined: 2013-10-28T01:57:10-07:00
Authentication code: 6789

Re: Transform portrait aspect to square aspect by adding blurred stripes

Post by DanielDD »

ImageMagick 6.9.5-7 Q16 x86_64

I would prefer a tcsh script (cygwin).
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Transform portrait aspect to square aspect by adding blurred stripes

Post by snibgo »

I've never used tcsh. Here is a Windows BAT solution, for IM v6:

Code: Select all

set SRC=c.png

for /F "usebackq" %%L in (`%IM%convert ^
  -ping %SRC% -format "HH=%%H\nWL=%%[fx:int((h-w)/2)]\nWR=%%[fx:h-w-int((h-w)/2)]" ^
  info:`) do set %%L
if "%WL%"=="" exit /B 1

echo HH=%HH% WL=%WL% WR=%WR%

%IM%convert ^
  %SRC% ^
  ( -clone 0 -gravity West -crop %WL%x%HH%+0+0 +repage -blur 0x5 ) ^
  ( -clone 0 -gravity East -crop %WR%x%HH%+0+0 +repage -blur 0x5 ) ^
  -swap 0,1 ^
  +append ^
  out.png
It doesn't have any error checking (eg if the input isn't portrait, or is too thin).
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Transform portrait aspect to square aspect by adding blurred stripes

Post by GeeMack »

DanielDD wrote:I want to crop stripes from the left and the right side of the input image, then I want to blur the stripes and montage the stripes left and right to the input image. The result should be an image of square aspect.
I'm using ImageMagick 6.9.3 on Windows 10 64. Here's another way to approach this running from a Windows command prompt. This gets pretty close to the example...

Code: Select all

convert input.png ^
   ( -clone 0 -rotate 90 ) ^
   ( -clone 1,0 -layers merge ) ^
   ( -clone 2,0 -gravity east -composite -blur 0x20 ) ^
   ( -clone 3,0 -gravity center -composite ) ^
   -delete 0--2 ^
      output.png
It should work with almost any size input image with an aspect ratio wider than about 1:3 and narrower than square. Since it's just a freestanding IM command it should translate pretty easily to a *nix bash or tcsh command line or script by changing the line ends from carets "^" to backslashes "\", escaping the parentheses with backslashes like "\(" and "\)", and maybe other minor tweaks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Transform portrait aspect to square aspect by adding blurred stripes

Post by fmw42 »

Here is another way. Unix syntax.

Input:
Image

Code: Select all

convert monet2.jpg \
	\( -clone 0 -set option:distort:viewport "%hx%h+0+0" \
	-filter point -distort SRT 0 +repage \
	-clone 0 -gravity east -compose over -composite \
	-blur 0x5 \
	-clone 0 -gravity center -compose over -composite \) \
-delete 0 \
monet2_stripes.jpg
Image

Or if you want to unfold (mirror) the sides, you can do

Code: Select all

convert monet2.jpg \
	\( -clone 0 -virtual-pixel mirror \
	-set option:distort:viewport "%hx%h-%[fx:(h-w)/2]+0" \
	-filter point -distort SRT 0 +repage -blur 0x5 \) \
+swap -gravity center -compose over -composite \
monet2_stripes2.jpg
Image


You can also use any of the virtual-pixel methods available such as dither.

Code: Select all

convert monet2.jpg \
	\( -clone 0 -virtual-pixel dither \
	-set option:distort:viewport "%hx%h-%[fx:(h-w)/2]+0" \
	-filter point -distort SRT 0 +repage -blur 0x5 \) \
+swap -gravity center -compose over -composite \
monet2_stripes2.jpg
Image

Or random

Code: Select all

convert monet2.jpg \
	\( -clone 0 -virtual-pixel random \
	-set option:distort:viewport "%hx%h-%[fx:(h-w)/2]+0" \
	-filter point -distort SRT 0 +repage -blur 0x5 \) \
+swap -gravity center -compose over -composite \
monet2_stripes2.jpg
Image


Too bad there is not a virtual-pixel option to copy the sides.


Aside: For a more general bordering effect similar to this, see my script imageborder at the link below.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Transform portrait aspect to square aspect by adding blurred stripes

Post by GeeMack »

Here's another way to get the same general effect with IM6 from a Windows command prompt...

Code: Select all

convert input.png ^
   ( -clone 0 +distort Affine "0,0 0,0   %[w],0 %[h],0   0,%[h] 0,%[h]" -blur 0x20 ) ^
   +swap -gravity center -composite output.png
That just stretches the original image into a square, blurs it, and composites the original over the center. It's actually more like the way it's done in the linked example, but that example background is resized maintaining the aspect ratio, blurred, and cropped to a square.

Edited to add: With ImageMagick 7 the effect in DanielDD's linked example can be virtually duplicated with this command...

Code: Select all

magick input.png -write mpr:input -resize %[h]x -blur 0x20 ^
   -gravity center -extent %[w]x%[w] mpr:input -composite outputIM7.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Transform portrait aspect to square aspect by adding blurred stripes

Post by fmw42 »

DanielDD wrote:
See here for an example:
https://www.instagram.com/p/BK3j9yJBaPl/

DanielDD
FYI:

From what I see in your example, it does not appear to be copying the sides and blurring and adding to make square. It appears to me to have come from a square image that simply has been blurred and the center portion copied over the blurring image. You can see that, since the blurred rails continue along the same slope with no discontinuity in height, which would have occurred if the sides had been copied.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Transform portrait aspect to square aspect by adding blurred stripes

Post by GeeMack »

fmw42 wrote:From what I see in your example, it does not appear to be copying the sides and blurring and adding to make square. It appears to me to have come from a square image that simply has been blurred and the center portion copied over the blurring image.
I tried several things, mostly to find the simplest, more or less cross platform way to achieve the effect. The example DanielDD linked looks to me like his original image was enlarged to make the height of the original into the width of the resized image, then that enlarged copy was blurred, then the original was composited onto the center, then it was cropped to square. This is the command, with IM7 on Windows 10, that matched the example almost perfectly...

Code: Select all

magick input.png -write mpr:input -resize %[h]x -blur 0x20 ^
   -gravity center -extent %[w]x%[w] mpr:input -composite outputIM7.png
And this is the result...

Image

I'm pretty sure that's how it was done.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Transform portrait aspect to square aspect by adding blurred stripes

Post by fmw42 »

That could be correct. I had not considered resizing to square first. Can you upload the original image? I do not recall that you provide the input image before, only the example output.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Transform portrait aspect to square aspect by adding blurred stripes

Post by snibgo »

Trim the OP image, extend it to make "holes", and fill them:

Image
(Definitely not what the OP wanted, but I couldn't resist.)

Code: Select all

%IMDEV%convert ^
  bridge.png ^
  -crop 332x538+109+0 ^
  -background None ^
  -gravity Center ^
  -extent 538x538 ^
  +repage ^
  -process 'fillholespri wr 10 lsr 5c als off a v' ^
  bridge_fhp.jpg
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: Transform portrait aspect to square aspect by adding blurred stripes

Post by fmw42 »

snibgo:

What was the original input? Is the picture you show your reconstructed original?
DanielDD
Posts: 17
Joined: 2013-10-28T01:57:10-07:00
Authentication code: 6789

Re: Transform portrait aspect to square aspect by adding blurred stripes

Post by DanielDD »

Thank you very much.

I decided to use fmw42's unix-syntax script, because it was easy to include it into an
existing tcsh-script. I already used it in instagram:
https://www.instagram.com/dr.phot.graph/

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

Re: Transform portrait aspect to square aspect by adding blurred stripes

Post by snibgo »

@Fred: in my fillholespri script, bridge.png is a crop from a screenshot of the link in the OP. I couldn't find a download button. Here is that crop, but as a jpg:
Image
For my fillholespri, I trim off the blurred sides, and a bit more so I don't have a vertical line at the left edge of the picture.

The picture I show is the output from fillholespri.
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: Transform portrait aspect to square aspect by adding blurred stripes

Post by fmw42 »

Understood. Thanks. Pretty neat recovery of the image. Wish we could get your fillholes (inpainting) code into IM!
Post Reply