Skip to content

Commit 2dcb166

Browse files
Merge pull request claimed-framework#268 from creatermaker/main
This is the improved version of image-tiling.
2 parents 8a05abd + 3a1008f commit 2dcb166

File tree

4 files changed

+300
-0
lines changed

4 files changed

+300
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
cwlVersion: v1.2
2+
class: CommandLineTool
3+
4+
baseCommand: "claimed"
5+
6+
inputs:
7+
component:
8+
type: string
9+
default: docker.io/mdorzweiler/claimed-image-tiling:0.1
10+
inputBinding:
11+
position: 1
12+
prefix: --component
13+
log_level:
14+
type: string
15+
default: "INFO"
16+
inputBinding:
17+
position: 2
18+
prefix: --log_level
19+
directory_path:
20+
type: string
21+
default: None
22+
inputBinding:
23+
position: 3
24+
prefix: --directory_path
25+
destination:
26+
type: string
27+
default: None
28+
inputBinding:
29+
position: 4
30+
prefix: --destination
31+
tile_size_x:
32+
type: string
33+
default: None
34+
inputBinding:
35+
position: 5
36+
prefix: --tile_size_x
37+
tile_size_y:
38+
type: string
39+
default: None
40+
inputBinding:
41+
position: 6
42+
prefix: --tile_size_y
43+
stride_x:
44+
type: string
45+
default: None
46+
inputBinding:
47+
position: 7
48+
prefix: --stride_x
49+
stride_y:
50+
type: string
51+
default: None
52+
inputBinding:
53+
position: 8
54+
prefix: --stride_y
55+
56+
57+
outputs: []
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "2e8a0fac-adbd-4428-90ea-869aee2b95bf",
6+
"metadata": {},
7+
"source": [
8+
"## Xview Dataset clipping\n",
9+
"This is a component designed to clip the dataset provided by xview. "
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 3,
15+
"id": "c0421aaf-4da4-4a66-b626-e8962315afe8",
16+
"metadata": {},
17+
"outputs": [
18+
{
19+
"name": "stdout",
20+
"output_type": "stream",
21+
"text": [
22+
"Requirement already satisfied: Pillow in /Users/maxdorzweiler/anaconda3/lib/python3.11/site-packages (9.4.0)\n"
23+
]
24+
}
25+
],
26+
"source": [
27+
"!pip install Pillow"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 2,
33+
"id": "0b7ad533-dabc-488e-bf65-59a54a85f3f9",
34+
"metadata": {},
35+
"outputs": [],
36+
"source": [
37+
"\n",
38+
"import os\n",
39+
"from PIL import Image\n"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 4,
45+
"id": "c8df047b-6167-43f1-9a31-d836580180ac",
46+
"metadata": {},
47+
"outputs": [],
48+
"source": [
49+
"\n",
50+
"#directory_path is the path to the folder with the unzipped .tif images from xview dataset \n",
51+
"directory_path = os.environ.get(\"directory_path\")\n",
52+
"\n",
53+
"#destination is the path to the folder which saves all the extracted tiles. \n",
54+
"destination = os.environ.get(\"destination\")\n",
55+
"\n",
56+
"#Each image is cropped using a rectangular window with edge_length tile_size_x and tile_size_y which has to be given in number of pixels \n",
57+
"tile_size_x = os.environ.get(\"tile_size_x\")\n",
58+
"tile_size_y = os.environ.get(\"tile_size_y\")\n",
59+
"\n",
60+
"#stride_x is the length in pixels the sliding window is moved to the right after each step\n",
61+
"#stride_y is the length in pixels the sliding window is moved down after completing a row\n",
62+
"#For tumbling window stride_x must equal tile_size_x and stride_y must equal tile_size_y\n",
63+
"stride_x = os.environ.get(\"stride_x\")\n",
64+
"stride_y = os.environ.get(\"stride_y\")\n"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 5,
70+
"id": "8ed74228-937d-4d5d-b6d9-97026af3eb50",
71+
"metadata": {},
72+
"outputs": [],
73+
"source": [
74+
"\n",
75+
"def clear_directory(directory):\n",
76+
" for item in os.listdir(directory):\n",
77+
" item_path = os.path.join(directory, item)\n",
78+
" # Check if the item is a file\n",
79+
" if os.path.isfile(item_path):\n",
80+
" # If it's a file, remove it\n",
81+
" os.remove(item_path)\n",
82+
" # If it's a directory, recursively clear it\n",
83+
" elif os.path.isdir(item_path):\n",
84+
" clear_directory(item_path)\n",
85+
" os.rmdir(item_path)\n",
86+
" "
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 9,
92+
"id": "d6e89f94-490a-4c1a-80cd-f2ab30adb374",
93+
"metadata": {},
94+
"outputs": [],
95+
"source": [
96+
"\n",
97+
"def sliding_window(directory_path, destination, tile_size_x, tile_size_y, stride_x, stride_y):\n",
98+
" \n",
99+
" if os.path.isdir(destination):\n",
100+
" clear_directory(destination)\n",
101+
" else: \n",
102+
" os.makedirs(destination)\n",
103+
" \n",
104+
" \n",
105+
" for item in os.listdir(directory_path):\n",
106+
" if item == \".DS_Store\":\n",
107+
" continue\n",
108+
" item_path = os.path.join(directory_path, item)\n",
109+
" clipped_item = os.path.join(destination, item)\n",
110+
" os.makedirs(clipped_item)\n",
111+
" \n",
112+
" image = Image.open(item_path)\n",
113+
" width, height = image.size\n",
114+
" \n",
115+
" x_range = [0]\n",
116+
" while(x_range[-1] + stride_x + tile_size_x < width):\n",
117+
" x_range += [x_range[-1] + stride_x]\n",
118+
" \n",
119+
" y_range = [0]\n",
120+
" while(y_range[-1] + stride_y + tile_size_y < height):\n",
121+
" y_range += [y_range[-1] + stride_y]\n",
122+
" \n",
123+
" counter = 0\n",
124+
" for x in x_range:\n",
125+
" for y in y_range:\n",
126+
" cropped = image.crop((x,y, x+tile_size_x, y+tile_size_y))\n",
127+
"\n",
128+
" file_name = \"{}\".format(y)\n",
129+
" file_name += \"_{}\".format(x) + \".tif\"\n",
130+
" path = os.path.join(clipped_item, file_name)\n",
131+
" \n",
132+
" cropped.save(path)\n",
133+
" \n"
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": 10,
139+
"id": "ce60bf47-7235-43a1-a4f7-5b81080d617d",
140+
"metadata": {},
141+
"outputs": [
142+
{
143+
"ename": "TypeError",
144+
"evalue": "stat: path should be string, bytes, os.PathLike or integer, not NoneType",
145+
"output_type": "error",
146+
"traceback": [
147+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
148+
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
149+
"Cell \u001b[0;32mIn[10], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m sliding_window(directory_path, destination, tile_size_x, tile_size_y, stride_x, stride_y)\n",
150+
"Cell \u001b[0;32mIn[9], line 3\u001b[0m, in \u001b[0;36msliding_window\u001b[0;34m(directory_path, destination, tile_size_x, tile_size_y, stride_x, stride_y)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21msliding_window\u001b[39m(directory_path, destination, tile_size_x, tile_size_y, stride_x, stride_y):\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39misdir(destination):\n\u001b[1;32m 4\u001b[0m clear_directory(destination)\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m: \n",
151+
"File \u001b[0;32m<frozen genericpath>:42\u001b[0m, in \u001b[0;36misdir\u001b[0;34m(s)\u001b[0m\n",
152+
"\u001b[0;31mTypeError\u001b[0m: stat: path should be string, bytes, os.PathLike or integer, not NoneType"
153+
]
154+
}
155+
],
156+
"source": [
157+
"sliding_window(directory_path, destination, tile_size_x, tile_size_y, stride_x, stride_y)"
158+
]
159+
}
160+
],
161+
"metadata": {
162+
"kernelspec": {
163+
"display_name": "Python 3 (ipykernel)",
164+
"language": "python",
165+
"name": "python3"
166+
},
167+
"language_info": {
168+
"codemirror_mode": {
169+
"name": "ipython",
170+
"version": 3
171+
},
172+
"file_extension": ".py",
173+
"mimetype": "text/x-python",
174+
"name": "python",
175+
"nbconvert_exporter": "python",
176+
"pygments_lexer": "ipython3",
177+
"version": "3.11.9"
178+
}
179+
},
180+
"nbformat": 4,
181+
"nbformat_minor": 5
182+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apiVersion: batch/v1
2+
kind: Job
3+
metadata:
4+
name: image-tiling
5+
spec:
6+
template:
7+
spec:
8+
containers:
9+
- name: image-tiling
10+
image: docker.io/mdorzweiler/claimed-image-tiling:0.1
11+
workingDir: /opt/app-root/src/
12+
command: ["/opt/app-root/bin/ipython","claimed_image-tiling.ipynb"]
13+
env:
14+
- name: log_level
15+
value: value_of_log_level
16+
- name: directory_path
17+
value: value_of_directory_path
18+
- name: destination
19+
value: value_of_destination
20+
- name: tile_size_x
21+
value: value_of_tile_size_x
22+
- name: tile_size_y
23+
value: value_of_tile_size_y
24+
- name: stride_x
25+
value: value_of_stride_x
26+
- name: stride_y
27+
value: value_of_stride_y
28+
restartPolicy: OnFailure
29+
imagePullSecrets:
30+
- name: image_pull_secret
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: image-tiling
2+
description: "## Xview Dataset clipping – CLAIMED V0.1"
3+
4+
inputs:
5+
- {name: log_level, type: String, description: "update log level", default: "INFO"}
6+
- {name: directory_path, type: String, description: "directory_path is the path to the folder with the unzipped .tif images from xview dataset"}
7+
- {name: destination, type: String, description: "destination is the path to the folder which saves all the extracted tiles."}
8+
- {name: tile_size_x, type: String, description: "Each image is cropped using a rectangular window with edge_length tile_size_x and tile_size_y which has to be given in number of pixels"}
9+
- {name: tile_size_y, type: String, description: ""}
10+
- {name: stride_x, type: String, description: "For tumbling window stride_x must equal tile_size_x and stride_y must equal tile_size_y"}
11+
- {name: stride_y, type: String, description: ""}
12+
13+
14+
outputs:
15+
16+
17+
implementation:
18+
container:
19+
image: docker.io/mdorzweiler/claimed-image-tiling:0.1
20+
command:
21+
- sh
22+
- -ec
23+
- |
24+
ipython ./claimed_image-tiling.ipynb log_level="${0}" directory_path="${1}" destination="${2}" tile_size_x="${3}" tile_size_y="${4}" stride_x="${5}" stride_y="${6}"
25+
- {inputValue: log_level}
26+
- {inputValue: directory_path}
27+
- {inputValue: destination}
28+
- {inputValue: tile_size_x}
29+
- {inputValue: tile_size_y}
30+
- {inputValue: stride_x}
31+
- {inputValue: stride_y}

0 commit comments

Comments
 (0)