如何通过for from load json获取价值?
how to get value via for from load json?
我要
pprint(data['shapes'][0]['points'][0][0])
到
pprint(data['shapes'][i]['points'][0][0])
因为我想获取关于矩形的每个点(来自 json 文件)
我可以裁剪 32x32 大小的小矩形
jpg
但在我的代码中,我只得到一个数字
如何加上 "for" + "with" 得到所有点?
我的json格式
<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>
我的代码(删减部分不完整)
# -*- 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')
如果我只想要第一点信息?
喜欢
在范围内(0,len(列表),步骤)
但现在是
在列表中
# -*- 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')
您可以使用以下嵌套循环来迭代形状和其中的点。
变化:
pprint(data['shapes'][0]['points'][0][0])
至:
for shape in data['shapes']:
for point in shape['points']:
pprint(point)
我要
pprint(data['shapes'][0]['points'][0][0])
到
pprint(data['shapes'][i]['points'][0][0])
因为我想获取关于矩形的每个点(来自 json 文件) 我可以裁剪 32x32 大小的小矩形 jpg 但在我的代码中,我只得到一个数字 如何加上 "for" + "with" 得到所有点?
我的json格式
<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>
我的代码(删减部分不完整)
# -*- 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')
如果我只想要第一点信息?
在范围内(0,len(列表),步骤)
但现在是
在列表中
# -*- 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')
您可以使用以下嵌套循环来迭代形状和其中的点。
变化:
pprint(data['shapes'][0]['points'][0][0])
至:
for shape in data['shapes']:
for point in shape['points']:
pprint(point)