Exemple #1
0
def main():
	parser = lib.create_options()
	opts, args = parser.parse_args()

	w, h = 24 * 72, 36 * 72

	surface = lib.create_surface(opts.fmt, os.path.basename(__file__), w, h)
	context = cairo.Context(surface)

	context.set_source_rgb(1, 1, 1)
	context.rectangle(0, 0, w, h)
	context.fill()

	pctx = pangocairo.CairoContext(context)
	pctx.set_antialias(cairo.ANTIALIAS_SUBPIXEL)

	layout = pctx.create_layout()
	layout.set_font_description(
		create_font_desc())
	layout.set_text('12')
	context.set_source_rgb(*color_of(0x0099ff))
	pctx.update_layout(layout)
	pctx.show_layout(layout)

	ink_rect, log_rect = layout.get_pixel_extents()
	context.rectangle(*ink_rect)
	context.set_source_rgba(*color_of(0x333333, a=0.5))
	context.stroke()

	context.rectangle(*log_rect)
	context.set_source_rgba(*color_of(0x333333, a=0.5))
	context.stroke()

	lib.commit(surface, os.path.basename(__file__) + '.png')
Exemple #2
0
def create_image_data(w, h):
	surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
	context = cairo.Context(surface)
	

	lib.commit(surface, 'junk.png')

	return surface.get_data()
Exemple #3
0
def main():
	parser = lib.create_options()
	parser.add_option('--size', default=20, dest='size', type='int',
		help='')
	opts, args = parser.parse_args()

	w, h = 24 * 72, 36 * 72


	pad = Padding(w/16.0, w/16.0, w/16.0, w/16.0)

	vw = w - pad.l - pad.r
	vh = h - pad.t - pad.b

	surface = lib.create_surface(opts.fmt, os.path.basename(__file__), w, h)
	context = cairo.Context(surface)

	context.rectangle(0, 0, w, h)
	context.set_source_rgb(*color_of(0xffffff))
	context.fill()

	nx = int(math.ceil(vw / float(opts.size)))
	ny = int(math.ceil(vh / float(opts.size)))

	mask = HeartMask(nx, ny)

	for j in range(ny):
		for i in range(nx):
			r, g, b = rgb_of(mask.pixel_at(i, j))
			if luminance(r, g, b) < 0.5:
				draw_doodle_dah(context,
					pad.l + i*opts.size + opts.size/2.0,
					pad.t + j*opts.size + opts.size/2.0,
					i / 8.0,
					j / 8.0,
					opts.size*0.3)
			else:
				draw_empty_spot(context,
						pad.l + i*opts.size + opts.size/2.0,
						pad.t + j*opts.size + opts.size/2.0,
						i / 8.0,
						j / 8.0,
						opts.size*0.3)

			context.set_source_rgb(*color_of(0x333333))
			context.arc(
				pad.l + i*opts.size + opts.size/2.0,
				pad.t + j*opts.size + opts.size/2.0,
				opts.size * 0.1,
				0,
				2*math.pi)
			context.fill()


	lib.commit(surface, os.path.basename(__file__) + '.png')
Exemple #4
0
def main():
	w, h = 1600, 600

	surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
	context = cairo.Context(surface)

	context.rectangle(0, 0, w, h)
	context.set_source_rgb(1, 1, 1)
	context.fill()

	size = 20 

	nx, ny = w / size, h / size

	cx = w/2.0
	cy = h/2.0

	for y in range(ny + 1):
		for x in range(nx + 1):
			px = x*size + size/2
			py = y*size + size/2

			dx, dy = 0, 0

			d = dist(cx, cy, px, py)
			if d < 200:
				dx = 20 * (px - cx) / d
				dy = 20 * (py - cy) / d

			context.arc(px + dx, py + dy, 0.6 * size/2, 0, 2*math.pi)
			context.set_source_rgb(0, 0.6, 0.9)
			context.fill()

			context.arc(px + dx, py + dy, 0.6 * size/2, 0, 2*math.pi)
			context.set_source_rgb(0.5, 0.5, 0.5)
			context.stroke()

	lib.commit(surface, 'test.png')

	return 0
Exemple #5
0
def main():
	w, h = 1600, 600

	surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
	context = cairo.Context(surface)

	nx = 25

	dx = w / nx

	print 'dx = %f' % dx
	context.rectangle(0, 0, w, h)
	context.set_source_rgb(0.1, 0.1, 0.1)
	context.fill()

	for x in range(nx):
		bx = x*dx + dx/2
		if (x & 1) == 1:
			context.move_to(bx, 0)
			context.line_to(bx, h)
			context.set_source_rgb(1, 1, 1)
			context.stroke()
		else:
			# generate a random number of points
			ny = random.randint(3, 10)
			# ny = int(math.pow(2, random.randint(1, 3)))
			dy = h / ny

			# create a random walk
			walk = random_walk(dx / 4.0)

			pts = [(bx + next(walk), y*dy) for y in range(ny+1)]

			# draw the lines
			context.set_line_width(4)

			lx, ly = pts[0]
			context.move_to(lx, ly)
			for x, y in pts[1:]:
				my = (y - ly) * 0.75
				context.curve_to(
					lx, ly + my,
					x, y - my,
					x, y)
				# context.line_to(x, y)
				lx, ly = x, y
			context.set_source_rgb(1, 1, 1)
			context.stroke()

			# highlight the verticies
			context.set_line_width(4)
			for pt in pts[1:-1]:
				context.arc(pt[0], pt[1], 8, 0, 2 * math.pi)
				context.set_source_rgb(0, 0, 0)
				context.fill()

				context.arc(pt[0], pt[1], 8, 0, 2 * math.pi)
				context.set_source_rgb(1, 1, 1)
				context.stroke()

	lib.commit(surface, os.path.basename(__file__) + '.png')
Exemple #6
0
def main():
	parser = optparse.OptionParser()
	parser.add_option('--seed', default=create_seed(), dest='seed', help='')
	parser.add_option('--fmt', default='pdf', dest='fmt', help='')
	parser.add_option('--nx', default=100, dest='nx', type='int', help='')
	opts, args = parser.parse_args()

	print('seed = %s' % opts.seed)	
	random.seed(opts.seed)

	w, h = 16 * 72, 10 * 72

	surface = lib.create_surface(opts.fmt, os.path.basename(__file__), w, h)
	context = cairo.Context(surface)

	context.rectangle(0, 0, w, h)
	context.set_source_rgb(1, 1, 1)
	context.fill()

	pad = w/16.0
	pa = Pt(pad, pad + (h - 2*pad) * random.random())
	pb = Pt(w - pad, pad + (h - 2*pad) * random.random())

	dx = pb.x - pa.x
	dy = pb.y - pa.y
	n = math.sqrt(dx*dx + dy*dy)
	m = dy / dx

	context.move_to(pa.x, pa.y)
	context.line_to(pb.x, pb.y)
	context.set_source_rgb(*color_of(0xaa, 0xaa, 0xaa))
	context.stroke()

	di = dx / opts.nx
	context.set_line_width(1.0)
	context.set_source_rgb(*color_of(0xee, 0xee, 0xee))
	for i in range(opts.nx + 1):
		context.move_to(pa.x + di*i, pad)
		context.line_to(pa.x + di*i, h - 2*pad)
		context.stroke()

	dj = 0.0
	r = 5

	pts = []
	for i in range(opts.nx + 1):
		x = di*i
		pts.append(Pt(pa.x + x, x*m + pa.y + r*noise.pnoise1(x/10.0, 1)))
		#pts.append(Pt(pa.x + x, x*m + pa.y + dj))
		#dj = max(min(dj + random.random()*r - r/2.0, r), -r)

	context.move_to(pts[0].x, pts[0].y)
	for i in range(1, len(pts)):
		# context.line_to(pts[i].x, pts[i].y)
		a = pts[i - 1]
		b = pts[i]
		context.curve_to(
			a.x + di/2.0, a.y,
			b.x - di/2.0, b.y,
			b.x, b.y)
	context.set_source_rgb(*color_of(0xff, 0x00, 0xff))
	context.stroke()


	lib.commit(surface, os.path.basename(__file__) + '.png')
Exemple #7
0
def main():
	parser = lib.create_options()
	parser.add_option('--size', default=10, dest='size', type='int',
		help='')
	opts, args = parser.parse_args()

	w, h = 24 * 72, 36 * 72

	pad = Padding(w/16.0, w/16.0, w/16.0, w/16.0)

	vw = w - pad.l - pad.r
	vh = h - pad.t - pad.b

	surface = lib.create_surface(opts.fmt, os.path.basename(__file__), w, h)
	context = cairo.Context(surface)

	context.rectangle(0, 0, w, h)
	context.set_source_rgb(*color_of(0xffffff))
	context.fill()

	nx = int(math.ceil(vw / float(opts.size)))
	ny = int(math.ceil(vh / float(opts.size)))

	heart_mask = Mask(nx, ny, draw_heart)
	text_mask = Mask(nx, ny, draw_text)

	for j in range(ny):
		for i in range(nx):
			a = text_mask.pixel_at(i, j)
			if a > 128:
				# context.set_source_rgb(*color_of(0xb3351a))
				# context.set_source_rgb(*color_of(0xef826b))
				context.set_source_rgb(*color_of(0x999999))
				context.arc(
					pad.l + i*opts.size + opts.size/2.0,
					pad.t + j*opts.size + opts.size/2.0,
					opts.size * 0.15,
					0,
					2*math.pi)
				context.fill()
				continue

			a = heart_mask.pixel_at(i, j)
			if a > 128:
				context.set_source_rgb(*color_of(0xef4723))
				context.arc(
					pad.l + i*opts.size + opts.size/2.0,
					pad.t + j*opts.size + opts.size/2.0,
					opts.size * 0.2,
					0,
					2*math.pi)
				context.fill()
				continue

			context.set_source_rgb(*color_of(0x999999))
			context.arc(
				pad.l + i*opts.size + opts.size/2.0,
				pad.t + j*opts.size + opts.size/2.0,
				opts.size * 0.1,
				0,
				2*math.pi)
			context.fill()

	if opts.debug:
		draw_debug_lines(context, pad, vw, vh, 12)

	lib.commit(surface, os.path.basename(__file__) + '.png')
Exemple #8
0
def main():
	parser = optparse.OptionParser()
	parser.add_option('--seed', default=create_seed(), dest='seed', help='')
	parser.add_option('--fmt', default='pdf', dest='fmt', help='')
	parser.add_option('--nx', default=10, dest='nx', help='')
	opts, args = parser.parse_args()

	print('seed = %s' % opts.seed)
	random.seed(opts.seed)

	w, h = 24 * 72, 36 * 72

	pad = Pad(h/16, h/16, h/16, h/16)

	surface = lib.create_surface(opts.fmt, os.path.basename(__file__), w, h)
	context = cairo.Context(surface)

	vw, vh = w - pad.l - pad.r, h - pad.t - pad.b

	nx = opts.nx

	dx = vw / float(nx)
	dy = dx / math.sin(math.pi / 3)
	ny = int(math.ceil(vh / dx))

	context.rectangle(0, 0, w, h)
	context.set_source_rgb(*color_of(0x33, 0x33, 0x33))
	context.fill()

	context.rectangle(pad.l, pad.t, w - pad.l - pad.r, h - pad.t - pad.b)
	context.set_source_rgb(*color_of(0xff, 0xff, 0xff))
	context.fill()

	context.rectangle(pad.l, pad.t, w - pad.l - pad.r, h - pad.t - pad.b)
	context.clip()


	idx = {}
	for i in range(0, nx):
		for j in range(0, ny):
			idx[(i, j)] = True

	colors = junk_colors(4, 0.1, 0.8, 0.5)[:10]


	a = float(nx * ny)

	while len(idx)/a > 0.15:
		x = random.randint(-2, nx + 2)
		y = random.randint(-2, ny + 2)
		r = random.randint(2, 3)
		for i in range(x, x + r):
			for j in range(y, y + r):
				if idx.has_key((i, j)):
					del idx[(i, j)]
		hexagon(context, pad.l + x * dx + dx/2.0, pad.t + y*dy + dy/2.0, r*dy - dy/2.0)
		context.set_source_rgba(*random.choice(colors))
		context.fill()

		hexagon(context, pad.l + x * dx + dx/2.0, pad.t + y*dy + dy/2.0, r*dy - dy/2.0)
		context.set_source_rgba(*color_of(0x00, 0x00, 0x00, 0.3))
		context.stroke()

	context.set_line_width(1.0)
	for x in range(1, nx):
		context.move_to(pad.l + x*dx, pad.t)
		context.line_to(pad.l + x*dx, h - pad.b)
		context.set_source_rgba(*color_of(0x99, 0x99, 0x99, 0.2))
		context.stroke()

	for y in range(1, ny):
		context.move_to(pad.l, pad.t + y*dy)
		context.line_to(w - pad.r, pad.t + y*dy)
		context.set_source_rgba(*color_of(0x99, 0x99, 0x99, 0.2))
		context.stroke()

	for i, color in enumerate(colors):
		print '%s %s %0.2f' % (color[:3], color_of(*color[:3]), luminance(*color[:3]))

		context.rectangle(pad.l + 10 + i * 20, pad.t + 20, 20, 20)
		context.set_source_rgb(*color[:3])
		context.fill()

		context.rectangle(pad.l + 10 + i * 20, pad.t + 20, 20, 20)
		context.set_source_rgb(*color_of(0x33, 0x33, 0x33))
		context.stroke()

	lib.commit(surface, os.path.basename(__file__) + '.png')
Exemple #9
0
def main():
    bg_color = color_of(0xEF, 0x47, 0x23)
    fg_color = color_of(0x33, 0x33, 0x33)

    parser = optparse.OptionParser()
    parser.add_option("--seed", default=create_seed(), dest="seed", help="")
    parser.add_option("--fmt", default="pdf", dest="fmt", help="")
    opts, args = parser.parse_args()

    print("seed = %s" % opts.seed)
    random.seed(opts.seed)

    w, h = 24 * 72, 24 * 72

    pad = Pad(h / 6, w / 12, h / 6, w / 12)

    if opts.fmt == "pdf":
        surface = cairo.PDFSurface(os.path.basename(__file__) + ".pdf", w, h)
    elif opts.fmt == "svg":
        surface = cairo.SVGSurface(os.path.basename(__file__) + ".svg", w, h)
    else:
        raise Error("invalid format: %s" % opts.fmt)

    context = cairo.Context(surface)

    nx = 8
    dx = (w - pad.r - pad.l) / float(nx)

    context.rectangle(0, 0, w, h)
    context.set_source_rgb(*bg_color)
    context.fill()

    for x in range(nx + 1):
        context.set_line_width(2)
        context.move_to(pad.l + x * dx, 0)
        context.line_to(pad.l + x * dx, h)
        # context.set_source_rgb(*fg_color)
        context.set_source_rgba(0x33 / 255.0, 0x33 / 255.0, 0x33 / 255.0, 0.6)
        context.stroke()

    for x in range(nx):
        bx = x * dx + dx / 2
        # generate a random number of points
        ny = random.randint(4, 10)

        dy = (h - pad.t - pad.b) / ny

        # create a random walk
        walk = random_walk(dx / 4.0)

        pts = [(bx + next(walk) + pad.l, y * dy + pad.t) for y in range(ny + 1)]

        # draw the lines
        context.set_line_width(10)

        lx, ly = pts[0]
        context.move_to(lx, ly)
        for x, y in pts[1:]:
            my = (y - ly) * 0.75
            context.curve_to(lx, ly + my, x, y - my, x, y)
            lx, ly = x, y
        context.set_source_rgb(*fg_color)
        context.stroke()

        # highlight the verticies
        context.set_line_width(10)
        for pt in pts:
            context.arc(pt[0], pt[1], 24, 0, 2 * math.pi)
            context.set_source_rgb(*bg_color)
            context.fill()

            context.arc(pt[0], pt[1], 24, 0, 2 * math.pi)
            context.set_source_rgb(*fg_color)
            context.stroke()

            context.arc(pt[0], pt[1], 8, 0, 2 * math.pi)
            context.set_source_rgb(*fg_color)
            context.fill()

    lib.commit(surface, os.path.basename(__file__) + ".png")
Exemple #10
0
def main():
	parser = optparse.OptionParser()
	parser.add_option('--seed', default=create_seed(), dest='seed', help='')
	parser.add_option('--fmt', default='pdf', dest='fmt', help='')
	parser.add_option('--ny', default=250, dest='ny', type='int', help='')
	parser.add_option('--debug', default=False, action='store_true', help='')
	opts, args = parser.parse_args()

	print('seed = %s' % opts.seed)
	random.seed(opts.seed)

	debug_color = color_of(0x00, 0x99, 0xff)

	w, h = 24 * 72, 36 * 72

	pad = Pad(h/32, h/32, h/32, h/32)

	surface = lib.create_surface(opts.fmt, os.path.basename(__file__), w, h)
	context = cairo.Context(surface)

	vw, vh = w - pad.l - pad.r, h - pad.t - pad.b

	context.rectangle(0, 0, w, h)
	context.set_source_rgb(1.0, 1.0, 1.0)
	context.fill()

	bg_sketcher = Sketcher(5.0, 1.0, 300)
	context.save()
	context.rectangle(pad.l, pad.t, vw, vh)
	context.clip()
	draw_hatches(context,
		pad.l,
		pad.t,
		vw,
		vh,
		opts.ny,
		color_of(0x44, 0x44, 0x44, 1.0),
		bg_sketcher.draw)
	context.restore()

	scale = vw/24.0
	path = lib.parse_svg_path(HEART).scale(scale, scale)

	context.save()
	context.translate(w/2.0, h/2.0)
	path.draw(context)
	context.set_source_rgba(*color_of(0xff, 0xff, 0xff, 1.0))
	context.fill()

	context.set_line_width(10.0)
	path.draw(context)
	context.set_source_rgba(*color_of(0xff, 0xff, 0xff, 1.0))
	context.stroke()
	context.restore()

	context.save()
	context.translate(w/2.0, h/2.0)
	path.draw(context)
	context.clip()

	ht_sketcher = Sketcher(10.0, 1.0, 250)
	draw_hatches(context,
		-scale*10,
		-scale*10,
		scale*20,
		scale*20,
		int(opts.ny * 0.6),
		color_of(0xff, 0x00, 0x00, 1.0),
		ht_sketcher.draw)
	context.restore()

	# context.save()
	# context.set_line_width(8.0)
	# context.rectangle(pad.l, pad.t, vw, vh)
	# context.set_source_rgb(*color_of(0x33, 0x33, 0x33))
	# context.stroke()
	# context.restore()

	if opts.debug:
		context.move_to(w / 2.0, 0)
		context.line_to(w / 2.0, h)
		context.set_source_rgb(*debug_color)
		context.stroke()

		context.move_to(0, h / 2.0)
		context.line_to(w, h / 2.0)
		context.set_source_rgb(*debug_color)
		context.stroke()

	lib.commit(surface, os.path.basename(__file__) + '.png')