[Tutor] Pythonify this code!

Muhammad Ali ali.jan at gmail.com
Tue Jul 14 10:46:14 CEST 2009


Hi everyone,

Thanks for the comments Dave and Alan!

Based on these I have updated the code... I don't know if this is more
readable.

@Dave: I kept the original separate and combine function with bases as I
thought I would move them to a math library if I need to work in other
bases, however now I have followed your advice and made them more specific
to the solution. I couldn't understand the bit about how to use format.
Could you check it out in the context of the new code? Also didn't get the
bit about zip() either, however it got me thinking and I used map instead.

I am still not very happy about the separateToList function :( and I hope I
haven't been overzealous in using map(). I have also changed the text
concatenation.

Here's the code:

import Image

ENDTEXT = '~~'

def separateToList(num):
    """
    changes an integer into a list with 0's padded to the left if the number
is in tens or units
    """
    assert(num <= 255)
    s = str(num)
    li = []
    if len(s) > 2:
        li = [s[0:1], s[1:2], s[2:3]]
    elif len(s) > 1:
        li = [0, s[0:1], s[1:2]]
    elif len(s) > 0:
        li = [0, 0, s[0:1]]

    return map(int, li)

def getCharFromRGB(rgb):
    """
    takes an rgb tuple and changes it to an ascii char by taking the unit
digits of each color
    """
    rgb = map(lambda x: x %10, rgb)
    ordChar = int(''.join(map(str, rgb)))
    assert(ordChar <= 255)
    return chr(ordChar)

def hidePartOfCharInColor(color, charDigit):
    """
    take a color as integer not exceeding 255 and replace the unit digit
with one of the ascii char's digit
    """
    assert(color <= 255)
    color -= (color % 10)
    color += charDigit
    if (color > 255):
        color -= 10
    return color

def incrementRowCol(imgWidth, row, col):

    row += 1
    if row == imgWidth:  row, col = 0, col+1
    return row, col

def encode(img, text):
    """
    Takes PIL image and any string, encodes the text in the image, saving
the changes to the image
    Returns False if text size is greater than pixels
    """
    x = 0
    y = 0
    height, width = img.size
    text = text + ENDTEXT

    if len(text) > height * width:
        return False

    pix = img.load()

    for c in text:
        ordAsList = separateToList(ord(c))
        rgb = pix[x, y]
        pix[x,y] = tuple(map(hidePartOfCharInColor, rgb, ordAsList))
        x, y = incrementRowCol(width, x, y)

    img.save(img.filename)
    return True

def decode(img):
    x = 0
    y = 0
    charList = []
    c = ""
    height, width = img.size
    pix = img.load()
    while True:
        lastc = c
        c = getCharFromRGB(pix[x, y])
        charList.append(c)

        if (c + lastc  == ENDTEXT):
            return ''.join(charList[:-2])

        x, y = incrementRowCol(width, x, y)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090714/ef1819dd/attachment-0001.htm>


More information about the Tutor mailing list