python opencv按照比例对原图像缩放
时间: 2023-10-10 11:07:00 浏览: 198
基于Opencv的图像缩放
要按照比例对原图像进行缩放,可以使用Python和OpenCV库来实现。以下是一个代码示例:
```python
import cv2
def resize_image_proportionally(image_path, scale):
# 读取图片
img = cv2.imread(image_path)
# 获取原始分辨率
height, width = img.shape[:2]
# 按比例缩放
new_height = int(height * scale)
new_width = int(width * scale)
resized_img = cv2.resize(img, (new_width, new_height))
# 输出结果
cv2.imshow('Resized Image', resized_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 调用函数进行缩放
image_path = 'path_to_image.jpg' # 替换为实际的图像路径
scale = 0.5 # 替换为想要缩放的比例
resize_image_proportionally(image_path, scale)
```
这段代码会按照指定的比例对原图像进行缩放,并在窗口中显示缩放后的图像。你可以替换`image_path`变量为你想要缩放的图像的路径,以及替换`scale`变量为你想要的缩放比例。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [Python Opencv等比例缩放图片](https://blog.csdn.net/FriendshipTang/article/details/124118026)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* [Python-OpenCV对图片进行缩放](https://blog.csdn.net/Scarlett2025/article/details/117964622)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文