所以我使用PIL从图像中获得像素数组。我拿像素数组并随机化数组的顺序。然后我在数组上使用快速排序,我想知道如何可视化像素的排序。我以为我可以使用Matplotlib做到这一点,但是,我只是不确定如何做到这一点。这是我用来抓取图像数据并对其进行排序的代码。我可以让它对图像和输出进行排序,但是,我希望能够实时看到排序。
import random
from PIL import Image
import numpy as np
import scipy.misc as smp
import matplotlib.pyplot as plt
def quickSort(alist):
quickSortHelper(alist,0,len(alist)-1)
def quickSortHelper(alist,first,last):
if first<last:
splitpoint = partition(alist,first,last)
quickSortHelper(alist,first,splitpoint-1)
quickSortHelper(alist,splitpoint+1,last)
def partition(alist,first,last):
pivotvalue = alist[first]
leftmark = first+1
rightmark = last
done = False
while not done:
while leftmark <= rightmark and alist[leftmark] <= pivotvalue:
leftmark = leftmark + 1
while alist[rightmark] >= pivotvalue and rightmark >= leftmark:
rightmark = rightmark -1
if rightmark < leftmark:
done = True
else:
temp = alist[leftmark]
alist[leftmark] = alist[rightmark]
alist[rightmark] = temp
temp = alist[first]
alist[first] = alist[rightmark]
alist[rightmark] = temp
return rightmark
img = Image.open('image2.png')
pixelValues = list(img.getdata())
data = np.zeros((1024, 1024, 3), dtype=np.uint8)
for i in xrange(len(pixelValues)):
pixelValues[i] = pixelValues[random.randint(0, 1048575)]
quickSort(pixelValues)
writtenImg = Image.new("RGB", (1024,1024), "white")
writtenImg.putdata(pixelValues)