2023-01-04 更新补充试验,使用国内镜像源来提高下载速度
默认情况下 pip 使用的是国外的镜像,在下载的时候速度非常慢,我们可以通过使用国内镜像源来提高下载速度,例如使用国内清华大学的源,地址如下:
https://pypi.tuna.tsinghua.edu.cn/simple
直接使用国内镜像源,可以在 pip 命令中使用-i参数来指定镜像地址,如通过以下命令使用清华镜像源安装 numpy 和 opencv-python 包。
python -m pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
python -m pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
1. 先安装 NumPy
库,使用 OpenCV
需要依赖 NumPy 这个库
python -m pip install numpy
Collecting numpy
Using cached numpy-1.23.1-cp38-cp38-win_amd64.whl (14.7 MB)
Installing collected packages: numpy
Successfully installed numpy-1.23.1
2. pip
安装 OpenCV
库的名称是 opencv-python
,所以下面命令错误
python -m pip install opencv
ERROR: Could not find a version that satisfies the requirement opencv (from versions: none)
ERROR: No matching distribution found for opencv
3. pip
安装 OpenCV
库的名称是 opencv-python
python -m pip install opencv-python
Collecting opencv-python
Downloading opencv_python-4.6.0.66-cp36-abi3-win_amd64.whl (35.6 MB)
---------------------------------------- 35.6/35.6 MB 599.9 kB/s eta 0:00:00
Requirement already satisfied: numpy>=1.17.3 in c:\users\vip\appdata\local\programs\python\python38\lib\site-packages (from opencv-python) (1.23.1)
Installing collected packages: opencv-python
Successfully installed opencv-python-4.6.0.66
4. 使用 OpenCV
库转换图片格式示例:把png格式转换jpg和webp格式
import cv2
img = cv2.imread('test.png')
cv2.imwrite('test.jpg', img)
cv2.imwrite('test.webp', img)
Linux 系统安装 OpenCV 库, 安装的时候会自动安装依赖库 numpy
python3 -m pip install --upgrade pip # 先更新pip
pip3 install opencv-python
Collecting opencv-python
Downloading opencv_python-4.6.0.66-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (60.9 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 60.9/60.9 MB 10.1 MB/s eta 0:00:00
Collecting numpy>=1.17.3
Downloading numpy-1.23.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 17.1/17.1 MB 15.0 MB/s eta 0:00:00
Installing collected packages: numpy, opencv-python
Successfully installed numpy-1.23.1 opencv-python-4.6.0.66