Python for generating CFDG files from images

Here you can discuss and share functionality improvements and helper programs to make Context Free better.

Moderators: MtnViewJohn, chris, mtnviewmark

Post Reply
jimmythemagnet
Posts: 1
Joined: Mon Jul 11, 2005 9:52 pm
Location: Sydney, Australia
Contact:

Python for generating CFDG files from images

Post by jimmythemagnet »

Below is the Python code for converting any kind of image into a set of CFDG rules. It's hardly tested but seems to work alright. It was written under windows and requires the Python Imaging Library to be installed in order to work (you can google for that).

My commandline usage looks like this (from same directory your img2cfdg.py file is):

c:\Python24\python.exe img2cfdg.py mystartrule myprefix inputimage.jpg > out.cfdg

The startrule option is obvious, the prefix is a mechanism to avoid duplicate rule names if you will be including lots of images.

CFDG isn't supposed to be about pixels, but I thought this might be useful to anyone who wanted to import pixel based fonts (and share them with the rest of us).

enjoy

jimmy

Code: Select all

import os, sys
import Image

def main(argv=None):
    if argv is None:
        argv = sys.argv
    if len(argv) < 4:
        print "img2cfdg startrule prefix filenametoconvert"
        sys.exit()
        
    startrule = argv[1]
    prefix = argv[2]
    infile = argv[3]

    try:
        greyimg = Image.open(infile).convert("L")
    except IOError:
        print "cannot convert", infile
        sys.exit()
        
    gwidth, gheight = greyimg.size

    print "startshape %s" % startrule
    print "rule %s {" % startrule

    #Call the rule for each row
    for i in range(gheight):
        # "gheight - i" because my jpegs were coming out upside down
        # if your images come out upside down remove "gheight -"
        # there should be a flag for this, but you have the code and i'm lazy
        print "%s_row_%d { y %d }" % (prefix, i, gheight - i)
    print "}"

    #Rules for rows
    for i in range(gheight):
        print "rule %s_row_%d {" % (prefix, i)        
        for j in range(gwidth):
            tshade = greyimg.getpixel( (j,i) )
            print "SQUARE { x %d b %.3f }" % (j, tshade/255.0)
        print "}"


if __name__ == "__main__":
    main(["img2cfdg", "test", "test2","test1.jpg"])

ehuber
Posts: 3
Joined: Sat Jul 09, 2005 10:36 pm

Post by ehuber »

This sounds pretty good.

When I first read the topic, I was very amazed, expecting to find within a program that would somehow analyze an image such that the circles, squares, etc comprising the script would somehow be the optimal 'solution' to the image. Pipe dream (: That would make compression wonderful, wouldn't it?

In any case, neat -- although I don't grok python (: Maybe this could be incorporated with the current code?

ShonSpawnei
Posts: 1
Joined: Tue Jul 03, 2018 1:28 am

I have a problem with Drivers

Post by ShonSpawnei »

Hi.
I'm faced with a problem. I can not find a driver for my PDA(HP).
There are a lot of viruses or doesn't work at all or more,they want me to make a paid subscription.Need a help.

Post Reply