我试图打乱图像中的所有像素,而我对 Knuths shuffle(以及其他人的)的实现似乎失败了。似乎每行都在工作。我不知道为什么——就是看不到。
这是发生的事情:
这不是很混乱!好吧,它可能会更加混乱,而且需要更加混乱。
这是我的代码:
import Image
from numpy import *
file1 = "lhooq"
file2 = "kandinsky"
def shuffle(ary):
a=len(ary)
b=a-1
for d in range(b,0,-1):
e=random.randint(0,d)
ary[d],ary[e]=ary[e],ary[d]
return ary
for filename in [file1, file2]:
fid = open(filename+".jpg", 'r')
im = Image.open(fid)
data = array(im)
# turn into array
shape = data.shape
data = data.reshape((shape[0]*shape[1],shape[2]))
# Knuth Shuffle
data = shuffle(data)
data = data.reshape(shape)
imout = Image.fromarray(data)
imout.show()
fid.close()