kull-centre / pdbwords Goto Github PK
View Code? Open in Web Editor NEWPrinting text using protein structures
License: GNU General Public License v3.0
Printing text using protein structures
License: GNU General Public License v3.0
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
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")
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.