Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have raster images of line drawings of pictures. I want to enlarge these but prevent the lines from becoming too thick to be used. I have converted these to vectors and then resized the vectors but with this process some of the detail is lost.

I have PerlMagick and I know that it is simple to resize raster images with this.

However I cannot readily see any method which will then allow me to thin the lines (which have become thicker during enlargement).

I have read about the convolution method which I suspect may help. However I have yet to find out detailed information about how this can be employed.

Can any monk versed in PerlMagick give me some ideas? The ideal would be some sample code that I could experiment with.

<P> tags added by GrandFather to improve readability

Replies are listed 'Best First'.
Re: Thining lines in raster images
by FreeBeerReekingMonk (Deacon) on Mar 10, 2016 at 22:43 UTC
    Way back, I did some experiments to resize pixel graphics (black and white), while retaining the 1 pixel line width, but correctly resizing things that had "volume".

    I did it like this:

    • Cut the image in 1 pixel horizontal strips.
    • Go over the line, adding object start-X and end-X pixel color.
    • Resize it by multiplying each start-X and end-X with the factor (2 or 4, 3 looks very ugly).
    • Reconstruct the lines into a new image canvas.

    As a 1 pixel width item will have the same start-X and end-X value, it will retain the 1 pixel width. And things that have different start-X and end-X values, will get streched as if normal...

    edit: Then, you need to rotate the image 90 degrees, and do it again to strech it in the Y axis.

    However, what you want is the inset function in inkscape, not a perl program, as your source is a bad quality jpg, instead of a pixel perfect png.... (so dithered pixels get in the way of the above mentioned algorithm... it gets confused)

Re: Thining lines in raster images
by BrowserUk (Patriarch) on Mar 10, 2016 at 22:51 UTC

    Can you make an example image available somewhere?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I guess this would make a 1 pixel red border around a blue rectangle.

      use Graphics::Magick; $image = Graphics::Magick->new; $image->Set(size=>'10x10'); $image->ReadImage('xc:white'); $image->Draw(stroke=>'red', primitive=>'rectangle', points=>'3,3 7,7') +; $image->ColorFloodfill(geometry=>geometry, x=>4, y =>4 , fill=>'blue', + bordercolor=>'red'); #$image->Set('pixel[1,3]'=>'green'); $image->Write('block_normal.png');

        (Just) Horizontal and vertical lines are pretty easy to deal with.

        On the other hand, if the OPs images contain diagonals, it gets much harder. Unless they are all the same angle, or just a few angles.

        Of course, his images might contain curves, which gets harder still.

        And if they contain a mix of all three ...

        And, then there is black and white; or grey scale or 8-bit dithered; or 24-bit fullcolor; or 48-bit; plus or minus alpha channels ....

        Attempting a totally generic solution is a waste of effort if his images only contain horizontal and vertical lines.

        Posting a solution for horizontal and vertical lines is a waste of time, if all his lines are curves.

        Knowing what you're up against is the key to an efficient, effective algorithm; and a picture paints a thousand words.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Thining lines in raster images
by WaywardCode (Sexton) on Mar 12, 2016 at 06:45 UTC
    I think if you want to stay in raster-land, Morphology is what you need. See especially the "erode" and "thinning" operators. Note that, if your lines are black on white, you want to dilate and thicken...because these operate on the white pixels. http://www.imagemagick.org/Usage/morphology/
Re: Thining lines in raster images (Updated to show full resolution.)(Tweaked algorithm)
by BrowserUk (Patriarch) on Mar 12, 2016 at 12:26 UTC

    Seems that tinypic enforces a maximum size of ~1600 (hence why two of your images appeared the same size), so I've switched the links below to imgbin.

    How do this & this compare to your current solution?

    Tweaked algorithm defaults: Sample 1: doubled and thinned Sample 2:doubled & thinned


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Thining lines in raster images
by FreeBeerReekingMonk (Deacon) on Mar 11, 2016 at 19:30 UTC
    Convert your image to scalable vector graphics (either through free online, or inkscape itself), then watch this (The inset command in Inkscape): Inkscape Inset, Outset,...

    Like in the example, you will need to take it slow, and not select the whole picture at once, but work each element separately for better effect, although selecting all and insetting 3 times gave the left tree a nice thin line, and the branches looked quite good.

    If you are not satisfied with the conversion, use GIMP or another program to depekle (remove the jpeg artifacts) before converting. Also, convert to, say, 8 grayscale colors before you do that, it will save you time. Remember: For a good quality, you need to spend time. Heh... graphics answer on a Perl forum... sorry for that.