python3-opencv打开图片

本文详细介绍使用Python和OpenCV库进行图像处理的过程,包括读取、调整尺寸、显示、裁剪图片,以及将彩色图像转换为灰度图像。通过具体代码示例,展示了如何获取图像信息、调整图像大小、显示图像、保存图像,并实现图像从彩色到灰度的转换。
import cv2
import numpy as np

def get_image_info(image):
    print(type(image))
    print(image.shape)
    print(image.size)
    print(image.dtype)
    pixel_data = np.array(image)
    print(pixel_data)

img = cv2.imread(r'test.png')  #读取项目文件下的test.png图片到img
new_img = cv2.resize(img, (416, 416), interpolation=cv2.INTER_AREA)  #把img读入的图片缩小到416*416并传给new_img
cv2.imshow("cat", new_img)  #显示new_img图片,显示窗口为cat
get_image_info(img)
cv2.imwrite(r'testa.png', new_img)  #把new_img图片写入项目文件目录下并命名为testa.png
flag = min(img.shape[0], img.shape[1])
new_img2 = img[((img.shape[0]-flag)//2):((img.shape[0]+flag)//2), ((img.shape[1]-flag)//2):((img.shape[1]+flag)//2)]
new_img2 = cv2.resize(new_img2, (416, 416), interpolation=cv2.INTER_AREA)
cv2.imwrite(r'cat.png', new_img)
gray = cv2.cvtColor(new_img2, cv2.COLOR_BGR2GRAY)
cv2.imwrite(r'testb.png', gray)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值