两种切割裁剪,切割图片的方法

1.使用opencv来切割,这个你可以把图片理解成为二维数组来玩

pip install opencv-python

代码:

import cv2
def cut_img(old_path,new_path,area):
    img = cv2.imread(old_path)
    cropped = img[area[1]:area[3],area[0]:area[2]]
    cv2.imwrite(new_path,cropped)
View Code

2.使用pillow来切割,这个是封装起来了,直接传参坐标

pip install pillow

代码:

from PIL import Image
def cut_img(old_path,new_path,area):
    img = Image.open(old_path)
    cropped = img.crop(area)
    cropped.save(new_path)
cut_image("E://0.png","E://0_new.png",(0,0,400,400))
View Code

 

------------------------------------------------------------------------------

以上代码未测试......

posted @ 2021-10-30 22:53    阅读(364)  评论(0编辑  收藏  举报