Hi all,

I have an image 300x300. What I need to split into 100 regions or boxes. The code im using is below:

box = (a1,a2,a3,a4)
h8 = bill.crop(box)

The values of box I had as 0,0,30,30 for the first one. Next box as 30,30,60,60, next one as 60,60,90,90. But I think I have the values wrong.

I wanted it like this below:

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100

The output I am getting is showing the wrong information so I know I have the wrong values. Can anyone explain to me what the values should be?

Thanks in advance

Recommended Answers

All 4 Replies

I do not see connection of the rest of the text with the beginning of your description. If you have one value per pixel, then 300x300 = 90000 divide 10x10 = 100 boxes of 30x30 = 900. Pixel in coordinates x,y belongs to box x // 30, y // 30.

>>> def coords_to_box(x,y):
	return (x//x_box,y//y_box), (x % x_box, y % y_box)

>>> x_box, y_box = 30, 30
>>> coords_to_box(20,20)
((0, 0), (20, 20))
>>> coords_to_box(50,50)
((1, 1), (20, 20))
>>> coords_to_box(50,150)
((1, 5), (20, 0))

Thanks for the reply Tonyjv. I am trying to use the PIL crop method to split my image up into the 100 boxes of 30x30. My first box is:

box = (0,0,30,30)
im.crop(box)

So box 1 is in the top left hand corner and covers 900 pixels (30x30)

My 2nd box I want it to be 30 pixels along so the right of the 1st box. My code is:

box = (30,30,60,60)
im.crop(box)

So I understand that this is 30 pixels along and is 30x30. Or do I have the coords wrong? Should it be 30,30,30,30? I know its hard to explain but hopefully u know what I mean.

Sorry I have seemed to fix this.

The region is defined by a 4-tuple, where coordinates are (left, upper, right, lower).

I simply got the coords wrong.

boxes = tuple(((x,y),(x+30,y+30)) for x in range(0, 300,30)
	 for y in range(0, 300, 30))
>>> len(boxes)
100
>>> import pretty # my pretty printer snippet
>>> pretty.printer(boxes)

Here the boxes as pair of two tuples for clarity.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.