LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-25-2014, 10:15 AM   #1
cantab
Member
 
Registered: Oct 2009
Location: England
Distribution: Kubuntu, Ubuntu, Debian, Proxmox.
Posts: 553

Rep: Reputation: 115Reputation: 115
Imagemagick: Replace image keeping aspect ratio of original.


I have various images that need replacing, however I want the replacement image to be padded with white in order to max the aspect ratio of the original. I've got a lot of them, so want a command that leaves me only to pick the two images and not have to manually read off values.

To give an example, let's say A.png is our original image, and is 800x600. B.png is our replacement image, and is 1000x1200. I want the output to be 1600x1200 - aspect ratio of A.png, dimensions to fully contain B.png - with B.png centred in it and white borders to the left and right.

How to do this? I can fairly easily overlay one image on another with composite, but can't figure out how to get the geometry to work.
 
Old 03-25-2014, 01:35 PM   #2
Philip Lacroix
Member
 
Registered: Jun 2012
Distribution: Slackware
Posts: 441

Rep: Reputation: 574Reputation: 574Reputation: 574Reputation: 574Reputation: 574Reputation: 574
Hi,

I believe there's no need to use composite. In fact I would first extract the needed geometry information (for both images) by using identify:

Code:
identify -format "%w" image.png  ## gives the width
identify -format "%h" image.png  ## gives the height
Then I would use the option -frame with convert, in order to selectively add the needed frame. The option -mattecolor will specify the color of the frame itself:

Code:
convert IN.png -mattecolor white -frame "${width}"x"${height}" OUT.png
Note that width and height parameters of the option -frame define the size of the frame alone, not of the output image. For instance, in order to get your IN.png image from 1000x1200 to 1600x1200 you will need:

Code:
convert IN.png -mattecolor white -frame 300x0 OUT.png
Basically I would use the extracted geometry information to calculate the needed frame, and put everything in a nice script (ImageMagick rocks).

Best regards,
Philip

Last edited by Philip Lacroix; 03-26-2014 at 11:38 AM. Reason: a few improvements (clearness)
 
Old 03-26-2014, 09:03 PM   #3
Philip Lacroix
Member
 
Registered: Jun 2012
Distribution: Slackware
Posts: 441

Rep: Reputation: 574Reputation: 574Reputation: 574Reputation: 574Reputation: 574Reputation: 574
I had some fun trying to figure out a way to do the task: it works and it does also a backup copy of the original file. If a backup file is already there, then the script exits without overwriting anything. You can run it like this:

Code:
# script.sh <old image> <new image>
NOTE - This was done as a scripting exercise, it works but it might also be incomplete or have some errors, so please don't use it without testing it in a safe place first. Besides, do the test using files of which you have at least one working backup copy stored in a safe place. That said, I hope it helps. Of course remarks, ideas and corrections are welcome.

Code:
#!/bin/sh

# (c) 2014 Philip Lacroix

###### Use this script at your own risk! ######

## Frame color.
COLOR=white

## Check if arguments are passed to the script.
if [ -z "${1}" -o -z "${2}" ]; then
  echo "Missing argument. Exiting."; exit 1
fi

## Check if files exist.
if [ ! -e "${1}" -o ! -e "${2}" ]; then
  echo "At least one file does not exist. Exiting."; exit 1
fi

## Exit if a backup copy of the original image already exists.
if [ -e "${1}.bak" ]; then
  echo "Backup of <${1}> exists: will not overwrite. Exiting."
  exit 2
fi

## Get geometry of the original image and calculate aspect ratio.
W1=$(identify -format "%w" "${1}")
H1=$(identify -format "%h" "${1}")
R1=$(echo "scale=3; ${W1}/${H1}" | bc)

## Get geometry of the new image and calculate aspect ratio.
W2=$(identify -format "%w" "${2}")
H2=$(identify -format "%h" "${2}")
R2=$(echo "scale=3; ${W2}/${H2}" | bc)

## See if a frame is needed and along which sides.

if [ $(echo "${R1} == ${R2}" | bc) -eq 1 ]; then

  ## Images have the same aspect ratio. Backup the original image
  ## and replace it with the new one.
  echo "Images <${1}> and <${2}> have the same aspect ratio. Replacing."
  cp "${1}" "${1}.bak"
  cp "${2}" "${1}"

elif [ $(echo "${R1} > ${R2}" | bc) -eq 1 ]; then

  ## Aspect ratio of the original image is greater than the new
  ## one. Backup the original image. Calculate frame size. Add
  ## frame along the new image's height. Replace the former with
  ## the latter.
  echo "Adding frame to <${2}> and replacing <${1}> ..."
  cp "${1}" "${1}.bak"
  FRAME="$(echo "scale=0; (${H2}*${R1}-${W2})/2" | bc)"
  convert "${2}" -mattecolor "${COLOR}" -frame "${FRAME}"x0 "${1}"

else

  ## Aspect ratio of the original image is lesser than the new
  ## one. Backup the original image. Calculate frame size. Add
  ## frame along the new image's width. Replace the former with
  ## the latter.
  echo "Adding frame to <${2}> and replacing <${1}> ..."
  cp "${1}" "${1}.bak"
  FRAME="$(echo "scale=0; (${W2}/${R1}-${H2})/2" | bc)"
  convert "${2}" -mattecolor "${COLOR}" -frame 0x"${FRAME}" "${1}"

fi

exit

Last edited by Philip Lacroix; 03-31-2014 at 12:02 PM. Reason: cleanup
 
Old 03-26-2014, 10:15 PM   #4
cantab
Member
 
Registered: Oct 2009
Location: England
Distribution: Kubuntu, Ubuntu, Debian, Proxmox.
Posts: 553

Original Poster
Rep: Reputation: 115Reputation: 115
Thanks both.

I solved this - then managed to delete the first script I've written. Right before I pointed my online backup tool at the folder I'm working on. *sigh*

Now rewriting that from memory and your example Philip. I'll post it when I'm better able to properly comment it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Set aspect ratio from 4:3 to 16:9? antareslq Linux - Newbie 4 09-26-2010 05:36 AM
How to resize image in CLI with keeping palette and type as original ts4053 Linux - Software 1 01-05-2010 09:35 AM
Aspect ratio problems! genderbender Linux - Laptop and Netbook 1 04-13-2005 11:24 PM
a need simple a program to resize images while keeping the aspect ratio Necronomicom Linux - Software 3 07-10-2004 01:21 AM
16/9 aspect ratio abby_normal Linux - Software 4 11-03-2003 04:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration