Extend Canvas with unknown color, maintain content size...

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?".
tackd
Posts: 42
Joined: 2013-12-30T07:39:08-07:00
Authentication code: 6789

Re: Extend Canvas with unknown color, maintain content size...

Post by tackd »

OK, will do Fred. Many thanks again.

I noticed some tearing on the upper left and right sides of your last example, is that expected or avoidable ?

Cheers
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Extend Canvas with unknown color, maintain content size...

Post by fmw42 »

What do you mean by tearing? The way IM works with -virtual-pixel edge, is that whatever pixel is on the edge of the image will be extended into the areas that were outside the domain of the input. In your case, the -trim left black on the upper left corner after the trim and so that would be extended to fill out the image to the size you wanted. Your only choices are some estimate of the background color to use with -extent or to somehow remove the black on the edges after the trim. Is that what is bothering you?

You can extend with -virtual-pixel background when you can determine the background color. -virtual-pixel background uses -background somecolor, where you have to define the color beforehand. You could do a histogram on the original image and pick the most frequent (whitish) color in the image to use for the background. But that would require some further coding. You could also turn all near black pixels transparent and then get the average (of the non transparent pixels) ahead of time to use for the background.

Code: Select all

ww=`convert -ping image.png -format "%w" info:`
hh=`convert -ping image.png -format "%h" info:`
color=`convert image.png -fuzz 25% -transparent black -scale 1x1! -alpha off -format "%[pixel:u.p{0,0}]" info:`
angle=`convert image.png -deskew 40% -format "%[deskew:angle]" info:`
convert image.png -virtual-pixel edge -distort SRT "1 -$angle" +repage  \
-fuzz 5% -trim +repage \
-gravity center -background "$color" -extent ${ww}x${hh} \
image_deskew4.png
Image

Is this more like what you wanted?
tackd
Posts: 42
Joined: 2013-12-30T07:39:08-07:00
Authentication code: 6789

Re: Extend Canvas with unknown color, maintain content size...

Post by tackd »

Fred, nothing is bothering, I am beyond words for your professional assistance in providing choices to save lots of time by automating.

It's a beautiful result.

You have helped far beyond my expectations, I'm grateful.

Best regards.

All the best to you.

[Solved]
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Extend Canvas with unknown color, maintain content size...

Post by fmw42 »

If you want to offset, you can add -geometry +X+Y relative to the -gravity setting just before -extent
Post Reply