mixing pixels of an image manually using python -


i trying create algorithm blends pixels of image , can bring image before, not know this.

i'm using python , pil, can use other libraries.

exemple: enter image description here enter image description here , enter image description here

thank you.

this should it. there's no error handling, doesn't follow pep8 standards, uses slow pil operations , doesn't use argument parsing library. i'm sure there other bad things also.

it works seeding python's random number generator invariant of image under scrambling. hash of size used. since size doesn't changed, random sequence built on same images share same size. sequence used one-to-one mapping, therefore it's reversible.

the script may invoked twice shell create 2 images, "scrambled.png" , "unscrambled.png". "qfhe3.png" source image.

python scramble.py scramble "./qfhe3.png" python scramble.py unscramble "./scrambled.png" 

-

#scramble.py pil import image import sys import os import random  def openimage():     return image.open(sys.argv[2])  def operation():     return sys.argv[1]  def seed(img):     random.seed(hash(img.size))  def getpixels(img):     w, h = img.size     pxs = []     x in range(w):         y in range(h):             pxs.append(img.getpixel((x, y)))     return pxs  def scrambledindex(pxs):     idx = range(len(pxs))     random.shuffle(idx)     return idx  def scramblepixels(img):     seed(img)     pxs = getpixels(img)     idx = scrambledindex(pxs)     out = []     in idx:         out.append(pxs[i])     return out  def unscramblepixels(img):     seed(img)     pxs = getpixels(img)     idx = scrambledindex(pxs)     out = range(len(pxs))     cur = 0     in idx:         out[i] = pxs[cur]         cur += 1     return out  def storepixels(name, size, pxs):     outimg = image.new("rgb", size)     w, h = size     pxiter = iter(pxs)     x in range(w):         y in range(h):             outimg.putpixel((x, y), pxiter.next())     outimg.save(name)  def main():     img = openimage()     if operation() == "scramble":         pxs = scramblepixels(img)         storepixels("scrambled.png", img.size, pxs)     elif operation() == "unscramble":         pxs = unscramblepixels(img)         storepixels("unscrambled.png", img.size, pxs)     else:         sys.exit("unsupported operation: " + operation())  if __name__ == "__main__":     main() 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -