KULL-Centre/pdbwords

running the code on Ubuntu

Opened this issue · 0 comments

Rdk0 commented

Hi Kresten, this is very nice idea. Thank you for sharing.
I had to add tweaks to make it run on Ubuntu. I had some issues with handling newlines - I wanted to simply insert "\n" into the input string to break the output into separate lines.

I need to add a line in make_empty_image() to check if file exists

ImageMagic still keeps throwing these warnings:
convert-im6.q16: insufficient image data in file `line00.jpg' @ error/jpeg.c/ReadJPEGImage/1106 - but this does not seem to affect the output.

#!/usr/bin/env python

By Kresten Lindorff-Larsen, University of Copenhagen, 2015

import sys
import os

LETTERDIR = "letters"
MAXCHARS = 25
LINEBREAK = "xLBx"
LINEBREAK2 = "\n" # this allows to break lines by inserting using \n

def make_empty_image(image_fn):
if os.path.isfile(image_fn):
cmd = "rm %s" % (image_fn)
os.system(cmd)
cmd = "touch %s" % (image_fn)
os.system(cmd)

def letter_to_fn(letter):
if letter == ".":
letter = "stop"
if letter == "!":
letter = "exclamation"
if letter == "?":
letter = "question"
if letter == ",":
letter = "comma"
if letter == ":":
letter = "colon"
if letter == "-":
letter = "hyphen"
letter_fn = LETTERDIR + "/" + letter.lower() + ".jpg"
if os.path.isfile(letter_fn):
return letter_fn
else:
sys.stderr.write(
"I don't know the letter %s so I am replacing it with a space" % letter
)
letter = "_"
letter_fn = LETTERDIR + "/" + letter.lower() + ".jpg"
return letter_fn

def add_letter(letter, image_fn):
letter_fn = letter_to_fn(letter)
tmp_fn = "tmp.jpg"
cmd = "convert +append %s %s %s" % (image_fn, letter_fn, tmp_fn)
os.system(cmd)
cmd = "mv %s %s" % (tmp_fn, image_fn)
os.system(cmd)

def add_space(image_fn):
letter = "_"
add_letter(letter, image_fn)

def line_to_image(line, fn):
make_empty_image(fn)
for word in line:
if word != LINEBREAK2:
for letter in word:
add_letter(letter, fn)
if word != line[-1]:
add_space(fn)

def make_image(lines, image_fn):
for i, line in enumerate(lines):
line_fn = "line%02d.jpg" % i
line_to_image(line, line_fn)
make_empty_image(image_fn)
tmp_fn = "tmp.jpg"
for i, line in enumerate(lines):
line_fn = "line%02d.jpg" % i
cmd = "convert -append %s %s %s" % (image_fn, line_fn, tmp_fn)
os.system(cmd)
cmd = "mv %s %s" % (tmp_fn, image_fn)
os.system(cmd)
cmd = "rm %s" % (line_fn)
os.system(cmd)

def words_to_lines(words):
lines = []
line = []
chars_in_line = 0
for word in words:
if (word == LINEBREAK) or (word == LINEBREAK2):
lines.append(line)
line = []
chars_in_line = 0
elif chars_in_line + len(word) + 1 < MAXCHARS:
line.append(word)
chars_in_line += len(word)
else:
lines.append(line)
line = [word]
chars_in_line = len(word)
if len(line) > 0:
lines.append(line)
print(lines)
return lines

if name == "main":
words = sys.argv[1:][0].split(' ')
lines = words_to_lines(words)
make_image(lines, "proteinword.jpg")