Ruby Pixel Art Programming Challenge #1 - Turn 24x24 8-Bit Colored Punks into All-White Punk Sketches with Black Outlines Only

Hello,

   Inspired by Punk Sketches [1] let's start a new ruby pixel art
programming challenge series:

   Turn a regular "plain vanilla" 24x24 8-bit color punk into an
all-white punk sketch with black outlines only.

   You might use the "CryptoPunks LED Light Pixel Art Experiment" [2]
   for some ideas on how to get started.

   For more background on programming pixel art in ruby,
   see the "Programming CryptoPunks & Copypastas Step-by-Step Book / Guide". [3]

   Questions? Comments? Post them here or on the CryptoPunksDev channel [4].

   And of course, show us your punk sketches. Happy pixel pushing &
programming in ruby.

   Cheers. Prost.

[1] https://twitter.com/PunkSketches
[2] cryptopunks/led at master · cryptopunksnotdead/cryptopunks · GitHub
[3] GitHub - cryptopunksnotdead/programming-cryptopunks: Crypto Collectibles Series. Programming CryptoPunks & Copypastas Step-by-Step Book / Guide. Inside Unique Pixel Art on the Blockchain...
[4] CryptoPunksDev

Hello,

  Spoiler alert.

Turn a regular "plain vanilla" 24x24 8-bit color punk into an
all-white punk sketch with black outlines only.

   Yes, you can.

   Find a little reference script and (automagically drawn) punk sketch samples
  at the new Black & White (Pencil Drawing) Sketch Pixel Art
Experiment [1] page.

   The formula used is:

1. Let's use a white background.
2. Let's check if the left neighbouring (x-1) pixel is of a different
color - if yes, draw a black vertical line.
3. Let's check if the top neighbouring (y-1) pixel is of a different
color - if yes, draw a black horizontal line.
4. Repeat for all pixels.

   And in ruby that reads:

module Pixelart

  class Image
    def sketch( sketch=4, line: 1 )
      width  = @img.width*sketch  + (@img.width+1)*line
      height = @img.height*sketch + (@img.height+1)*line

      puts "  #{width}x#{height}"

      img = Image.new( width, height, Color::WHITE )

      @img.width.times do |x|
        @img.height.times do |y|
          pixel = @img[x,y]

          ## get surrounding pixels - if "out-of-bound" use transparent (0)
          left   =  x == 0  ? Color::TRANSPARENT : @img[x-1,y]
          top    =  y == 0  ? Color::TRANSPARENT : @img[x  ,y-1]

          if pixel != left   ## draw vertical line
              (sketch+line*2).times do |n|
                line.times do |m|
                  img[    x*sketch + line*x + m,
                      n + y*sketch + line*y] = Color::BLACK
                end
              end
          end

          if pixel != top   ## draw horizontal line
             (sketch+line*2).times do |n|
               line.times do |m|
                 img[n + x*sketch + line*x,
                         y*sketch + line*y + m] = Color::BLACK
                end
             end
          end
        end
      end

      img
  end
end # class Image
end # module Pixelart

  And with the pixelart gem use it like:

require 'pixelart'

punk = Pixelart::Image.read( 'punk-3100.png' )

punk_sketch = punk.sketch( 4 )
punk_sketch.save( 'punk-3100_sketch4x.png' )

punk_sketch = punk.sketch( 4, line: 4 )
punk_sketch.save( 'punk-3100_sketch4x4.png' )

punk_sketch = punk.sketch( 8, line: 2 )
punk_sketch.save( 'punk-3100_sketch8x2.png' )

punk_sketch = punk.sketch( 12, line: 3 )
punk_sketch.save( 'punk-3100_sketch12x3.png' )

  Happy pixel pushing & programming in ruby. Cheers. Prost.

[1] https://github.com/cryptopunksnotdead/cryptopunks/tree/master/sketches