Hold_My_Anger
Hold_My_Anger

Reputation: 1015

How to select a random portion from an image in python

Suppose I have a large image (5000 x 5000), how can I randomly select a portion ( 200 x 200 square) from this large image? Also I want to set boundary so the selection won't take any area outside the image.

If anyone has any idea, please shed some light.

Upvotes: 0

Views: 1963

Answers (1)

Amber
Amber

Reputation: 526583

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)

How you do the last bit depends on how you're working with the image.

Upvotes: 5

Related Questions