Page 1 of 1

Keep Aspect Ratio on Resize and Fill with Blur Background

Posted: 2015-07-13T09:14:40-07:00
by visitor x
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

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

Posted: 2015-07-13T09:37:20-07:00
by fmw42
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.

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

Posted: 2015-07-14T01:19:50-07:00
by visitor x
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

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

Posted: 2015-07-14T09:11:45-07:00
by fmw42
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.

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

Posted: 2017-06-28T06:24:48-07:00
by Sweetins
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

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

Posted: 2017-06-28T07:11:31-07:00
by snibgo
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 ).

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

Posted: 2017-06-28T09:41:05-07:00
by Sweetins
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

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

Posted: 2017-06-28T09:57:58-07:00
by fmw42
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

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

Posted: 2017-06-28T10:06:28-07:00
by GeeMack
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

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

Posted: 2017-06-28T10:09:08-07:00
by snibgo
... 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.

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

Posted: 2017-06-28T10:10:53-07:00
by fmw42
@Geemack -- don't you need to double the % in a bat file?

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

Posted: 2017-06-28T10:16:56-07:00
by GeeMack
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. ;)

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

Posted: 2017-06-28T10:21:58-07:00
by fmw42
@Geemack -- thanks for explanation to a purely Unix syntax user.

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

Posted: 2017-07-02T11:27:36-07:00
by Sweetins
Sure thing guys, changing the "convert" command to "magick" did solve the problem. Thanks so much :D