Page 1 of 1
[Resolved] Creating border of equal percent of image hight
Posted: 2017-02-18T13:29:55-07:00
by Videomajk
Hello.
This is my first attemt to use ImageMagick to create a "passe-partout" effect to my photos. I've made a mock-up in PS so you can understand what I'm aiming at:
http://foto.lindberg.pp.se/Darwin-Kakadu_033-PS-ram.jpg
The idea is to duplicate the original image, blur it and scale it to fit the new dimension, Place it under the original image and combine them together. I want the borders to be a percentage of the original image height. Left, right and top border should be 8% of the image height and the bottom border should be 12%.
I'm trying to do this in a Win10 batch-file. My IM version is 6.9.7.
This is what I've been doing so far:
Code: Select all
SET SRC=Darwin-Kakadu_033-web.jpg
convert %SRC% ^
( +clone ^
-bordercolor white ^
-compose Dst_In ^
-border "x8%%" ) ^
( -clone 0 -blur 0x20 ) ^
-background none -compose DstOver -layers merge ^
test.jpg
This is not even close to be finished. Right now I need help with the following:
1. how do I resize image the blured image (Image stack 2) to match the previous image in the stack (stack 1, the one with the border)?
2. How do I get the the border in step one to be 12% of the image hight in the bottom of the image?
Any help would be helpful.
Re: Creating border of equal percent of image hight
Posted: 2017-02-18T14:06:21-07:00
by snibgo
I'm not sure where you are going with the white border. I would do the basic effect like this:
Code: Select all
set SRC=toes.png
%IM%convert ^
%SRC% ^
( +clone ^
-resize 116%%x130%% ^
-blur 0x20 ^
-set page -%%[fx:h*0.08]-%%[fx:w*0.08] ^
) ^
+swap ^
-layers Merge ^
b2.png
The blurred version is 16% wider and 30% taller (more than your 20%, just to make it easier to see). This is placed behind the original, offset by 8% from the left and 8% from the top.
Your mock-up also has a shadow, which is a bit more awkward.
Re: Creating border of equal percent of image hight
Posted: 2017-02-18T14:29:32-07:00
by snibgo
There are many methods for a shadow, such as:
Code: Select all
%IM%convert ^
%SRC% ^
( -clone 0 ^
-resize 116%%x130%% ^
-blur 0x20 ^
-set page -%%[fx:h*0.08]-%%[fx:w*0.08] ^
) ^
+swap ^
( -clone 1 ^
-fill Black -colorize 100 ^
-background White ^
-virtual-pixel Background ^
-blur 8x10000 ^
-level 0%%,50%% ^
-alpha Copy ^
-channel RGB -evaluate Set 0 +channel ^
) ^
-layers Merge ^
b3.png
Re: Creating border of equal percent of image hight
Posted: 2017-02-18T14:37:49-07:00
by Videomajk
Thanks, that gave me a few good tips. The White border was a try to get the right dimensions for the final picture.
However, I see a few problems with this code. First the top image is not centerd horisontaly over the bottom one. Secondly the "Resize 116%x130%" does not allow for different aspect ratios while maintaining a constant whith border. I really need the border whith to be based on the shortest edge only, since I have a lot of panoramas with AR 3x1 or wider.
Re: Creating border of equal percent of image hight
Posted: 2017-02-18T15:03:04-07:00
by snibgo
Oops, sorry, I put:
Code: Select all
-set page -%%[fx:h*0.08]-%%[fx:w*0.08] ^
... but h and w are the wrong way round. It should be:
Code: Select all
-set page -%%[fx:w*0.08]-%%[fx:w*0.08] ^
You can make these as complex as you want. If you have complex rules about width, height and margins it may be easier to calculate the four numbers before the convert command.
Re: Creating border of equal percent of image hight
Posted: 2017-02-19T02:57:53-07:00
by Videomajk
Yes, I Think you are right with calculating the parameters in advance, but I just can't figure out how to put the reuslt of a convert calculation into a Win Batch file variable. Doing somthing like this just doesn't work for me, so I'm probably doing it wrong:
Code: Select all
SET size="convert image.jpg -format "%wx%h" info:"
I've tried bot with and without the "" around the convet command.
Or is there a way to set variables inside the convert command itself?
Anyway, thanks for the help again!
Re: Creating border of equal percent of image hight
Posted: 2017-02-19T07:20:26-07:00
by GeeMack
Videomajk wrote: ↑2017-02-19T02:57:53-07:00Or is there a way to set variables inside the convert command itself?
ImageMagick 6 does have a few built in ways to calculate variables which can be especially useful in "-distort" operations. A Windows BAT script like this can stretch the top and sides by 8% of the height, and the bottom by 12% of the height, blur it, punch a hole in it, put a shadow around the edge of the hole, then lay the original image behind the hole. The result is very much like the example image linked in your opening post, and all done in a single "convert" command.
Code: Select all
@echo off
set SRC=myimage.jpg
convert %SRC% ^
-write mpr:input ^
-gravity center ^
-background none ^
-virtual-pixel none ^
-set option:distort:viewport "%%[fx:w+(h*0.16)]x%%[fx:h+(h*0.20)]" ^
-distort affine "0,0 %%[fx:h*0.08],%%[fx:h*0.08]" ^
-write mpr:windowed ^
-delete 0 ^
mpr:input ^
-virtual-pixel edge ^
-distort affine "0,0 0,0 %%[w],0 %%[fx:w+(h*0.16)],0 0,%%[h] 0,%%[fx:h+(h*0.20)]" ^
-blur 0x16 ^
mpr:windowed ^
-compose dstout ^
-composite ^
-compose over ^
-write mpr:framer ^
-fill black ^
-colorize 100%% ^
-blur 0x4 ^
mpr:framer ^
mpr:windowed ^
-insert 0 ^
-background white ^
-flatten ^
+repage ^
outimage.png
exit /b
EDITED: Added "-background white" before the "-flatten" to eliminate any semi-transparent pixels from the blurring.
The whole thing could probably be done in far fewer steps using IM7.
Re: Creating border of equal percent of image hight
Posted: 2017-02-19T07:24:58-07:00
by snibgo
Videomajk wrote:SET size="convert image.jpg -format "%wx%h" info:"
That's a bash method. In Windows, use a "for" loop, eg:
Code: Select all
for /F "usebackq" %L in (`convert toes.png -format "SIZE=%wx%h" info:`) do set %L
If using a BAT file, double each %.
See my web pages for many Windows examples.
Re: Creating border of equal percent of image hight
Posted: 2017-02-20T12:56:55-07:00
by Videomajk
Thanks for all the help!
GeeMack's script worked exactly as I imagined it should. The script is however quite slow when processing 88 MP images, but that's probably not because of the script
.
Just a few ending questions:
Is there an advantage writing temp images to MPR rather than using the image stack?
Is there some way to scale the border effect in ralation to the size of the Picture?
Re: Creating border of equal percent of image hight
Posted: 2017-02-20T19:00:11-07:00
by GeeMack
Videomajk wrote: ↑2017-02-20T12:56:55-07:00Is there an advantage writing temp images to MPR rather than using the image stack?
I don't know if using memory registers with "mpr:..." is more or less efficient than building a stack with clones. The script I posted above moves pieces in and out of the process with "mpr:..." and "-delete ...". The same thing can be done using clones inside sets of parentheses. It may not be more efficient, but it may be easier to see what the separate operations are doing, and that makes it easier to edit and tweak. Here's a way to do it...
Code: Select all
@echo off
set SRC=myimage.jpg
convert %SRC% ^
-gravity center ^
-background none ^
-set option:distort:viewport "%%[fx:w+(h*0.16)]x%%[fx:h+(h*0.20)]" ^
( ^
-clone 0 ^
-virtual-pixel edge ^
-blur 0x20 ^
-distort affine "0,0 0,0 %%[w],0 %%[fx:w+(h*0.16)],0 0,%%[h] 0,%%[fx:h+(h*0.20)]" ^
) ^
( ^
-clone 0 ^
-virtual-pixel none ^
-distort affine "0,0 %%[fx:h*0.08],%%[fx:h*0.08]" ^
) ^
( ^
-clone 1,2 ^
-compose dstout ^
-composite ^
-compose over ^
) ^
( ^
-clone 3 ^
-fill black ^
-colorize 100%% ^
-blur 0x4 ^
) ^
-delete 0,1 ^
+swap ^
-background white ^
-flatten ^
outimage.png
exit /b
In my experience, one of the slower operations there would be the "-blur ...", so the difference between using "mpr:..." or "( -clone ... )" may not be an issue anyway. I don't know.
Videomajk wrote: ↑2017-02-20T12:56:55-07:00Is there some way to scale the border effect in ralation to the size of the Picture?
The way that script works it's already making the blurred border 8% larger than the original image's height on the left, top, and right, and 12% larger on the bottom. It uses the dimensions of the input image to create the border effect relative to that size. If you need to apply other conditions, minimums, maximums, etc., you can do just about any calculations you can imagine in those "-set ..." and "-distort ..." operations. Find more information about using FX expressions
at this LINK.
Re: Creating border of equal percent of image hight
Posted: 2017-02-20T19:34:29-07:00
by snibgo
In my tests, there is no speed difference between using mpr: or clone. I doubt if there is any memory difference, but I haven't tested this. I expect they use the same underlying code.
The choice of one or the other is essentially personal.
Re: [Resolved] Creating border of equal percent of image hight
Posted: 2017-02-21T01:51:44-07:00
by Videomajk
Thanks, then I know. All the best to both of you.