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

I was wondering if there is a module somewhere that takes an image file and based on the shade of the pixel applies a kind of "sketch render" to the image making it look like a sketch.

If you're not sure what I mean, I am looking for something similar to what the Q3 mod that turned the entire game into a sketch. I was wondering if there is a module for this type of thing, and if not, is there a method I could read about so I could attempt to make my own?

Thanks for the help.

Replies are listed 'Best First'.
Re: Image Sketch Convertion
by merlyn (Sage) on Jun 03, 2001 at 20:17 UTC
Re: Image Sketch Convertion
by da (Friar) on Jun 03, 2001 at 20:21 UTC
    Image::Magick has a 'charcoal' mode that might do what you want. Examples of the various modes are available. Perhaps 'Detect Edges' followed by 'Charcoal' will come close, though you'll certainly have to play with the parameters. You can try imagemagick out on Cristy's server. If none of these work, the source code (in C) is available.

    ___
    -DA

Re: Image Sketch Convertion
by toma (Vicar) on Jun 04, 2001 at 00:25 UTC
    The best way to make the sketch depends on the type of images that you are starting with. If you only have a few colors in your image you just need to decide which colors are black and which are white. This is easy with the cartoonish kinds of pictures.

    Photographs of the real world are much more difficult to convert to sketches. There are several problems to be overcome.

    • You usually don't want to include much detail that is in the background. Say you have a picture of an animal in its environment. The animal will share many colors with the background, and will be difficult for any algorithm to distinguish. The easiest pictures to convert have no detail in the background, such as a bird against a blue sky or a musical instrument against black velvet.
    • Image compression techniques are working against you. JPEG files, in particular, have compression artifacts that are much more noticable after you process the images to make the sketch.
    Most JPEG artifacts can be eliminated somewhat by blurring the images, followed by reducing their size. So it is best to convert large images. The large images take a long time to process, making experimentation tedious. Despeckling is an algorithm available in ImageMagick and it sometimes helps with pictures that look noisy.

    Drawing the lines can be done by color mapping, as you suggested in your question, but this is difficult for photographs where shadows and angles cause surfaces to have many different colors. Skin has an especially rich set of colors.

    A better method is to use edge detection, which looks for sharp transitions from one color to another. Edge detection still has troubles with dark, sharp shadows, such as those caused by a point source of light.

    Once the edges have been detected, a threshhold function converts the various edge intensities to just black and white. Despeckling may also help here.

    Here is a simple sequence of ImageMagick commands that make a line drawing from a photo.

    Blur 90
    Detect Edges 99.9
    Threshhold 128
    Negate

    These are Image Magick commands with parameters, which I use from the menubar of the 'display' command, which is the interactive image manipulator that comes with Image Magick. The same commands should be available through the perl API.

    If you have large photographs, you might try this sequence:

    Blur 75
    View half size
    View half size
    Enhance brightness 20
    Greyscale
    Spiff
    Spiff
    Spiff
    Save as gif
    Despecle

    The spiff command enhances contrast. I use it three or four times in a row. For some unkown reason I seemed to need to save as a gif file to get the despecle command to work. This sequence creates a greyscale sketch.

    Once you have your sketches, save them as a png. I found that png files are much smaller than both jpg and gif for this type of image.

    I don't have perl code for these commands but it shouldn't be too difficult to write. I have used ImageMagick with perl before and didn't have any trouble with it, mostly because of the first tutorial provided by merlyn, which is referenced above.

    If you want really nice, artistic sketches you have to be prepared to do some hand editing of the final result. Look at the sketches by an artist that you like. You will see that the work has a unique style associated with the artist. This style is difficult to capture in a simple algorithm. But not so difficult that I won't stop trying.

    It should work perfectly the first time! - toma

      Some people have hacked together a Quake rendering engine which displays things as blueprints or as hand drawn sketches like what you are talking about.

      So you could play with that engine's source code, or just import what you want to draw into Quake and display it (in 3d of course.. with monsters if you like).

      You can get the source code to the renderer, which would be really neat for someone to interface with Perl, at http://www.cs.wisc.edu/graphics/Gallery/NPRQuake/

        This 3D Quake sketch rendering engine is *very* cool.

        It would be a great project to take the renderer and integrate it with the FreeWRL perl VRML viewer. This would give you the ability to draw virtual worlds with a variety of 3D VRML tools and prebuilt objects and explore them in sketch form.

        It looks to me like this is possible since both codestreams render with OpenGL. FreeWRL uses Mesa library on Linux instead of OpenGL. This shouldn't be a problem, since Mesa typically works in place of OpenGL.

        Of course, working with 3D vector models is very different from working with bitmaps.

        It should work perfectly the first time! - toma

Re: Image Sketch Convertion
by Beatnik (Parson) on Jun 03, 2001 at 20:15 UTC
    AFAIK GIMP is as close as you'll get :)... You could always try a PerlMagick based solution, GD still doesn't do true colors.

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.