python读取图片并且显示

本文介绍如何使用Python结合OpenCV库读取图片,并通过OpenCV和matplotlib库进行图片显示的方法。文中提供了两种不同的图片读取方式:灰度读取和RGB颜色读取,以及展示图片的具体实现代码。

使用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()

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值