一、簡單例子:
# -*- coding=UTF-8 -*-
import Image
from matplotlib import pyplot as plt
if __name__ == "__main__":
img = Image.open("./Alex.jpg")
img_gray = img.convert("L")
fig = plt.figure()
ax = fig.add_subplot(121)
ax.imshow(img)
ax = fig.add_subplot(122)
ax.imshow(img_gray, cmap="gray")#以灰度圖顯示圖片
ax.set_title("hei,i'am the title")#給圖片加titile
#plt.axis("off")#不顯示刻度
plt.show()#顯示剛纔所畫的所有操作
圖片的其他處理,可以查看我的前幾篇文章。
二、簡單說一下
ax = fig.add_subplot(121)
裏的121.第一個“1”代表圖片只有一行;第一個“2”代表有兩列;第二個“1”代表第一張圖片在1行2列的矩陣中的位置。
如果是一個2*2的矩陣,第三個數字的排序是:
1 2
3 4
即,以行爲主
當然還會出現這樣的需求:
# -*- coding=UTF-8 -*-
import Image
from matplotlib import pyplot as plt
if __name__ == "__main__":
img = Image.open("./Alex.jpg")
img_gray = img.convert("L")
fig = plt.figure()
ax = fig.add_subplot(121)
ax.imshow(img)
ax.set_title("hei,i'am the first")
ax = fig.add_subplot(222)
ax.imshow(img_gray, cmap="gray")#以灰度圖顯示圖片
ax.set_title("hei,i'am the second")#給圖片加titile
ax = fig.add_subplot(224)
ax.imshow(img_gray, cmap="gray")#以灰度圖顯示圖片
ax.set_title("hei,i'am the third")#給圖片加titile
#plt.axis("off")#不顯示刻度
plt.show()#顯示剛纔所畫的所有操作
效果:
三、需求:在圖中框出你想要的區域:
# -*- coding=UTF-8 -*-
import Image
from matplotlib import pyplot as plt
if __name__ == "__main__":
img = Image.open("./Alex.jpg")
img_gray = img.convert("L")
fig = plt.figure()
ax = fig.add_subplot(121)
ax.imshow(img)
ax.set_title("hei,i'am the first")
pointx = [20, 120, 120, 20, 20]
pointy = [20, 20, 120, 120, 20]
ax.plot(pointx, pointy, 'r')#畫一個矩形,黑色;'r'紅色
效果:
四、或者你想要畫點:
# -*- coding=UTF-8 -*-
import Image
from matplotlib import pyplot as plt
if __name__ == "__main__":
img = Image.open("./Alex.jpg")
img_gray = img.convert("L")
fig = plt.figure()
ax = fig.add_subplot(121)
ax.imshow(img)
ax.set_title("hei,i'am the first")
pointx = [20, 120, 120, 20, 20]
pointy = [20, 20, 120, 120, 20]
ax.plot(pointx, pointy, 'r')#畫一個矩形,黑色;'r'紅色
ax.scatter(65, 70)#畫點
ax.scatter(90, 70)#畫點
plt.axis("off")#不顯示刻度
效果:
(額,我怎麼把我男神搞成這個樣子了。。。。)