Git Product home page Git Product logo

pdbwords's People

Contributors

lindorff-larsen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pdbwords's Issues

running the code on Ubuntu

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")

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.