2017-05-24 2 views
0

최근 오픈 소스 인 Google Quick Draw 데이터 세트에서이 ndjson 파일을 받았습니다. 이 코디네이트에서 PNG 이미지를 만들려고합니다. 나는어떻게이 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}] 
+0

당신은 당신이 지금까지 시도했다 우리에게 보여 주시겠습니까? 당신의 구체적인 문제는 무엇입니까? – crizzis

+0

확실히 @crizzis, 이것은 내가 시도한 코드이다. 파이썬은 저에게 새로운 것입니다. 나는 ndjson 값을 그리거나 파싱하는 데 사용할 수있는 인기있는 모듈을 많이 모른다. 내 문제와 코드 공유. 링크 : https://pastebin.com/cSvpv4Ej 내가 파이썬에서 질문에 붙여 넣은 ndjson 파일을 구문 분석해야합니다. 위에 내가 시도한 접근 방식입니다. –

답변

0

사용이 파이썬이 ndjson 파일을 구문 분석해야합니다

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