I have searched on Google but I couldn't find anything.
I want to create a Python script that can import an image, change the order of pixels, and save an output image.
I have worked with Python a lot, but only with the built-in libraries. So if I have to use new commands, please describe it as much as you can.
import sys
import random
from PIL import Image
BLOCKLEN = 64 # Adjust and be careful here.
img = Image.open(sys.argv[1])
width, height = img.size
xblock = width / BLOCKLEN
yblock = height / BLOCKLEN
blockmap = [(xb*BLOCKLEN, yb*BLOCKLEN, (xb+1)*BLOCKLEN, (yb+1)*BLOCKLEN)
for xb in xrange(xblock) for yb in xrange(yblock)]
shuffle = list(blockmap)
random.shuffle(shuffle)
result = Image.new(img.mode, (width, height))
for box, sbox in zip(blockmap, shuffle):
c = img.crop(sbox)
result.paste(c, box)
result.save(sys.argv[2])
Example input, BLOCKLEN = 1
, BLOCKLEN = 64
, BLOCKLEN = 128
:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With