【Python】裁剪图片代码

【Python】裁剪图片代码


存一个自用裁剪图片代码
功能:固定像素中心裁剪、固定像素左上角裁剪(填充黑边)、图片一分为四

import os
from PIL import Image

def crop(input_img_path, output_img_path, crop_w, crop_h): #固定像素中心裁剪
    image = Image.open(input_img_path)

    x_max = image.size[0]
    y_max = image.size[1]
    mid_point_x = int(x_max / 2)
    mid_point_y = int(y_max / 2)

    right = mid_point_x + int(crop_w / 2)
    left = mid_point_x - int(crop_w / 2)
    down = mid_point_y + int(crop_h / 2)
    up = mid_point_y - int(crop_h / 2)

    BOX_LEFT, BOX_UP, BOX_RIGHT, BOX_DOWN = left, up, right, down
    box = (BOX_LEFT, BOX_UP, BOX_RIGHT, BOX_DOWN)
    crop_img = image.crop(box)
    crop_img.save(output_img_path)

def crop2(input_img_path, output_img_path, crop_w, crop_h, num): #左上角裁剪
    image = Image.open(input_img_path)

    x_max = image.size[0]
    y_max = image.size[1]

    if crop_w > x_max:  #超出范围填充黑边
        print(num, "pic width out of range!")
    if crop_h > y_max:  #超出范围填充黑边
        print(num, "pic height out of range!")

    up = 0  # up:上边裁剪起始位置
    left = 0  #left:左边裁剪起始位置
    down = int(crop_h)
    right = int(crop_w)

    BOX_LEFT, BOX_UP, BOX_RIGHT, BOX_DOWN = left, up, right, down
    box = (BOX_LEFT, BOX_UP, BOX_RIGHT, BOX_DOWN)
    crop_img = image.crop(box)
    crop_img.save(output_img_path)

def crop1_4(input_img_path, output_img_path):  #一分为四
    image = Image.open(input_img_path)
    height = image.size[1]
    width = image.size[0]
    # print("old image size (wxh)=", (width, height))

    img_1 = image.crop([0, 0, width / 2, height / 2])
    img_2 = image.crop([width / 2, 0, width,  height / 2])
    img_3 = image.crop([0, height / 2, width / 2, height])
    img_4 = image.crop([width / 2, height / 2, width, height])
    img1_path = output_img_path.replace('.jpg', '(1).jpg')
    img2_path = output_img_path.replace('.jpg', '(2).jpg')
    img3_path = output_img_path.replace('.jpg', '(3).jpg')
    img4_path = output_img_path.replace('.jpg', '(4).jpg')
    img_1.save(img1_path)
    img_2.save(img2_path)
    img_3.save(img3_path)
    img_4.save(img4_path)


if __name__ == '__main__':
    dataset_dir = ""  # 图片路径
    output_dir = ""  # 输出路径
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    num = 0       # 裁剪图片数
    crop_w = 300  # 裁剪图片宽
    crop_h = 300  # 裁剪图片高
    
    # 获得需要转化的图片路径并生成目标路径
    image_filenames = [(os.path.join(dataset_dir, x), os.path.join(output_dir, x))
                       for x in os.listdir(dataset_dir)]
    # for x in os.listdir(dataset_dir):
    #     print(x)
    
    # 转化所有图片
    for path in image_filenames:
        num = num + 1
        # crop(path[0], path[1], crop_w, crop_h)  #固定像素中心裁剪
        # crop2(path[0], path[1], crop_w, crop_h, num)  #左上角为起点裁剪
        crop1_4(path[0], path[1])  #一分为四

    print(num, "picture has done!")

互联网代码缝合怪,侵删

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,可以使用Pillow库或OpenCV库来裁剪图片。 使用Pillow库进行裁剪代码如下所示: ```python from PIL import Image img = Image.open("image.jpg") cropped = img.crop((x1, y1, x2, y2)) # (left, upper, right, lower) cropped.save("cropped_image.jpg") ``` 其中,`x1`和`y1`表示裁剪区域的左上角坐标,`x2`和`y2`表示裁剪区域的右下角坐标。 使用OpenCV库进行裁剪代码如下所示: ```python import cv2 img = cv2.imread("image.jpg") cropped = img\[y1:y2, x1:x2\] # 裁剪坐标为\[y1:y2, x1:x2\] cv2.imwrite("cropped_image.jpg", cropped) ``` 同样,`x1`和`y1`表示裁剪区域的左上角坐标,`x2`和`y2`表示裁剪区域的右下角坐标。 请注意,以上代码中的`image.jpg`是待裁剪图片路径,`cropped_image.jpg`是裁剪后保存的图片路径。你可以根据自己的需求修改这些路径。 #### 引用[.reference_title] - *1* *3* [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^control,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^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值