【发布时间】:2012-10-17 02:47:15
【问题描述】:
我正在尝试打乱图像中的所有像素,而我对 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()
【问题讨论】:
-
那个蒙娜丽莎好像有胡子。此外,在
shuffle中,您正在遍历图像中的每一行。尝试reshape将图像放入一维数组,然后对其进行洗牌。 -
IIRC
shape将是(height, width, channels),因此他的reshape应该已经将其变平了。 -
这是 Marcel Duchamp 的 LHOOQ。 en.wikipedia.org/wiki/L.H.O.O.Q。行 data = data.reshape((shape[0]*shape[1],shape[2])) 应该按照你说的做。我想打乱像素位置而不是它们的颜色。
标签: python numpy python-imaging-library random-sample