Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GIMP+Python-Fuでcairo(pycairo)を使って描画するスクリプト、の雛形
#!/usr/bin/env python
# -*- mode: python; Encoding: utf-8; coding: utf-8 -*-
# Last updated: <2017/04/21 18:35:47 +0900>
u"""
Python-Fu cairo draw example
usage: Filter -> Python-Fu -> Cairo draw example
Author : mieki256
License : CC0 / Public Domain
testing environment :
* GIMP 2.8.20 Portable + Windows10 x64
* GIMP 2.8.16 + Ubuntu Linux 16.04 LTS
"""
from gimpfu import *
import cairo
import struct
import random
def get_rgba_str(src):
"""convert cairo surface data to RGBA."""
rgba_buf = ""
l = len(src)
for i in range(l / 4):
i0 = i * 4
i1 = i0 + 4
bgra = struct.unpack('=L', src[i0: i1])[0]
a = (bgra >> 24) & 0x0ff
r = (bgra >> 16) & 0x0ff
g = (bgra >> 8) & 0x0ff
b = bgra & 0x0ff
rgba = struct.pack('4B', r, g, b, a)
rgba_buf += rgba
return rgba_buf
def draw_by_cairo(surface, w, h):
"""draw by cairo."""
ctx = cairo.Context(surface)
ctx.set_antialias(cairo.ANTIALIAS_NONE)
ctx.set_line_width(1.0)
ctx.set_line_join(cairo.LINE_JOIN_MITER)
ctx.set_line_cap(cairo.LINE_CAP_SQUARE)
l = random.randint(16, 64)
for i in range(l):
r = random.random()
g = random.random()
b = random.random()
ctx.set_source_rgb(r, g, b)
rx = random.randint(0, w / 2)
ry = random.randint(0, h / 2)
rw = random.randint(1, w / 2)
rh = random.randint(1, h / 2)
ctx.rectangle(rx, ry, rw, rh)
ctx.fill_preserve()
ctx.set_source_rgb(0, 0, 0)
ctx.stroke()
return surface
def python_fu_cairo_draw_example_main(img, layer, w, h,
createtype, layername, rnd_on, seed):
"""main func."""
w = int(w)
h = int(h)
if rnd_on:
random.seed()
else:
random.seed(int(seed))
if createtype == "Image":
img = gimp.Image(w, h, RGB)
gimp.Display(img)
layer = gimp.Layer(img, layername, w, h, RGBA_IMAGE, 100, NORMAL_MODE)
layer.fill(TRANSPARENT_FILL)
img.add_layer(layer, 0)
# draw cairo
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
draw_by_cairo(surface, w, h)
# transfer gimp layer
src = surface.get_data()
dst = get_rgba_str(src)
rgn = layer.get_pixel_rgn(0, 0, w, h, True, True)
rgn[0:w, 0:h] = str(dst)
layer.flush()
layer.merge_shadow()
layer.update(0, 0, w, h)
pdb.gimp_displays_flush()
# gimp.message("Done.")
register(
"python_fu_cairo_draw_example",
"cairo draw example",
"cairo draw example by Python-Fu",
"Your name",
"Your name",
"2017/04/21",
"Cairo draw example", # menu name
"RGB*", # Alternately use RGB, RGB*, GRAY*, INDEXED etc.
# params
[
# (type, name, description, default [, extra])
(PF_IMAGE, "timg", "Input image", None),
(PF_DRAWABLE, "tdrawable", "Input drawable", None),
(PF_SPINNER, "width", "Width", 512, (8, 8192, 1)),
(PF_SPINNER, "height", "Height", 512, (8, 8192, 1)),
(PF_RADIO, "createtype", "Create", "Image",
(("Image", "Image"), ("Layer", "Layer"))),
(PF_STRING, "layername", "Layer name", 'cairo_draw_layer'),
(PF_TOGGLE, "randomize", "Randomize:", 1),
(PF_INT, "seed", "Random seed", 0)
],
# return vals
[],
python_fu_cairo_draw_example_main, # function name
menu="<Image>/Filters/Languages/Python-Fu"
)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.