python 裁剪图片

情况:

有时候看视频,看到一个漂亮的妹子,按下 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 .

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值