Thanks to visit codestin.com
Credit goes to jugad2.blogspot.com

Showing posts with label imaging. Show all posts
Showing posts with label imaging. Show all posts

Tuesday, March 11, 2014

SVGFig, a pure Python SVG library

By Vasudev Ram


SVGFig is a pure Python library to generate and manipulate SVG images.

I came across it recently and tried it out a little.

Here's an example program I wrote, that uses SVGFig to generate a series of overlapping squares as an SVG image:

# test_svgfig.py
from svgfig import *

svgs = []
colors = [ "red", "green", "blue" ]

for x in range(10, 400, 50):
    y = x
    for j in range(3):
        svg = SVG("rect", x=x + 20 * j, y=y + 20 * j, \
            width=100, height=100, fill=colors[j])
        svgs.append(svg)

g = SVG("g", *svgs, fill_opacity="50%")
print g.xml()

I ran it with the command: python test_svgfig.py

That gave the output shown in the screenshot below (click image to enlarge):



- Vasudev Ram - Dancing Bison Enterprises

Contact Page

Saturday, January 11, 2014

pngcanvas, a pure Python PNG library

By Vasudev Ram

I saw pngcanvas recently on Github.

pngcanvas is a minimalist pure Python library by Rui Carmo that lets you create PNG images from programs.

I only did a quick trial of it by modifying the test_pngcanvas.py program that comes with it - mainly stripping it down to the minimum needed to create a PNG file (since the program is more about testing the library than being an introductory example).

Here is my modified version, try_pngcanvas.py - some of the imports may not be needed:
#!/usr/bin/env python

from __future__ import (
    absolute_import, division, print_function, unicode_literals
)

import io
import logging

from pngcanvas import *

BUFSIZE = 8*1024  # Taken from filecmp module
HEIGHT = WIDTH = 512

logging.debug("Creating canvas: %d, %d", WIDTH, HEIGHT)
c = PNGCanvas(WIDTH, HEIGHT, color=(0xff, 0, 0, 0xff))
c.rectangle(0, 0, WIDTH - 1, HEIGHT - 1)

logging.debug("Generating gradient...")
c.vertical_gradient(1, 1, WIDTH - 2, HEIGHT - 2,
    (0xff, 0, 0, 0xff), (0x20, 0, 0xff, 0x80))

logging.debug("Drawing some lines...")
c.color = bytearray((0, 0, 0, 0xff))
c.line(0, 0, WIDTH - 1, HEIGHT - 1)
c.line(0, 0, WIDTH / 2, HEIGHT - 1)
c.line(0, 0, WIDTH - 1, HEIGHT / 2)

with open("try_pngcanvas.png", "wb") as png_fil:
    logging.debug("Writing to file...")
    png_fil.write(c.dump())


Here is the PNG image created by the program:


The program worked on the first try, no tweaking needed - a tribute to the author of pngcanvas.

Subjectively, it also seemed to run fast, though the image created is small.

Update: In my initial version, I had deleted the code to create a gradient. That was when the program ran fast. When I added the gradient code back, it ran significantly slower. Like, less than a second, vs. maybe 2 seconds. Not timed, just guessed. (It can be timed, of course, using various methods.) But considering that generating a gradient is a lot more work than just drawing lines, it seems not bad, at least relatively speaking.

I had also blogged some time ago about pypng, another such library:

pypng, pure Python module to encode/decode PNG

Read other Python posts on my blog.

- Vasudev Ram - Dancing Bison Enterprises


O'Reilly 50% Ebook Deal of the Day


Contact Page