Dear all, I'm trying to create oblique texts by reordering their letters. Here is an exemple to illustrate this (to be display in a monospaced font):
text = """\ ... This ... is ... a ... test""" print(make_oblique(text)) T i h a s i t s e s t
So far I have this: def make_oblique(text): words = text.splitlines() matrix = [[" " for i in xrange(len(words) + len(max(words)))] \ for j in xrange(len(words) + len(max(words)))] for i, word in enumerate(words): for j, letter in enumerate(word): matrix[j + i][j] = letter matrix = map(lambda x: "".join(x), matrix) return "\n".join(matrix) But it is not very flexible. For instance, I'd like to control the "line spacing" (by adding extra spaces in between letters since it isn't real lines). I just found numpy and I have the intuition that it could do the job since it deals with matrices. Am I right? If so how would you do such a thing? Thanks! Alex
04.10.2011 21:10, Alexandre Leray kirjoitti: [clip]
But it is not very flexible. For instance, I'd like to control the "line spacing" (by adding extra spaces in between letters since it isn't real lines). I just found numpy and I have the intuition that it could do the job since it deals with matrices.
Am I right? If so how would you do such a thing?
import numpy as np def make_oblique(words): n = max(2*len(word) for word in words) + len(words) canvas = np.zeros((n, n), dtype='S1') canvas[...] = ' ' # ye mighty FORTRAN, we beseech thee for j, word in enumerate(words): i = np.arange(len(word)) canvas[i+j, 2*i] = list(word) canvas[:,-1] = '\n' return canvas.tostring().rstrip() -- Pauli Virtanen
Thanks Pauli, exactly what I wanted! On 04/10/2011 22:22, Pauli Virtanen wrote:
04.10.2011 21:10, Alexandre Leray kirjoitti: [clip]
But it is not very flexible. For instance, I'd like to control the "line spacing" (by adding extra spaces in between letters since it isn't real lines). I just found numpy and I have the intuition that it could do the job since it deals with matrices.
Am I right? If so how would you do such a thing?
import numpy as np
def make_oblique(words): n = max(2*len(word) for word in words) + len(words) canvas = np.zeros((n, n), dtype='S1') canvas[...] = ' ' # ye mighty FORTRAN, we beseech thee
for j, word in enumerate(words): i = np.arange(len(word)) canvas[i+j, 2*i] = list(word)
canvas[:,-1] = '\n' return canvas.tostring().rstrip()
participants (2)
-
Alexandre Leray
-
Pauli Virtanen