[Script] Automatic Coloring for Grease Pencil

I wanted a way to quickly color loose lineart without needing to close any lines, so I juryrigged a setup with G’MIC’s existing automatic colorize method (which you may have used in GIMP or Krita). Hopefully, it’ll help someone else out! Directions below the script.
Be sure to install G’MIC for the command line (NOT the plug-in!) from the official site here, or the script won’t work.

Script:


import os
import bpy
import subprocess
scn = bpy.context.scene


#specify the names of the folders you're keeping colors #and lineart in, and where you want final art output to, respectively
colordir = "color"
lineartdir = "lineart"
finaldir = "final"


#specify where those folders are found
#outputs to render directory by default
output_path = scn.render.filepath


#specify the frames you want
render_frames = [0,1,2]


#if you just want frames 0-10 or any other range,
#uncomment and use the below
#render_frames = range(10)


#set to a decimal between 0 and 1
#larger values ignore gaps more
blur = "0.5"


#you should be good to go!


raw = """\
-repeat {$!/2} \
--to_rgba[0] -split_opacity. -+.. 1 -!=. 0 -*[-2,-1] \
--norm[1] -n. 0,1 --histogram. 2,0,1 \
-if {i(0)>i(1)} -*.. -1 -+.. 1 -endif -rm. \
-b. \
""" + blur + """\
% -watershed.. [-1] -rm. -rm[0] -rm[0] \
-done \
"""


rawArr = []
for i in raw.split(" "):
    rawArr.append(i)


def formatPath(f, number, name):
     return output_path + name + "/" + formatNumbers(f, number) + ".png"


def formatNumbers(number, length):
    return '%0*d' % (length, number)


def formatArr(f, color, lineart, arr):
    arr.append("-i")
    arr.append(formatPath(f, 4, color))
    arr.append(formatPath(f, 4, lineart))
    


arr = []


if os.name == 'nt':
    arr.append("gmic.exe")
else:
    arr.append("gmic")


for f in render_frames:
    formatArr(f, colordir, lineartdir, arr)


arr = arr + rawArr    
arr.append("-o")
arr.append(formatPath(f, 3, finaldir))


#print(arr)
subprocess.Popen(arr)

How to Use:

Save your lineart as a transparent .png sequence to its own folder, in 0000.png etc. format. Here’s a gif of my lineart as an example.
http://i67.tinypic.com/293ucjm.gif

Go into Object mode and use a bunch of small cubes to mark regions of color (not Grease Pencil–OpenGL has weird issues with antialiasing that mess colorize up), being sure to work in the Blender Render engine and setting the material to “Shadeless.” Move the cubes accordingly with the lineart in each frame. Don’t forget to add a color for the background, or it won’t work! Here, I chose blue.
http://i67.tinypic.com/35larf9.gif

Then, export this as a sequence to its own folder–again, transparent.
http://i68.tinypic.com/28jgr3b.gif

Now, go into the script and change the appropriate variables to reflect the folders you’re using. Make sure both lineart and color have the same dimensions and duration. Run the script, and voila! Instant coloring!
http://i67.tinypic.com/2agk9k3.gif

When I lay the lineart over, however, I can see it isn’t perfect.
http://i63.tinypic.com/2a8lyyp.jpg

So, I bring the sequence back into Blender as a background image (toward the bottom of the properties panel).

Now, I turn on fill in grease pencil and edit the areas accordingly.
http://i63.tinypic.com/1fgv2t.gif

Composit it all together while replacing the background color with transparency, and that’s it! Feel free to integrate it with other things, and let me know if you have any suggestions :slight_smile:

We are working in a similar tool for grease pencil in 2.8, but integrated in C code.

Oh, good! The performance leaves something to be desired with Python glue. Works in the meantime, I guess!

Oh man I wish I found this sooner. I don’t like how grease pencil fill tool works and I preferred more krita filling tool. Thank god I found this. Maybe you can make it into node compositor , that would be cool.

Again, thank you so much.