如何在Python中从图像中选择随机部分

Hol*_*ger 0 python image-processing

假设我有一张大图像(5000 x 5000),如何从这张大图像中随机选择一部分(200 x 200 正方形)?另外我想设置边界,这样选择就不会占据图像之外的任何区域。

如果有人有任何想法,请透露一些信息。

Amb*_*ber 5

import random

image_size = (5000,5000)
portion_size = (200, 200)

x1 = random.randint(0, image_size[0]-portion_size[0]-1)
y1 = random.randint(0, image_size[1]-portion_size[1]-1)

x2, y2 = x1+portion_size[0]-1, y1+portion_size[1]-1

# Grab the area of the image that is the rectangle defined by (x1,y1) and (x2,y2)
Run Code Online (Sandbox Code Playgroud)

最后一点的处理方式取决于您处理图像的方式。