Scale down picture, fill rest with border.

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
Sweptr
Posts: 4
Joined: 2013-10-22T21:41:10-07:00
Authentication code: 6789

Scale down picture, fill rest with border.

Post by Sweptr »

I suppose this wil be more a mathematic problem than anythingelse..
I have this Command

Code: Select all

convert   Forvaltningsratten.png -resize 20% -bordercolor  Black -border 200%x200%  20-perc_Förvaltningsratten.png 
Regarding to how much I shrink the original image - the border parameters will be different.
I am have to placed it into a script so I can use variables for the -resize value and the -border value.

Here is my script, which is not working corectly. It gives different output sizes depending to the percentage I have as input parameter

Code: Select all

#!/bin/bash 
ImageSize=$1 #eg 1280*720 
Percent=$2 # eg 30 
Color=$3 

BorderSize=$(bc -l <<< "($ImageSize/($ImageSize*$Percent/100)/2+10)*10") 
 
convert   Forvaltningsratten.png -resize $Percent% -bordercolor  $Color -border \  $BorderSize%x$BorderSize%  $Percent-perc_Forvaltningsratten.png
~
~
~
~
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Scale down picture, fill rest with border.

Post by snibgo »

Your expression is strange. It contains ImageSize twice, and they cancel each other. So the result does not depend on ImageSize.
snibgo's IM pages: im.snibgo.com
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Scale down picture, fill rest with border.

Post by Bonzo »

You can use -extent and that is even easier if all your final images are the same size:

Code: Select all

convert input.jpg -resize 500x500 -background blue -gravity center -extent 600x600 output.jpg
Sweptr
Posts: 4
Joined: 2013-10-22T21:41:10-07:00
Authentication code: 6789

Re: Scale down picture, fill rest with border.

Post by Sweptr »

Bonzo wrote:You can use -extent and that is even easier if all your final images are the same size:

Code: Select all

convert input.jpg -resize 500x500 -background blue -gravity center -extent 600x600 output.jpg
Oh thank you sir.

That was exactly what I needed. :D
Sweptr
Posts: 4
Joined: 2013-10-22T21:41:10-07:00
Authentication code: 6789

Re: Scale down picture, fill rest with border.

Post by Sweptr »

snibgo wrote:Your expression is strange. It contains ImageSize twice, and they cancel each other. So the result does not depend on ImageSize.
At the time writen the code, it was more important to understand what is going on thant make the code pretty.

In this cas eI clearly can see that I
A.. take the size of the whole image,
B. divde it with the size of the whole image multiplied with the value which I use as percent.
C. devide the result with 100 to get the actual percent and then
E. divide the reult with ten.


Why I had to add ten and divide with ten is for now . out of my reach.

I can not gert rid of the imagesize at both ends of the division because the first ImageSize is devided with the result of the second ImageSize and anther divison and multiplication. Try it yourself with some random numbers...

(712/(712*30/100)/2+10)*10)
Is NOT the same as
((30/100)/2+10)*10

Or- did i missunderstand you.?

But I know that my code is wrong, and that's why I come to here in the first place.

I will wait a few days and see If someone can point out the error in my code, then I will mark it as solved anyway - because Bonzo gave me an excellent and better alternative.

Still - I would really love to understand the mathematics behind the scene.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Scale down picture, fill rest with border.

Post by snibgo »

(712/(712*30/100)/2+10)*10)
Is NOT the same as
((30/100)/2+10)*10
Agreed. But it is the same as ((100/30)/2+10)*10.

Your expression can be simplified:

($ImageSize/($ImageSize*$Percent/100)/2+10)*10
= 500 / $Percent + 100

For example:
(712/(712*30/100)/2+10)*10 = 116.6667
500 / 30 + 100 = 116.6667
snibgo's IM pages: im.snibgo.com
Sweptr
Posts: 4
Joined: 2013-10-22T21:41:10-07:00
Authentication code: 6789

Re: Scale down picture, fill rest with border.

Post by Sweptr »

snibgo wrote:
(712/(712*30/100)/2+10)*10)
Is NOT the same as
((30/100)/2+10)*10
Agreed. But it is the same as ((100/30)/2+10)*10.

Your expression can be simplified:

($ImageSize/($ImageSize*$Percent/100)/2+10)*10
= 500 / $Percent + 100

For example:
(712/(712*30/100)/2+10)*10 = 116.6667
500 / 30 + 100 = 116.6667
Yes, but then I donẗ keep track of how I was thinking.
The equation was wrong anyway - It worked for the particular value of 30 percent, but all other values went wrong.

So I still hope to get help finding the correct method to solve this, though not more needed for this purpose - I bet I will get some use of it in the future.

And yes, I am not very bright in mathematics - but I am stubborn as f****ng h**l, so I often solve my problems anyway.
But on this one, I fault.
Post Reply