How to widen transparency?

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
joew
Posts: 42
Joined: 2012-08-23T01:19:56-07:00
Authentication code: 67789

How to widen transparency?

Post by joew »

Hello wizards,

I am looking for a method to "widen" a transparent area. For example, given this picture:

Code: Select all

convert -size 300x300 xc:none -fill none \
  -stroke blue  -strokewidth 10 -draw "roundrectangle 10,10 50,50 5,5" \
  -stroke red   -strokewidth 20 -draw "roundrectangle 110,110 150,150 5,5" \
  -stroke green -strokewidth 15 -draw "roundrectangle 210,210 250,250 5,5" \
  t.png
I would like to widen the transparent area by e.g. 3 pixels, so in effect the drawn lines of the rectangles would become 6 pixels smaller.

Any hints how to achieve that?
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: How to widen transparency?

Post by GreenKoopa »

You want all lines and shapes thinned? Morphology is one option.
http://www.imagemagick.org/Usage/morphology/

The aliasing of your lines may add a step of complexity.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to widen transparency?

Post by snibgo »

Yes, morphology "erode" is the obvious method. It handles anti-aliasing. Windows code:

Code: Select all

%IM%convert -size 300x300 xc:none -fill none ^
  -stroke blue  -strokewidth 10 -draw "roundrectangle 10,10 50,50 5,5" ^
  -stroke red   -strokewidth 20 -draw "roundrectangle 110,110 150,150 5,5" ^
  -stroke green -strokewidth 15 -draw "roundrectangle 210,210 250,250 5,5" ^
  t.png

%IM%convert t.png ^
  -alpha Extract ^
  -write ta.png ^
  -morphology Erode Disk:3.0 ^
  tb.png

%IM%convert t.png tb.png -compose CopyOpacity -composite tz.png
"-write ta.png" isn't needed, but you can then easily see what the "erode" does. And the three commands could be merged into one.
snibgo's IM pages: im.snibgo.com
joew
Posts: 42
Joined: 2012-08-23T01:19:56-07:00
Authentication code: 67789

Re: How to widen transparency?

Post by joew »

Thanx, that was exactly what I was looking for!

There's only one drawback: it is so extremely slow. For a DIN-A4 page in 600 DPI and Disk:120 (that is 5mm), it is running for hours and still not ready :shock:.

BTW: the documentation of the perlmagic API seems to be very outdated. The "method" argument to Morphology() method is not documented at all. Here is how it should be called:

Code: Select all

$sma->Morphology(method=>"Erode", kernel=>"Disk:120");
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to widen transparency?

Post by snibgo »

Scaling your example up x10, so 3000x3000 pixels, took only 2 minutes. Do you have a memory problem?

If you provide your actual image, and explain what you need, you might get better advice. (But I don't do Perl.)
snibgo's IM pages: im.snibgo.com
joew
Posts: 42
Joined: 2012-08-23T01:19:56-07:00
Authentication code: 67789

Re: How to widen transparency?

Post by joew »

snibgo wrote:Scaling your example up x10, so 3000x3000 pixels, took only 2 minutes. Do you have a memory problem?
Don't think so. 4GB. Still running after about 9 hours.

BTW: A4 at 600dpi is 4961x7016. But the real problem is probably the Disk:120.
If you provide your actual image, and explain what you need, you might get better advice. (But I don't do Perl.)
I'd need to ask for permission first.

In a nutshell: this is for printing labels. I have an image which describes the shape of the labels. The actual label is transparent, the rest is (almost) white. I want to print right to the border, and a little over the border, to adjust for printer/label inaccuracy. So the plan is to widen the transparent area by 5mm so the printed picture would be a little bigger than the label.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to widen transparency?

Post by snibgo »

Is the printing area rectangular, as in your original example? Finding those dimensions can be done very quickly.
snibgo's IM pages: im.snibgo.com
joew
Posts: 42
Joined: 2012-08-23T01:19:56-07:00
Authentication code: 67789

Re: How to widen transparency?

Post by joew »

snibgo wrote:Is the printing area rectangular, as in your original example? Finding those dimensions can be done very quickly.
Not really. There are various types of curves. But there is no requirement for high precision in this step.

I think I would get a good result by shifting a copy of the original by 5mm to nw/ne/sw/se and overlay those copies over the original with In or Dstin. This should be quite fast and would be good enough for this particular application.
joew
Posts: 42
Joined: 2012-08-23T01:19:56-07:00
Authentication code: 67789

Re: How to widen transparency?

Post by joew »

The Erode solution was running about 14 hours on a dual core with 4GB.

I've now gone with the other route I described in the previous post:

Code: Select all

my $sm = Image::Magick->new(density => $DPI);
$sm->Read("stanzmuster/stanzmuster.ai");
$sm->Set(colorspace=>"sRGB");
$sm->Transparent(color=>"white");
my $smwide = $sm->Clone();
{
    my @shifts = (&cm(0.4), -&cm(0.4));
    foreach my $xshift (@shifts) {
        foreach my $yshift (@shifts) {
            my $sms = $sm->Clone();
            $e=$sms->Roll("x"=>$xshift, "y"=>$yshift); &e;
            $e=$smwide->Composite(compose=>"In",image=>$sms); &e;
        }
    }
}
(I tried to convert this into the command line syntax, but could not come up with the correct sequence of clone/composite options)

On the same machine, this one is done in 37 seconds. As I stated above, the precision is not very important in this application.
Post Reply