0

我从最近开源的 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}]
4

1 回答 1

0

用这个:

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   
于 2017-07-02T13:21:02.710 回答