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

Showing posts with label PNG. Show all posts
Showing posts with label PNG. Show all posts

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

Monday, April 22, 2013

pypng, pure Python module to encode/decode PNG


By Vasudev Ram



pypng is a pure Python library to read/write PNG images.

Here are some code examples of using pypng. Note: the actual library is called png.py, not pypng.py. So import that, not this :-)

I tried out a few of the examples, and they worked.

The image above uses the transparency feature of PNG.

- Vasudev Ram - Dancing Bison Enterprises

Monday, December 31, 2012

Cairo, multi-language, cross-platform graphics library, and PyCairo


http://cairographics.org

The cairo graphics library is an interesting toolkit,  used by many well-known products, including GTK+, Mono, Poppler, Mozilla Firefox, WebKit, R, Gnuplot and others.

It supports graphics output to many targets, including the X Window System (Unix and other platforms), Win32, Mac OS X Quartz, PDF, PostScript, PNG and SVG.

Though written in C, it has bindings for many programming languages, including C++ , Factor, Haskell , Lua , Perl , Python , Ruby, Scheme , Smalltalk and others.

Pycairo is the Python binding to cairographics.

http://en.m.wikipedia.org/wiki/Cairo_(graphics).

According to Wikipedia (see above), one of the founders of the cairo library was Keith Packard, who was also one of the key developers of X-Windows:

http://en.m.wikipedia.org/wiki/Keith_Packard

It was interesting to read that, because I had attended a training program on X, XLib, Xt and Motif, at the company where I worked, years ago; this was before X became widely available on Linux desktop computers; we used HP workstations during the course. I remember having a lot of fun during that course, along with the other participants. We kidded each other and the instructor a lot. Who can forget "DC the DC; Window theWindow", etc. :)
(DC being Device Context). Those were C / XLib declarations, from an X book used for the course, which might have been by Packard (or Gettys, IIRC).

I also remember a colleague of mine, creating a rudimentary version of a graphical paint program, like MS Paint, in Motif,  during the Motif part of the course, in under an hour or so. That was probably my first introduction to the power of frameworks, and to the concept of callbacks (in C). I don't think I really grokked them at the time, though I did, later.

http://cairographics.org/samples/

I had come across cairo some time ago, and made a note of it, because of its support for PDF output, an area I'm interested in, and also because of its  Python support.

I saw it again today via the page  below on preshing.com (which is the same site where I saw the info for my recent post on Mandelbrot graphics and Python):

http://preshing.com/20110920/the-python-with-statement-by-example

The above page gives a nice example of the use of Python's with statement, in the context of using PyCairo to draw some rectangles, each rotated by some angle from the previous one, without the transformations being additive (in a sense - see the post for details).

- Vasudev Ram