"Muhammad Ali" <ali....@gmail.com> wrote
I tried my hand at steganography
Never heard of it before!
#The code starts here:
def separate(num, base):
li = []
while num / base > 0:
li.insert(0, num % base)
num = num / base
li.insert(0,num)
return li
def combine(tup, base):
num = 0
mul = pow(base, len(tup) - 1)
for i in tup:
num = num + i * mul
mul = mul / base
A general comment is that you can use the operator shortcuts
num /= base
mul /= base
same for the incrementing below.
return num
import Image
def encode(img, text):
...
for c in text:
li = separate(ord(c), 10)
...
r, g, b = pix[x, y]
r = r - (r % 10) + li[0]
if (r > 255):
r = r - 10
g = g - (g % 10) + li[1]
if (g > 255):
g = g - 10
b = b - (b % 10) + li[2]
if (b > 255):
b = b - 10
The 3 sections above are nearly identical, you could create
a helper function to do the work.
def f(item, aList, index)
r = f(r,li,0)
g = f(g,li,1)
b = f(b,li,2)
Just think of a descriptive name for f()!
pix[x,y] = (r,g,b)
if y == width - 1:
y = 0
x = x + 1
else:
y = y + 1
img.save(img.filename)
def decode(img):
x = 0
y = 0
text = ""
c = ""
height, width = img.size
pix = img.load()
while 1:
While 1 is now deprecated inf favour of while True
r, g, b = pix[x, y]
if (c == '~') and chr(combine([r % 10, g % 10, b % 10], 10)) ==
'~':
return text[:len(text) - 1]
This could be:
return text[:-2]
c = chr(combineUnits([r % 10, g % 10, b % 10], 10))
text = text + c
if y == width - 1:
y = 0
x = x + 1
else:
y = y + 1
Again, this is repeated code so could go in a helper function.
I didn't notice anything that would be v3 specific. Also I can't
comment on the algorithm since I don't really know what its
doing!
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor