This repository was archived by the owner on Dec 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_img_cut.py
More file actions
32 lines (26 loc) · 1.42 KB
/
Copy pathtest_img_cut.py
File metadata and controls
32 lines (26 loc) · 1.42 KB
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
from PIL import Image
import math
import itertools
def make_chunks(image_path, num_crop_col = 2, num_crop_row = 2):
"""slice an image into parts """
img = Image.open(image_path)
width, height = img.size
# compute crop properties using image measure and the wanted number of pieces
width_crop_col = width / num_crop_col
width_crop_row = height / num_crop_row
chunks_numerotation = [(col,row) for col in range(num_crop_col) for row in range(num_crop_row) ]
# x and y represent Cartesian pixel coordinates.
# 0,0 is up left
# the norm between 2 ticks on horizontal x axis is width_crop_col
# the norm between 2 ticks on vertical y axis is width_crop_row
cut_col = [width_crop_col * e for e in range (num_crop_col +1)]
cut_row = [width_crop_row * e for e in range (num_crop_row +1)]
# +1 in order to have coord of rigth limit of the image
chunks_starting_coords = itertools.product(cut_col[:-1], cut_row[:-1])
chunks_ending_coords = itertools.product(cut_col[1:], cut_row[1:])
chunks_coords = zip (chunks_starting_coords, chunks_ending_coords)
for chunk_idx, chunk_coords in enumerate(chunks_coords) :
box = list(itertools.chain.from_iterable(chunk_coords)) #(left , upper , right , lower) # pixel coords of the chunk
print (chunk_idx, box)
new_chunk = img.crop(box)
new_chunk.save("out_{:d}_{:d}.jpg".format(*chunks_numerotation[chunk_idx]) )