i want get
pprint(data['shapes'][0]['points'][0][0])
to
pprint(data['shapes'][i]['points'][0][0])
cause i want get every point(from json file) about rectangle that i can crop 32x32 size little rectangle jpg but in my code ,i just get one number how to plus "for" + "with" to get all poit?
my json format
<pre>
{
"flags": {},
"imagePath": "035653_20141227_CT_8038_27_09.png",
"lineColor": null,
"shapes": [
{
"points": [
[
322,
208
],
[
354,
208
],
[
354,
240
],
[
322,
240
]
],
"fill_color": null,
"label": "hematoma_with_brain",
"line_color": null
},
{
"points": [
[
347,
185
],
[
379,
185
],
[
379,
217
],
[
347,
217
]
],
"fill_color": null,
"label": "hematoma_with_brain",
"line_color": null
},
{
"points": [
[
366,
226
],
[
398,
226
],
[
398,
258
],
[
366,
258
]
],
"fill_color": null,
"label": "hematoma_with_brain",
"line_color": null
},
{
"points": [
[
354,
264
],
[
386,
264
],
[
386,
296
],
[
354,
296
]
],
"fill_color": null,
"label": "hematoma_with_brain",
"line_color": null
},
{
"points": [
[
321,
249
],
[
353,
249
],
[
353,
281
],
[
321,
281
]
],
"fill_color": null,
"label": "hematoma_with_brain",
"line_color": null
}
],
"fillColor": null,
"imageData": null
}
</pre>
my code (cut by poit part not complete)
# -*- coding:utf-8 -*-
from PIL import Image
from glob import glob
from os import getcwd,chdir
import json
from pprint import pprint
# get file from directory
def list_files(directory, extension):
saved = getcwd()
chdir(directory)
it = glob('*.' + extension)
chdir(saved)
for imgName in it:
filename=imgName[:-4]
filename="C:\\Users\\admin\\Desktop\\crop_image_source\\"+filename
print(filename)
with open(filename+'.json') as f:
data = json.load(f)
pprint(data['shapes'][0]['points'][0][0])
# cut(imgName,32,32)
# print(type(it))
# crop image
def cut(id,vx,vy):
# open image
name1 = "C:\\Users\\admin\\Desktop\\crop_image_source\\"+id
name2 = "C:\\Users\\admin\\Desktop\\crop_image_source\\" + id + "_crop.jpg"
im =Image.open(name1)
#offset
dx = 32
dy = 32
n = 1
#left top
x1 = 0
y1 = 0
x2 = vx
y2 = vy
#vertical
while x2 <= 512:
#horizontal
while y2 <= 512:
name3 = name2 + str(n) + ".jpg"
im2 = im.crop((y1, x1, y2, x2))
im2.save(name3)
y1 = y1 + dy
y2 = y1 + vy
n = n + 1
x1 = x1 + dx
x2 = x1 + vx
y1 = 0
y2 = vy
print("success cut done,get total crop image count:")
return n-1
if __name__=="__main__":
id = "1"
#crop size vx,vy
#big size
# res = cut(id,32,32)
#meddle size
#res = cut(id,120,120)
#small size
#res = cut(id,80,80)
# print(res)
source_directory="C:\\Users\\admin\\Desktop\\crop_image_source\\"
list_files(source_directory,'png')
And If i just want first point info ? like
for in range(0,len(list),step)
but now is
for in list
# -*- coding:utf-8 -*-
from PIL import Image
from glob import glob
from os import getcwd,chdir
import json
from pprint import pprint
def cut(id,vx,vy,x1,y1):
name1 = "C:\\Users\\admin\\Desktop\\crop_image_source\\"+id
name2 = "C:\\Users\\admin\\Desktop\\crop_image_source\\" + id + "_crop.jpg"
im =Image.open(name1)
n = 1
x2 = x1 + vx
y2 = y1 + vy
name3 = name2 + str(n) + ".jpg"
im2 = im.crop((x1,y1, x2, y2))
im2.save(name3)
def list_files(directory, extension):
saved = getcwd()
chdir(directory)
it = glob('*.' + extension)
chdir(saved)
for imgName in it:
filename=imgName[:-4]
filename="C:\\Users\\admin\\Desktop\\crop_image_source\\"+filename
with open(filename+'.json') as f:
data = json.load(f)
for shape in data['shapes']:
for point in shape['points']:
pprint(point[0])
pprint(point[1])
cut(imgName,32,32,point[0],point[1])
if __name__=="__main__":
source_directory="C:\\Users\\admin\\Desktop\\crop_image_source\\"
list_files(source_directory,'png')
You can use the following nested loops to iterate over shapes and over the points within.
Change:
pprint(data['shapes'][0]['points'][0][0])
to:
for shape in data['shapes']:
for point in shape['points']:
pprint(point)