Page 1 of 1

How to widen transparency?

Posted: 2013-08-15T08:54:25-07:00
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?

Re: How to widen transparency?

Posted: 2013-08-15T09:13:24-07:00
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.

Re: How to widen transparency?

Posted: 2013-08-15T10:09:43-07:00
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.

Re: How to widen transparency?

Posted: 2013-08-15T15:38:02-07:00
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");

Re: How to widen transparency?

Posted: 2013-08-15T16:20:27-07:00
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.)

Re: How to widen transparency?

Posted: 2013-08-16T00:46:26-07:00
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.

Re: How to widen transparency?

Posted: 2013-08-16T00:55:38-07:00
by snibgo
Is the printing area rectangular, as in your original example? Finding those dimensions can be done very quickly.

Re: How to widen transparency?

Posted: 2013-08-16T04:37:12-07:00
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.

Re: How to widen transparency?

Posted: 2013-08-16T05:39:19-07:00
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.