簡體   English   中英

如何在python中解析此ndjson文件?

[英]How do I parse this ndjson file in python?

我從最近開放源代碼的Google Quick draw數據集中獲得了這個ndjson文件。 我正在嘗試根據此坐標制作png圖像。 我需要在python中解析此ndjson文件

[{'key_id': 5344373355053056, 'image': [((2, 38, 71, 108, 128, 139), (10, 24, 31, 22, 14, 4)), ((0, 13, 13, 3, 14, 50, 74, 143), (20, 70, 98, 124, 122, 103, 96, 95)), ((134, 133, 144, 144), (0, 84, 207, 253)), ((131, 148, 154, 153, 176, 176, 171, 154), (8, 4, 6, 79, 236, 249, 253, 255))], 'recognized': 1, 'countrycode': 'KR', 'timestamp': 1490164248}, {'key_id': 6545061917491200, 'image': [((54, 37, 7, 5, 12, 0, 7, 34, 52, 56, 52, 42, 38), (66, 49, 8, 11, 63, 105, 105, 89, 84, 88, 149, 205, 255)), ((59, 62, 88, 95, 97), (66, 13, 1, 1, 33))], 'recognized': 1, 'countrycode': 'SE', 'timestamp': 1485553794}, {'key_id': 4767264018530304, 'image': [((146, 145, 154, 154, 197, 196, 189, 191, 178, 172, 148), (83, 121, 194, 254, 254, 228, 194, 143, 80, 78, 82)), ((176, 169, 153, 57, 18, 0, 97, 158), (75, 0, 12, 35, 55, 60, 68, 82)), ((0, 18, 28, 69, 151), (62, 93, 102, 100, 87))], 'recognized': 0, 'countrycode': 'US', 'timestamp': 1489097582}, {'key_id': 5711061690875904, 'image': [((0, 8, 48, 69, 102, 118, 136, 142, 142, 135, 108, 70, 60, 59), (253, 230, 145, 108, 72, 62, 59, 66, 88, 110, 172, 243, 255, 252)), ((96, 95, 105, 135, 117, 98, 115, 135), (83, 86, 96, 103, 98, 84, 98, 103)), ((138, 137, 124, 136, 138, 140), (71, 65, 79, 73, 63, 66)), ((65, 88, 237, 226, 196, 190, 170, 137), (11, 12, 63, 99, 158, 161, 159, 144)), ((79, 55, 16, 8, 5, 5, 11, 32, 71), (20, 8, 0, 3, 14, 58, 76, 99, 117))], 'recognized': 1, 'countrycode': 'DE', 'timestamp': 1490298672}, {'key_id': 6276192535576576, 'image': [((25, 19, 4, 0, 22, 49, 59, 63, 66, 62, 59, 52, 39, 24), (26, 82, 161, 250, 255, 254, 250, 234, 84, 30, 23, 17, 17, 23)), ((58, 72, 126, 208, 210, 206, 194, 153, 91, 81), (16, 19, 19, 9, 13, 113, 117, 113, 113, 116)), ((72, 82, 105, 155, 192, 209, 221, 221, 210, 207, 202, 125, 105, 74, 27), (117, 110, 104, 104, 114, 124, 137, 118, 65, 9, 7, 23, 23, 16, 0))], 'recognized': 1, 'countrycode': 'US', 'timestamp': 1485524332}]

用這個:

def create_image(image, filename):
    img = Image.new('RGB', (256,256), "white")
    pixels = img.load()

    x = -1
    y = -1

    for stroke in image:
        for i in range(len(stroke[0])):
            if x != -1: 
                for point in get_line(stroke[0][i], stroke[1][i], x, y):
                    pixels[point[0],point[1]] = (0, 0, 0)
            pixels[stroke[0][i],stroke[1][i]] = (0, 0, 0)
            x = stroke[0][i]
            y = stroke[1][i]
        x = -1
        y = -1
    img.save(filename)

def get_line(x1, y1, x2, y2):
    points = []
    issteep = abs(y2-y1) > abs(x2-x1)
    if issteep:
        x1, y1 = y1, x1
        x2, y2 = y2, x2
    rev = False
    if x1 > x2:
        x1, x2 = x2, x1
        y1, y2 = y2, y1
        rev = True
    deltax = x2 - x1
    deltay = abs(y2-y1)
    error = int(deltax / 2)
    y = y1
    ystep = None
    if y1 < y2:
        ystep = 1
    else:
        ystep = -1
    for x in range(x1, x2 + 1):
        if issteep:
            points.append((y, x))
        else:
            points.append((x, y))
        error -= deltay
        if error < 0:
            y += ystep
            error += deltax
    # Reverse the list if the coordinates were reversed
    if rev:
        points.reverse()
    return points   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM