画像内のすべてのピクセルをスクランブルしようとしていますが、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()