PNGEncode and PNGDecode v0.1 Beta

I was looking for a utility to convert any file to an image format but couldn’t any “free”. So I’m posting two quick and dirty Python scripts I wrote. One for encoding a file to PNG format and one for decoding the PNG file back to the original file.  FYI, the Python scripts assume that the Python Image Library is already installed along with Python 2.4 or later.

PNGEncode v0.1 Beta

import Image
import ImageFile
import ImageDraw
import os.path
import sys
from math import *

filename =sys.argv[1]

filesize=os.path.getsize(filename)

#print filesize

squarelength=int(ceil(sqrt(filesize))
)

#print squarelength

imagesize=pow(squarelength,2)

#print imagesize

FILE = open(filename, “rb”)

im = Image.new(“RGB”,(squarelength, squarelength),(0,0,0))

x=0
y=0
i=0

while (i<filesize):

byte= FILE.read(1)

#print squarelength, x, y

byte=ord(byte)

rgb= byte,byte,1

im.putpixel((x,y), rgb)

if (x<squarelength-1):
x=x+1
else:
x=0
y=y+1

i=i+1

FILE.close()

im.save(sys.argv[2],”PNG”)
PNGDecode.py v0.1 Beta
import Image
import ImageFile
import ImageColor
import ImageDraw
import os.path
import sys
from math import *

filename =sys.argv[2]

im = Image.open(sys.argv[1])

im = im.convert(“RGB”)

#draw = ImageDraw.Draw(im)

x=0
y=0
i=0

FILE = open(filename, “wb”)

w,h = im.size;

while (y<h):

rgb = im.getpixel((x,y))

#rgb = ImageColor.getrgb(color)

r,g,b = rgb

if ( b==1):
FILE.write(chr(r))

if (x<w-1):
x=x+1
else:
x=0
y=y+1

FILE.close()

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: