Reputation: 842
I'm looking for a way to consolidate my imagemagick command lines and fix the aberrations to give me the desired effect which is a graph on a background from the image.
You can see the image files here - I think embedding them here would make a mess of the layout in stack overflow.
The first image to you can see is the source graph. The 2nd and 3rd images are what I would deem failures because in:
2 The background is cut off as a result of trying to get the background and graph sizes to match properly.
3 where I didn't add the black backdrop you can see the text has no background
The 4th image is the background I used in the examples.
What I'm aiming for is a graph with the background scaled to fit, but not stretched or squashed to fit. The background file will always have larger dimensions than the graph.
Below is the script I knocked up to get the examples with some notes to explain what doing.
Essentially what I'm after is scale the background image until it fills the graph size, crop any excess.
Anyone help?
#!/bin/bash
if [ -z "$3" ]
then
echo "usage: $0 background.png foreground.png output.png"
exit 1
fi
orig_size=`identify -format '%wx%h' "$2"`
bg_size=`identify -format '%wx%h' "$1"`
# make a black background size of graph
convert -size $orig_size xc:black ./thisblack.png
# resize background image to size of graph
# this might result in areas with no background
convert -resize $orig_size "$1" "_$1"
# make the graph the background to force size
# by merging the graph and resized background.
# By using the graph as first parameter the size
# is always correct (even though you can't see
# the graph in this image)
convert -composite "$2" "_$1" -depth 8 "_$3"
# overlay graph onto the composite background and graph
# so we can see the graph again
convert -size $orig_size -composite "_$3" "$2" -depth 8 "__$3"
# merge the black and final graph for end image and fill
# areas with no background with black.
convert -composite "thisblack.png" "__$3" -depth 8 "$3"
# Clean up
rm -f "__$3"
rm -f "_$3"
rm -f "_$1"
rm -f thisblack.png
Upvotes: 0
Views: 155
Reputation: 3782
If I get it right, you want to fill an original image with background by smallest dimention and cut the extending areas. You can achieve this by using -resize
with ^
option and -extent
:
convert background.jpg -resize 100x100^ -extent 100x100 background_resize.jpg
http://www.imagemagick.org/Usage/resize/#fill
Upvotes: 1