python实现图片裁剪代码
时间: 2023-08-06 12:09:58 浏览: 196
js裁剪图片代码
5星 · 资源好评率100%
以下是使用Python实现图片裁剪的代码示例:
```python
from PIL import Image
def crop_image(image_path, output_path, left, upper, right, lower):
img = Image.open(image_path)
cropped_img = img.crop((left, upper, right, lower))
cropped_img.save(output_path)
# 示例用法
crop_image("./data/cut/thor.jpg", "./data/cut/pil_cut_thor.jpg", 0, 0, 512, 128)
```
这段代码使用了Pillow库来进行图片的裁剪。首先,通过`Image.open()`方法打开原始图片。然后,使用`crop()`方法传入裁剪区域的左上角和右下角坐标来裁剪图片。最后,使用`save()`方法将裁剪后的图片保存到指定路径。在示例中,裁剪区域的左上角坐标为(0, 0),右下角坐标为(512, 128)。
#### 引用[.reference_title]
- *1* [Python实现图片裁剪的两种方式——Pillow和OpenCV](https://blog.csdn.net/hfutdog/article/details/82351549)[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^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [如何用python裁剪图片](https://blog.csdn.net/m0_59485658/article/details/123808484)[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^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [Python 实现图片裁剪(附代码) | Python工具](https://blog.csdn.net/zhiweihongyan1/article/details/122605995)[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^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文