我使用的是iPython筆記本,我有一個512x512圖像,我想用'最接近'的顏色插值'變換'爲'塊狀'圖像塊,類似於Photoshop中的「馬賽克」濾鏡,以進一步處理一批圖片。使用matplotlib和/或numpy創建塊狀圖像的圖像調色板的尺寸縮小
在Photoshop中,加載圖像,並可以將過濾器設置爲「馬賽克」,並設置在像素中的「塊大小」。
Here有一個關於如何使用numpy來做的部分解釋,但是因爲我不是一個numpy專家,所以我發現了一個'快捷方式',這對我來說是一種使用PIL的工作。我不知道如何適當調整它以避免一些不必要的步驟。
img = Image.open(filename)
h = img.size[0]
v = img.size[1]
blocksize = 100
rsize = img.resize((h/blocksize,v/blocksize)) # resize the image
rsizeArr = np.asarray(rsize)
lum_img = rsizeArr[:,:,0]
#plt.axes.get_xaxis().set_visible(False)
#plt.axes.get_yaxis().set_visible(False)
imgplot = plt.imshow(rsizeArr)
#imgplot = plt.imsave('test.jpg', rsizeArr)
imgplot.set_interpolation('nearest')
imgplot.axes.get_xaxis().set_visible(False)
imgplot.axes.get_yaxis().set_visible(False)
plt.savefig('testplot1.png', bbox_inches = 'tight')
由於我使用IPython的筆記本,這隻要在執行生產線生產的筆記本內嵌輸出。 (我不想顯示此特定步驟的內聯輸出,因爲我將在筆記本中處理多張圖片)。
的「tesplot1.png」文件的大小1KB,但是,出於某種原因,它的尺寸獲取的減少爲264x264像素。因爲我想在512×512的原大小的圖像,我做到以下幾點:
img2 = Image.open('testplot1.png')
rsize = img2.resize((512,512)) # resize the image
rsize.save('testplot2.jpg')
這將生成512×512像素的右側圖像。
我的問題是: 1)有沒有辦法產生的圖像不必通過imgplot = plt.imshow(rsizeArr)內聯顯示它?我需要在我的筆記本上內聯pylab,所以我不知道它是否可以「暫時」禁用。 2)有沒有辦法通過將'tesplot1.png'保存爲合適的大小來忽略大小調整步驟(img2)? 3)使用解釋here完成此操作所需的矢量操作是什麼?
謝謝你馬特。我找到了另一個解乾杯 –