Back in CPSC 101, we did a lot of image editing. We only used PPM images in class and I was thinking the other day about how even though I hated the project at the time, it was pretty interesting. So, I decided to re-code some of it in Python. Basically, all I’ve done is written a script that will allow the conversion of any color PPM image into greyscale. I feel like this can be pretty handy, as it’s fast and seems to do a pretty good job.

Here’s the before image (Ironically, I had to convert to another image format before posting):

alt text

And here’s the output:

alt text

A few particulars about the files and scripts:

  • I used the P3 header, which is easier because it allows you just to write ASCII into the file.

  • Also, you must ensure that the header of the input photo must only have 3 lines. I’ve found that if you convert a JPEG from Gimp, it adds its on line in the header. It was easy to just do ‘nano in.ppm’ in terminal and remove the line they put in.

  • The script can be run using ‘python <input filename>’

  • This ONLY works on PPM images. Maybe someday I’ll look into another image format.

And that’s it!

Here’s the code, check it out:

#Greyscale Converter
#Spencer Smith
#Mar. 22, 2010
import sys
#Open file
FILE = open(sys.argv[1],"r")
#Read input file
indata = FILE.readlines()
#Strip header
del indata[0]
size = indata[0]
del indata[0:2]
#Open output image
GREY = open("out_grey.ppm","w")
#Write header to output image
#Calculate and write RGB values
counter = 0
for x in indata:
  val = x
  if counter % 3 == 0:
    val1 = int(val)*.3
  elif counter % 3 == 1:
    val2 = int(val)*.59
  elif counter % 3 == 2:
    val3 = int(val)*.11
  px = val1+val2+val3
  GREY.write("%d\n%d\n%d\n" % (int(px),int(px),int(px)))
  counter = counter + 1
#Close files

Hi all,

So this week, I’ve been in a crunch to get some last minute cash for spring break. My Dad had a little opportunity for me to write a Python script for him to use at the office. The back story is basically that the warehouse where he works has to take inventory using some sort of hand-held scanning devices.

He wanted me to write a script that, when placed in a directory with any given number of output files from the devices, generates a document stating each file and how many cartons are in each pallet. Sounds like a perfect job for Python, right? Exactly right. It literally took me about 45 minutes to 1 hour and I had the thing completely off the ground running. I’ve decided to post it not so much because this exact program is really useful to anyone else, but because I definitely learned some cool I/O stuff while doing it.

First, I think it’s useful to get an idea of how the files looked coming in. They were basically Wordpad documents generated by Windows CE and didn’t have much format to them. Here’s a little example:

* This is a Microsoft WordPad document.
* For further details visit the Microsoft Windows CE web site, at
* Or search MSDN for 'Microsoft WordPad'
}\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Tahoma;}}
{\colortbl ;}
\viewkind4\uc1\pard\cf0\f0\fs22 008022376\par

As you can see, It’s a little hard to decipher. Also, keep in mind that this is not a full file as I didn’t want to post 600 lines in there. Anywhere there is a ‘\par’, except for the lone one at the end, there is a carton in the warehouse. All I had to do was figure out a way to add.

My program gave the following output:

Inventory Count
Generated: 03/09/2010 21:45:37
Filename           # of Cartons
m468                602
m466                286
m461                144
m465                245
m464                199

So, as you can see, it seems to work pretty well. I can also imagine this is WAY easier than counting by hand. So, here is the code that converts one to the other. Be sure to notice the usefulness of the calls like getcwd(), which proved to be awesome for finding all the files in the directory.

#Spencer Smith
#Inventory Calculator
#Created 3/9/2010
import sys, os
from time import strftime
#Name of output file
filename2 = "inventory_count.txt"
#Get all files in current directory
total_files = os.listdir(os.getcwd())
pwd_files = list()
#Find all files with '.pwd' extension
for x in total_files:
   if x.find(".pwd") != -1:
#Return error to 'error.log' if there are no '.pwd' files in the directory
if len(pwd_files) == 0:
  FILE = open("error.log","w")
  FILE.write(strftime("%m/%d/%Y %H:%M:%S") + " -- No files '.pwd' files in directory to read.")
#Open output file for writing
OUT = open(filename2,"w")
#Create header
OUT.write("Inventory Count\n")
OUT.write("Generated: "+strftime("%m/%d/%Y %H:%M:%S")+"\n")
OUT.write("Filename           # of Cartons\n")
#Iterate through available '.pwd' files
for name in pwd_files:
#Open file for reading
   FILE = open(name,"r")
   counter = 0
   suffix = "\par"
#Search each line for "\par". "\par" on the line means that there is a carton on this line.
#Increment carton counter if found
   for line in FILE:
      if line.find(suffix) != -1:
        counter = counter + 1
#After reading all lines in file, write filename and count to output file.
   OUT.write("%s                %d\n" % (name[0:-4],counter-1))
#Close current file and reset.
#Close output file so that it can be read.

Enjoy! Hope someone finds it useful!

I am in a Distributed Computing class this semester. We just had to write a brute-force password cracker using the Condor grid we have on-campus. It really took forever to generate an 8 character (a-z) code even though I ran it 676 different times. Total run-time was about 25 minutes. Anyways, we also had to create a password cracker using a dictionary file. I thought this was an awesome project and it was so much faster! I’ve decided to post the dictionary Python script. It should crack any word, given that it is in whatever dictionary you choose to use it against. My word for the project was “schnecke”, which is German for “snail”. Knowing that, I checked it against the Ubuntu New German dictionary. The run-time was ~.3 secs.

Check it out!

P.S. In order to get language dictionaries to check against in Ubuntu, it is as easy as “apt-get install ngerman” or whatever language package you need. I found a list of these packages with a quick Google search. If you’re doing more evil than good, you can also easily find a list of “common passwords” in text form.

#Spencer Smith
#Usage: python <md5 encrypted password> <dictionary path>
#Example: python ...
#... 75d3d991f75c3bcbecbbcaa712baa678 /usr/shared/dict/ngerman
import time
import hashlib
import sys
#start runtime clock
start = time.time()
#set md5 encrypted password from command line we are looking for
solution = str(sys.argv[1])
guess=" "
sol = "no solution found"
#open dictionary file
filename = open(str(sys.argv[2]),'r')
#for each entry, generate md5 encryption and check ...
# ... against desired solution
for line in filename:
  m = hashlib.md5()
  #cut off '\n' character
  guess = m.hexdigest()
  if guess == solution:
    sol = line
#close dictionary file
#stop time
end = time.time()
#calculate rough estimate of run-time
t_time = end - start
print "total runtime was -- ",t_time," seconds and the answer was:",sol