1、首先创建一个Pycharm工程

2、然后新建一个Python file

录入基本的内容:
import cv2
img=cv2.imread('pedal.jpg')
#显示图片
cv2.imshow('image',img)
#等待按键
cv2.waitKey(0)
#结束显示,销毁窗口
cv2.destroyAllWindows()
#保存图片
cv2.imwrite('example2.jpg',img)
截图如下:

3、采用F5调试运行

会出错,说明缺少cv2模块,如下如图:

4、cv2模块的安装
点击这个,打开Pycharm自带Terminal

然后输入:
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
回车运行

如此一来,cv2就安装成功了
5、运行程序
将jpg文件复制进来

得到运行的结果:

再按回车键,画面消失,程序正常退出
同时可以看到保存好的example2.jpg
