天宫鹤

Python以边沿裁剪图片

 1 # 以边沿裁剪图片
 2 def crop_image_by_border(image, l_mm=0, t_mm=0, r_mm=0, b_mm=0):
 3     # 计算新的宽度和高度(像素单位)
 4     l_dot = int(l_mm / inch_to_mm * dpi)  # 左边
 5     t_dot = int(t_mm / inch_to_mm * dpi)  # 上边
 6     r_dot = int(r_mm / inch_to_mm * dpi)  # 右边
 7     b_dot = int(b_mm / inch_to_mm * dpi)  # 下边
 8 
 9     width, height = image.size
10     left, upper = l_dot, t_dot
11     right, lower = width - l_dot - r_dot, height - t_dot - b_dot
12     image = image.crop(box=(left, upper, right, lower))
13 
14     return image

 

posted on 2024-07-06 17:14  GoGrid  阅读(1)  评论(0编辑  收藏  举报

导航