Python randomly cut image

Preface

Today I was working on a litchi orchard canopy target detection data set, and I felt that the area of ​​the spliced ​​pictures captured by the drone was too large. Try using Python to do random cutting of the image to get more data. First implement a simple center point cutting.

Ideas

1. Use image.size of the PIL library to obtain the width and height of the image.

2. Obtain the pixel coordinates of the image center point mid_point_x, mid_point_y.

3. Construct the areas of interest BOX_LEFT, BOX_UP, BOX_RIGHT, and BOX_DOWN.

4. Use image.crop of the PIL library to cut. Save the image.

Source code

from PIL import Image
import os
import random


# 定义待批量裁剪图像的路径地址
IMAGE_INPUT_PATH = 'C:\\Users\\27651\Desktop\crops_canopy\Lab_me\img_jpg'
# 定义裁剪后的图像存放地址
IMAGE_OUTPUT_PATH = 'C:\\Users\\27651\Desktop\crops_canopy\Lab_me\img_crop'

for each_image in os.listdir(IMAGE_INPUT_PATH):
# 每个图像全路径
    image_input_fullname = IMAGE_INPUT_PATH + "/" + each_image
# PIL库打开每一张图像
    img = Image.open(image_input_fullname)
# 定义裁剪图片左、上、右、下的像素坐标
    x_max = img.size[0]
    y_max = img.size[1]
    
    mid_point_x = int(x_max/2)
    mid_point_y = int(y_max/2)

    down = mid_point_y+random.randint(0,mid_point_y)
    up = mid_point_y-(down-mid_point_y)
    right = mid_point_x+(down-mid_point_y)
    left = mid_point_x-(down-mid_point_y)

    BOX_LEFT, BOX_UP, BOX_RIGHT, BOX_DOWN = left,up,right,down
# 从原始图像返回一个矩形区域,区域是一个4元组定义左上右下像素坐标
    box = (BOX_LEFT, BOX_UP, BOX_RIGHT , BOX_DOWN )
# 进行roi裁剪
    roi_area = img.crop(box)
# 裁剪后每个图像的路径+名称
    image_output_fullname = IMAGE_OUTPUT_PATH + "/" + each_image
# 存储裁剪得到的图像
    roi_area.save(image_output_fullname)
    print('{0} crop done.'.format(each_image))

Effect

Original picture

 After cutting

Summarize

implemented a simple square cutting based on the center point of the image, and then further optimized the "center point" to be randomly movedand (such as determining the range from the upper, lower, left and right boundaries to the "center point"). Enhance the constraints on the boundary of the area of ​​interest

Updated on 2021/11/18

Improved the random movement of the center point and multiple rounds of random cutting output. Modify the code as follows

for i in range(0,5):
        crop_x = mid_point_x+random.randint(int(-mid_point_x/5),int(mid_point_x/5))
        crop_y = mid_point_y+random.randint(int(-mid_point_y/5),int(mid_point_y/5)) #中心点移动
        
        dis_x = x_max-crop_x
        dis_y = y_max-crop_y 

        dis_min = min(dis_x,dis_y,crop_x,crop_y)#获取变动范围

        down = crop_y+random.randint(0,dis_min)
        up = crop_y-(down-crop_y)
        right = crop_x+(down-crop_y)
        left = crop_x-(down-crop_y)

        BOX_LEFT, BOX_UP, BOX_RIGHT, BOX_DOWN = left,up,right,down
# 从原始图像返回一个矩形区域,区域是一个4元组定义左上右下像素坐标
        box = (BOX_LEFT, BOX_UP, BOX_RIGHT , BOX_DOWN )
# 进行roi裁剪
        roi_area = img.crop(box)
# 裁剪后每个图像的路径+名称
        image_output_fullname = IMAGE_OUTPUT_PATH + "/" + str(i) + "_" + each_image
# 存储裁剪得到的图像
        roi_area.save(image_output_fullname)
        print('{0} crop done.'.format(each_image))

Effect

 

Guess you like

Origin blog.csdn.net/weixin_47666981/article/details/121358252