def insert_text(img, dest, msg):
image = Image.open(img)
width, height = image.size
if image.mode[:3] != "RGB" or width * height * 3 < len(msg) + RESERVED_PIXELS * 3:
raise IndexError("Unable to use this picture")
bits = bin(len(msg))[2:].zfill(RESERVED_PIXELS * 3)
msg = bits + msg
msg = enumerate(msg + "0" * (3 - len(msg) % 3))
p = image.load()
for i, j in pixels(image.size):
try:
rgb = map(lambda color, bit: color - (color % 2) + int(bit), p[i, j][:3], [msg.next()[1] for _ in xrange(3)])
p[i, j] = tuple(rgb)
except StopIteration:
image.save(dest, "PNG", quality = 100)
return
评论列表
文章目录