情况:
有时候看视频,看到一个漂亮的妹子,按下 Alt + PrintScreen 进行截图之后,会把整个屏幕都截图。
 需要适当剪裁一下。 每次打开 PS , 也太慢了。 所以写个代码, 快速处理。
效果对比:
原始图片:
 
剪裁后:

代码
from PIL import Image
# 裁剪图片     5% 刚刚好!
def trim_image(image_path, output_path, percent=0.05):
    # 打开图片
    img = Image.open(image_path)
    # 获取图片的宽度和高度
    width, height = img.size
    # 计算需要裁剪的宽度和高度
    crop_width = int(width * percent)
    crop_height = int(height * percent)
    # 计算裁剪区域的坐标
    left = crop_width
    top = crop_height
    right = width - crop_width
    bottom = height - crop_height
    # 裁剪图片
    cropped_img = img.crop((left, top, right, bottom))
    # 保存裁剪后的图片
    cropped_img.save(output_path)
    print("done!")
# 使用示例
trim_image('a.jpg', 'a2.jpg')
 
Todo
可以尝试把这个功能重写为一个 GUI .



















