Imitation coal drawing

Share your advanced PureBasic knowledge/code with the community.
kvitaliy
Enthusiast
Enthusiast
Posts: 162
Joined: Mon May 10, 2010 4:02 pm

Re: Imitation coal drawing

Post by kvitaliy »

AAT wrote: I have another version of the converted picture :)
Excellent result!
That's just upsetting size of the program as a result.
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: Imitation coal drawing

Post by walbus »

Very nice effect !

With a little tricky fill function you can also automatically remove the artefacts :wink:

Image
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: Imitation coal drawing

Post by Keya »

walbus nice, good to have a non-cross-stitch version hehe :)
what's the "tricky function" though
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: Imitation coal drawing

Post by walbus »

But, i like colors :wink:

Many other presets and changes are so simple available

Try this code with the girl picture

Code: Select all

UseJPEGImageEncoder()
UseJPEGImageDecoder()

Procedure plot_(x, y, gray)
  If Red(gray)<70
    Plot(x, y, RGB(gray, gray, gray))
    EndIf
EndProcedure

Value = 0 ; Or 1
Threshold =270       ;The threshold of sensitivity
Pattern$ = "Image |*.jpg;*.bmp"
Pattern = 0    
File$ = OpenFileRequester("Image JPG,BMP", StandardFile$, Pattern$, Pattern)
If File$
   If LoadImage(0,File$)
            
    Dim Colors(ImageWidth(0),ImageHeight(0))
            
    StartDrawing( ImageOutput(0))
       For x =  0 To ImageWidth(0) -1
           For y = 0 To ImageHeight(0)-1
              Colors(x,y) = Point(x, y)
           Next
       Next
     StopDrawing()
     CopyImage(0, 1)
    EndIf
StartDrawing( ImageOutput(1))
For x = 1 To ImageWidth(0) - 2
  For y = 1 To ImageHeight(0) - 2
       temp = Colors(x,y)
       
          Orig_blue  = Blue(temp)
          Orig_green = Green(temp)
          Orig_red   = Red(temp)
          Diff = 0
      
        For xx = x - 1 To x + 1
            For yy = y - 1 To y + 1
              If (xx <> x) Or (yy <> y)  
                 temp = Colors(xx,yy)
                 Other_blue  = Blue(temp)
                 Other_green = Green(temp)
                 Other_red   = Red(temp)
                 temp = (Orig_red - Other_red) * (Orig_red - Other_red) + (Orig_blue - Other_blue)*(Orig_blue - Other_blue)+(Orig_green - Other_green) * (Orig_green - Other_green)
                 Diff = Diff +Sqr(temp)
              EndIf
            Next yy
        Next xx
 If Value = 0  
    Diff = 255 - Diff
    If Diff < 0:   Diff = 0: EndIf
    If Diff > 255:   Diff = 255:EndIf
    Plot_ (x, y, RGB(Diff, Diff, Diff))

 Else
    If  Diff > Threshold
        Diff = 255 - (Diff - Threshold)
        If Diff < 0 :  Diff = 0:EndIf
        If Diff > 255 :  Diff = 255:EndIf
        Plot_ (x, y, RGB(Diff, Diff, Diff))
    Else
        Plot_ (x, y, RGB(255, 255, 255))
    EndIf
 EndIf
   Next y

 Next x
 
StopDrawing()

  If SaveImage(1,GetTemporaryDirectory() + "charcoal.jpg",#PB_ImagePlugin_JPEG)       
      RunProgram(GetTemporaryDirectory() + "charcoal.jpg")        
  EndIf
 
Else
  MessageRequester("Information", "The requester was canceled.", 0) 
EndIf
Last edited by walbus on Sat Nov 26, 2016 5:55 pm, edited 1 time in total.
kvitaliy
Enthusiast
Enthusiast
Posts: 162
Joined: Mon May 10, 2010 4:02 pm

Re: Imitation coal drawing

Post by kvitaliy »

Another little code.
imitation pastel drawing
Image

Code: Select all

;imitation pastel - pencil drawing
; kvitaliy 2016 г.

UseJPEGImageEncoder()
UseJPEGImageDecoder()

w=3 ; The thickness can be changed (1-5)


Pattern$ = "Image |*.jpg;*.bmp"
Pattern = 0    
File$ = OpenFileRequester("Image JPG,BMP", StandardFile$, Pattern$, Pattern)
If File$
   If LoadImage(0,File$)
conversionTime = ElapsedMilliseconds()      ; Start Timer
    Dim Colors(ImageWidth(0),ImageHeight(0))
            
    StartDrawing( ImageOutput(0))
       For x =  0 To ImageWidth(0) -1
           For y = 0 To ImageHeight(0)-1
              Colors(x,y) = Point(x, y)
           Next
       Next
     StopDrawing()
     CopyImage(0, 1)
    EndIf
StartDrawing( ImageOutput(1))
For x = 1 To ImageWidth(0) - 1
  For y = w To ImageHeight(0) - 1
    temp1 = Colors(x,y-w)
    R1=Red(temp1):G1=Green(temp1):B1=Blue(temp1)
    temp3 = Colors(x,y)
    R3=Red(temp3):G3=Green(temp3):B3=Blue(temp3)
    R = Sqr((R1 - R3)*(R1 - R3)+(R3 - R1)*(R3 - R1)) 
    G = Sqr((G1 - G3)*(G1 - G3)+(G3 - G1)*(G3 - G1)) 
    B = Sqr((B1 - B3)*(B1 - B3)+(B3 - B1)*(B3 - B1)) 
    If R>255:R=255:EndIf
    If G>255:G=255:EndIf
    If B>255:B=255:EndIf
    
    Plot (x , y, RGB(R,G,B) ! $FFFFFF) ;pastel drawing
    ;Plot (x , y, RGB(R,G,B)) ; Or neon glow
    
  Next y

 Next x
 
 StopDrawing()
 
 conversionTime = ElapsedMilliseconds() - conversionTime ; Stop Timer
 
MessageRequester("Conversion time", Str(conversionTime)+" mS.",0)

  If SaveImage(1,GetTemporaryDirectory() + "charcoal.jpg",#PB_ImagePlugin_JPEG)       
      RunProgram(GetTemporaryDirectory() + "charcoal.jpg")        
  EndIf
 
Else
  MessageRequester("Information", "The requester was canceled.", 0) 
EndIf
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: Imitation coal drawing

Post by Keya »

WELL THIS IS A BIT UNCANNY ... doing some unrelated googling i came across the Canny Edge Detector (similar to Sobel operator i guess) - https://en.wikipedia.org/wiki/Canny_edge_detector
Here's its original output:
Image
but if you simply invert it i think it looks a lot like a stencil/line-style (ie pure B&W and sharp lines) drawing out of a childrens book (as opposed to a sketch-style like the charcoals etc with shades/blurs etc). I couldnt draw to save my life but this has given me new hope if this programming gig doesnt work out and i need to find work as a freelance artist lol
Image
anyway, back on topic! lol
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: Imitation coal drawing

Post by BasicallyPure »

I have a better solution for the one pixel border problem.
I have just copied the pixels one row and column in to make the outer border pixels.
It looks better than my first try which was just a white one pixel wide border.
More speed increase I believe from my previous code.
Also, I have improved the user interface for the demo code.
Now it has a threshold adjustment control to try various threshold settings.
I have also added copy and paste clipboard support.

See my previous post for the code.
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
kvitaliy
Enthusiast
Enthusiast
Posts: 162
Joined: Mon May 10, 2010 4:02 pm

Re: Imitation coal drawing

Post by kvitaliy »

BasicallyPure wrote: See my previous post for the code.
Very nice !
Post Reply