python讀取圖片並且顯示

使用python-opencv讀取圖片,利用opencv或matplotlib顯示圖片。

# -*- coding: utf-8 -*-

import numpy as np
from matplotlib import pyplot as plt
#import urllib
import cv2

def load_image1(file):
	# Load an color image in grayscale
	img = cv2.imread(file,0)
	cv2.imshow('image',img)
	cv2.waitKey(0)
	cv2.destroyAllWindows()
	
def load_image2(file):
	# Load an color image in RGB color scale
	img = cv2.imread(file,1)
	cv2.namedWindow('image', cv2.WINDOW_NORMAL)
	cv2.imshow('image',img)
	k = cv2.waitKey(0)
	 
	if k == ord('s') or k == ord('S'):
        # save image
		cv2.imwrite('messigray.png',img)
		print('Save image ', file, ' as png format.')
	
	#cv2.imwrite('messigray1.png',img)
	cv2.destroyAllWindows()
	
def matplotlibShowImage(picture):
	img = cv2.imread(picture,0)
	plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
	plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
	plt.show()

def main():
	file = "F:\\dataSet\\picture\\000006.jpg"
	#load_image1(file)
	load_image2(file)
	#matplotlibShowImage(file)

if __name__ == '__main__':
	main()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章