python 用opencv把人像上半身剪切制作一寸照
时间: 2023-10-15 13:04:07 浏览: 132
要用Python和Opencv把人像上半身剪切并制作成一寸照片,可以按照以下步骤进行:
1. 导入必要的库和模块:cv2、numpy
```python
import cv2
import numpy as np
```
2. 读取原始图片
```python
img = cv2.imread('path/to/image')
```
3. 对图片进行剪切,只保留上半身部分
```python
height, width = img.shape[:2]
half_height = int(height / 2)
upper_body_img = img[0:half_height, :]
```
4. 将图片调整为一寸照片的尺寸
```python
resized_img = cv2.resize(upper_body_img, (295, 413))
```
其中,一寸照片的尺寸为295x413像素。
5. 保存剪切后的照片
```python
cv2.imwrite('path/to/save/image', resized_img)
```
将上述代码整合起来,可以得到以下完整代码:
```python
import cv2
import numpy as np
img = cv2.imread('path/to/image')
height, width = img.shape[:2]
half_height = int(height / 2)
upper_body_img = img[0:half_height, :]
resized_img = cv2.resize(upper_body_img, (295, 413))
cv2.imwrite('path/to/save/image', resized_img)
```
运行代码后,就可以得到剪切后的一寸照片。
阅读全文