简体   繁体   English

如何通过从负载JSON获取价值?

[英]how to get value via for from load json?

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? 因为我想得到有关矩形的每一个点(从json文件),所以我可以裁剪32x32大小的小矩形jpg,但是在我的代码中,我只是得到一个数字,如何加“ for”和“ with”来获得所有poit?

my json format 我的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> 

my code (cut by poit part not complete) 我的代码(按Poit部分剪切未完成)

# -*- 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) 在范围内(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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM