Keep Aspect Ratio on Resize and Fill with Blur Background

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
visitor x
Posts: 16
Joined: 2013-07-27T13:26:38-07:00
Authentication code: 6789

Keep Aspect Ratio on Resize and Fill with Blur Background

Post by visitor x » 2015-07-13T09:14:40-07:00

Hi everyone,

I need to resize a lot images with different sizes to a uniform size, while keeping the aspect ratio of each image. But instead of a single color background, I would like to use a blur version of the original image as background. Here is an example, how it should look like:

Imagehttp://i.imgur.com/wmseTZp.png

How can I implement this with ImageMagick?


Cheers
x

User avatar
fmw42
Posts: 25137
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Keep Aspect Ratio on Resize and Fill with Blur Background

Post by fmw42 » 2015-07-13T09:37:20-07:00

Create your blurred image, then resize the original and composite it over the center of the blurred image.

Unix syntax:

Code: Select all

convert image \( -clone 0 -blur 0xsigma \) \( -clone 0 -resize WxH \) \
-delete 0 -gravity center -compose over -composite result
Please always provide your version of IM and platform when asking questions. Syntax may differ on unix and windows systems.

visitor x
Posts: 16
Joined: 2013-07-27T13:26:38-07:00
Authentication code: 6789

Re: Keep Aspect Ratio on Resize and Fill with Blur Background

Post by visitor x » 2015-07-14T01:19:50-07:00

Thanks! This is what I was looking for:

Code: Select all

convert source.png \( -clone 0 -blur 0x9-resize WxH\! \) \( -clone 0 -resize WxH \) \
-delete 0 -gravity center -compose over -composite result.png

User avatar
fmw42
Posts: 25137
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Keep Aspect Ratio on Resize and Fill with Blur Background

Post by fmw42 » 2015-07-14T09:11:45-07:00

convert source.png \( -clone 0 -blur 0x9-resize WxH\! \) \( -clone 0 -resize WxH \) \
-delete 0 -gravity center -compose over -composite result.png
You must put in real numbers for WxH. Also you need a space between -blur 0x9 and -resize.

Sweetins
Posts: 3
Joined: 2017-06-28T06:10:08-07:00
Authentication code: 1151

Re: Keep Aspect Ratio on Resize and Fill with Blur Background

Post by Sweetins » 2017-06-28T06:24:48-07:00

Hello guys, am new to image magic but this thread describes exactly what I want to do with a bunch of images at once. The challenge is I managed to install image magic (first option from this link "http://www.imagemagick.org/script/binar ... hp#windows") but I don't know how to carry on with the script thing. I created a notepad, copied the script into it and saved it as a .bat file in the folder which contains the images I want to convert, but all I get is errors when I run the .bat file. Please any help on how to get my images resized to 4:3 but keeping aspect ratio of original and using a blurred orginal as background? Am running a windows 8 OS

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

Re: Keep Aspect Ratio on Resize and Fill with Blur Background

Post by snibgo » 2017-06-28T07:11:31-07:00

Please don't multi-post. I've removed your other post.

What is in your BAT file? What error messages do you get?

The commands given above are for bash, not Windows BAT. For BAT, change the end-of-line markers from \ to ^, and remove the back-slashes before parentheses ( and ).
snibgo's IM pages: im.snibgo.com

Sweetins
Posts: 3
Joined: 2017-06-28T06:10:08-07:00
Authentication code: 1151

Re: Keep Aspect Ratio on Resize and Fill with Blur Background

Post by Sweetins » 2017-06-28T09:41:05-07:00

Sorry for the multiple post anyway. Now I did change the code from

convert source.png \( -clone 0 -blur 0x9-resize WxH\! \) \( -clone 0 -resize WxH \) \
-delete 0 -gravity center -compose over -composite result.png

to

convert source.png ^( -clone 0 -blur 0x9-resize WxH\! ) ^( -clone 0 -resize WxH ) ^
-delete 0 -gravity center -compose over -composite result.png

but it didn't work, I suppose I didn't do the changes right. I also tried this code too

if not exist "%~dp1output" md "%~dp1output"
for %%f in ("%~dp1*.jpg" "%~dp1*.jpeg" "%~dp1*.png") do convert "%%f" ( +clone -geometry x1080 ) ( -clone 0 -geometry 1920x -gravity center -crop 1920x1080+0+0 -blur 0x8 ) -delete 0 +swap -gravity center -compose over -composite "output\%%~nf_1080.jpg"

as described on this page (https://www.reddit.com/r/software/comme ... ackground/) but when I ran the .bat file which I created, I got "Invalid parameter" error

If it could help, how about we picture a folder on Desktop (windows 8 64bit) containing pictures. How do I carry the aforementioned process on them? I already have image magic (first option from this link "http://www.imagemagick.org/script/binar ... hp#windows") installed. Your thoughts are most welcomed

User avatar
fmw42
Posts: 25137
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Keep Aspect Ratio on Resize and Fill with Blur Background

Post by fmw42 » 2017-06-28T09:57:58-07:00

Try

Code: Select all

convert source.png ( -clone 0 -blur 0x9 -resize WxH^! ) ( -clone 0 -resize WxH ) -delete 0 -gravity center -compose over -composite result.png 
or perhaps

Code: Select all

convert source.png ( -clone 0 -blur 0x9 -resize "WxH!" ) ( -clone 0 -resize WxH ) -delete 0 -gravity center -compose over -composite result.png 
Where you replace W and H with your desired width and height

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

Re: Keep Aspect Ratio on Resize and Fill with Blur Background

Post by GeeMack » 2017-06-28T10:06:28-07:00

Sweetins wrote:
2017-06-28T09:41:05-07:00
... but when I ran the .bat file which I created, I got "Invalid parameter" error

If it could help, how about we picture a folder on Desktop (windows 8 64bit) containing pictures. How do I carry the aforementioned process on them? I already have image magic (first option from this link "http://www.imagemagick.org/script/binar ... hp#windows") installed. Your thoughts are most welcomed
First, the "Invalid Parameter" error indicates your command is using the Windows "convert" command. If you installed ImageMagick v6 you'll have to call your ImageMagick "convert" using its full path or add its path to your PATH statement in the Windows settings. If you installed ImageMagick v7 you won't use the "convert" command. Instead you'll use "magick".

Once you are accessing the correct "convert" or "magick" command, you'll have to give some values to those W's and H's. I usually use WIDE and HIGH instead of single letters as variable names because it's easier for me to keep track of them. So at a command line or in a BAT script you need to do something like this...

Code: Select all

set WIDE=1920
set HIGH=1080

convert source.png ^( -clone 0 -blur 0x9 -resize %WIDE%x%HIGH%^! ^) ^
   -resize %WIDE%x%HIGH% +swap -gravity center -compose over -composite result.png
Edited to add: Or, as fmw42 mentioned above, if you're always using the same numbers for width and height, just put them directly into your command and don't bother with the SET variables, like this...

Code: Select all

convert source.png ^( -clone 0 -blur 0x9 -resize 1920x1080^! ^) ^
   -resize 1920x1080 +swap -gravity center -compose over -composite result.png
Last edited by GeeMack on 2017-06-28T10:12:31-07:00, edited 1 time in total.

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

Re: Keep Aspect Ratio on Resize and Fill with Blur Background

Post by snibgo » 2017-06-28T10:09:08-07:00

... I got "Invalid parameter" error.
As GeeMack says, that's a big clue. Does "convert" work from the command line? Like this:

Code: Select all

convert rose: r.png
If you get "Invalid parameter", you are running the Microsoft program, not ImageMagick.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 25137
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Keep Aspect Ratio on Resize and Fill with Blur Background

Post by fmw42 » 2017-06-28T10:10:53-07:00

@Geemack -- don't you need to double the % in a bat file?

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

Re: Keep Aspect Ratio on Resize and Fill with Blur Background

Post by GeeMack » 2017-06-28T10:16:56-07:00

fmw42 wrote:
2017-06-28T10:10:53-07:00
@Geemack -- don't you need to double the % in a bat file?
Variables that are set like "set WIDE=1920" can be accessed even in a BAT script with a percent sign on each end like "%WIDE%". Other uses of percent signs like for FX expressions or the single letter variables used inside a FOR loop must be doubled in a BAT script, like "... %%[fx:h/2] ..."

It's Windows. It's not supposed to be understandable. ;)

User avatar
fmw42
Posts: 25137
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Keep Aspect Ratio on Resize and Fill with Blur Background

Post by fmw42 » 2017-06-28T10:21:58-07:00

@Geemack -- thanks for explanation to a purely Unix syntax user.

Sweetins
Posts: 3
Joined: 2017-06-28T06:10:08-07:00
Authentication code: 1151

Re: Keep Aspect Ratio on Resize and Fill with Blur Background

Post by Sweetins » 2017-07-02T11:27:36-07:00

Sure thing guys, changing the "convert" command to "magick" did solve the problem. Thanks so much :D

Post Reply