コード例 #1
0
def TraceImage(path, outPen, threshold=.2, blur=None, invert=False, turd=2, tolerance=0.2, offset=None):
    potrace = getExternalToolPath(os.path.dirname(__file__), "potrace")
    mkbitmap = getExternalToolPath(os.path.dirname(__file__), "mkbitmap")

    if isinstance(path, ImageObject):
        image = path
    else:
        image = ImageObject(path)

    x, y = image.offset()
    w, h = image.size()

    imagePath = tempfile.mktemp(".bmp")
    bitmapPath = tempfile.mktemp(".pgm")
    svgPath = tempfile.mktemp(".svg")

    saveImageAsBitmap(image, imagePath)

    assert mkbitmap is not None
    cmds = [mkbitmap, "-x", "-t", str(threshold)]
    if blur:
        cmds.extend(["-b", str(blur)])
    if invert:
        cmds.extend(["-i"])
    cmds.extend([
        # "-g",
        # "-1",
        "-o",
        bitmapPath,
        imagePath
    ])
    log = executeExternalProcess(cmds)
    if log != ('', ''):
        print(log)

    assert potrace is not None
    cmds = [potrace, "-s"]
    cmds.extend(["-t", str(turd)])
    cmds.extend(["-O", str(tolerance)])
    cmds.extend(["-o", svgPath, bitmapPath])

    log = executeExternalProcess(cmds)
    if log != ('', ''):
        print(log)

    importSVGWithPen(svgPath, outPen, (x, y, w, h), offset)

    os.remove(imagePath)
    os.remove(svgPath)
    os.remove(bitmapPath)
コード例 #2
0
def TraceImage(path, outPen, threshold=.2, blur=None, invert=False, turd=2, tolerance=0.2, offset=None):
    potrace = getExternalToolPath(os.path.dirname(__file__), "potrace")
    mkbitmap = getExternalToolPath(os.path.dirname(__file__), "mkbitmap")

    if isinstance(path, ImageObject):
        image = path
    else:
        image = ImageObject(path)

    x, y = image.offset()
    w, h = image.size()

    imagePath = tempfile.mktemp(".bmp")
    bitmapPath = tempfile.mktemp(".pgm")
    svgPath = tempfile.mktemp(".svg")

    saveImageAsBitmap(image, imagePath)

    assert mkbitmap is not None
    cmds = [mkbitmap, "-x", "-t", str(threshold)]
    if blur:
        cmds.extend(["-b", str(blur)])
    if invert:
        cmds.extend(["-i"])
    cmds.extend([
        # "-g",
        # "-1",
        "-o",
        bitmapPath,
        imagePath
    ])
    log = executeExternalProcess(cmds)
    if log != ('', ''):
        print(log)

    assert potrace is not None
    cmds = [potrace, "-s"]
    cmds.extend(["-t", str(turd)])
    cmds.extend(["-O", str(tolerance)])
    cmds.extend(["-o", svgPath, bitmapPath])

    log = executeExternalProcess(cmds)
    if log != ('', ''):
        print(log)

    importSVGWithPen(svgPath, outPen, (x, y, w, h), offset)

    os.remove(imagePath)
    os.remove(svgPath)
    os.remove(bitmapPath)
コード例 #3
0
def generateGif(sourcePaths, destPath, delays):
    gifsiclePath = getExternalToolPath(os.path.dirname(__file__), "gifsicle")
    assert gifsiclePath is not None
    cmds = [
        # gifsicle path
        gifsiclePath,
        # optimize level
        # "-O3",
        # ignore warnings
        "-w",
        # force to 256 colors
        "--colors",
        "256",
        # make it loop
        "--loop",
    ]
    # add source paths with delay for each frame
    for i, inputPath in enumerate(sourcePaths):
        cmds += [
            # add the frame duration
            "--delay",
            "%i" % delays[i],
            # add the input gif for each frame
            inputPath
        ]

    cmds += [
        # output path
        "--output",
        destPath
    ]
    executeExternalProcess(cmds)
    # remove the temp input gifs
    for inputPath in sourcePaths:
        os.remove(inputPath)
コード例 #4
0
def generateGif(sourcePaths, destPath, delays):
    gifsiclePath = getExternalToolPath(os.path.dirname(__file__), "gifsicle")
    assert gifsiclePath is not None
    cmds = [
        # gifsicle path
        gifsiclePath,
        # optimize level
        # "-O3",
        # ignore warnings
        "-w",
        # force to 256 colors
        "--colors", "256",
        # make it loop
        "--loop",
    ]
    # add source paths with delay for each frame
    for i, inputPath in enumerate(sourcePaths):
        cmds += [
                # add the frame duration
                "--delay", "%i" % delays[i],
                # add the input gif for each frame
                inputPath
            ]

    cmds += [
        # output path
        "--output",
        destPath
    ]
    executeExternalProcess(cmds)
    # remove the temp input gifs
    for inputPath in sourcePaths:
        os.remove(inputPath)
コード例 #5
0
def generateMP4(imageTemplate, mp4path, frameRate, codec="libx264"):
    ffmpegPath = getExternalToolPath(os.path.dirname(__file__), "ffmpeg")
    assert ffmpegPath is not None
    cmds = [
        # ffmpeg path
        ffmpegPath,
        "-y",                   # overwrite existing files
        "-loglevel", "16",      # 'error, 16' Show all errors, including ones which can be recovered from.
        "-r", str(frameRate),   # frame rate
        "-i", imageTemplate,    # input sequence
        "-c:v", codec,          # codec
        "-crf", "20",           # Constant Rate Factor
        "-pix_fmt", "yuv420p",  # pixel format
        mp4path,                # output path
    ]
    executeExternalProcess(cmds)
コード例 #6
0
def _explodeGif(path):
    gifsiclePath = getExternalToolPath(os.path.dirname(__file__), "gifsicle")
    if isinstance(path, AppKit.NSURL):
        path = path.path()
    destRoot = tempfile.mkdtemp()
    cmds = [
        gifsiclePath,
        # explode
        "--explode",
        # source path
        path
    ]
    executeExternalProcess(cmds, cwd=destRoot)
    files = os.listdir(destRoot)
    _explodedGifCache[path] = dict(
        source=destRoot,
        fileNames=files,
    )
コード例 #7
0
def _explodeGif(path):
    gifsiclePath = getExternalToolPath(os.path.dirname(__file__), "gifsicle")
    if isinstance(path, AppKit.NSURL):
        path = path.path()
    destRoot = tempfile.mkdtemp()
    cmds = [
        gifsiclePath,
        # explode
        "--explode",
        # source path
        path
    ]
    executeExternalProcess(cmds, cwd=destRoot)
    files = os.listdir(destRoot)
    _explodedGifCache[path] = dict(
        source=destRoot,
        fileNames=files,
    )
コード例 #8
0
ファイル: mp4Tools.py プロジェクト: meyouwe/drawbot
def generateMP4(imageTemplate, mp4path, frameRate):
    ffmpegPath = getExternalToolPath(os.path.dirname(__file__), "ffmpeg")
    assert ffmpegPath is not None
    cmds = [
        # ffmpeg path
        ffmpegPath,
        "-y",  # overwrite existing files
        "-loglevel",
        "16",  # 'error, 16' Show all errors, including ones which can be recovered from.
        "-r",
        str(frameRate),  # frame rate
        "-i",
        imageTemplate,  # input sequence
        "-c:v",
        "libx264",  # codec
        "-crf",
        "20",  # Constant Rate Factor
        "-pix_fmt",
        "yuv420p",  # pixel format
        mp4path,  # output path
    ]
    executeExternalProcess(cmds)
コード例 #9
0
from drawBot.context.baseContext import BezierPath
from drawBot.context import subscribeContext
from drawBot.misc import getExternalToolPath

from mojo.events import addObserver
from mojo.extensions import getExtensionDefault

from drawBotController import DrawBotController
import glyphContext

# add drawBot to the sys path
sys.path.append(os.path.dirname(__file__))

# set the gifsicle tool as executable
gifsicle = getExternalToolPath(
    os.path.join(os.path.dirname(__file__), "drawBot", "context", "tools"),
    "gifsicle")
potrace = getExternalToolPath(
    os.path.join(os.path.dirname(__file__), "drawBot", "context", "tools"),
    "potrace")
mkbitmap = getExternalToolPath(
    os.path.join(os.path.dirname(__file__), "drawBot", "context", "tools"),
    "mkbitmap")
os.chmod(gifsicle, 0o0755)
os.chmod(potrace, 0o0755)
os.chmod(mkbitmap, 0o0755)


# add a drawGlyph callback
def drawGlyph(glyph):
    if hasattr(glyph, "getRepresentation"):