1

I would like to convert a JSON file back into a png image or a NumPy array. The file JSON file consists of a list of coordinates and other metadata. As an example. it would look like this :

 "firstEditDate": "2019-12-02T19:05:45.393Z",
 "lastEditDate": "2020-06-30T13:21:33.371Z",
 "folder": "/Pictures/poly",
 "objects": [
  {
   "classIndex": 5,
   "layer": 0,
   "polygon": [
    {
     "x": 0,
     "y": 0
    },
    {
     "x": 1699.7291626931146,
     "y": 0
    },
    {
     "x": 1699.7291626931146,
     "y": 1066.87392714095
    },
    {
     "x": 0,
     "y": 1066.87392714095
    }
   ],
  },
  {
   "classIndex": 2,
   "layer": 0,
   "polygon": [
    {
     "x": 844.2300556586271,
     "y": 711.8243676199173
    },
    {
     "x": 851.156462585034,
     "y": 740.5194820293175
    },
    {
     "x": 854.1249226963513,
     "y": 744.477428844407
    },
    {
     "x": 854.1249226963513,
     "y": 747.4458889557243
    },

 

(coordinates should be rounded to the nearest ones prior to creating the array or image)

The dimension of the array/ picture should be 1727 x 971

Is there any function in python that can convert the file either into an array with the value inside the array of the ClassIndex? Or into a picture where each ClassIndex is assigned to a specific color?

Jolanthan
  • 81
  • 7
  • 1
    mahotas.polygon seems to be a solution to your problem. You can use it to draw polygons in a numpy array (see samplebias's answer in https://stackoverflow.com/questions/5587839/drawing-polygons-in-numpy-arrays) – bousof Jun 30 '20 at 14:07

1 Answers1

1

Here is a solution:

import matplotlib.pyplot as plt
import numpy as np
import mahotas.polygon as mp

json_dict = {
  "firstEditDate": "2019-12-02T19:05:45.393Z",
  "lastEditDate": "2020-06-30T13:21:33.371Z",
  "folder": "/Pictures/poly",
  "objects": [{
    "classIndex": 1,
    "layer": 0,
    "polygon": [
      {"x": 170, "y": 674},
      {"x": 70, "y": 674},
      {"x": 70, "y": 1120},
      {"x": 870, "y": 1120},
      {"x": 870, "y": 674},
      {"x": 770, "y": 674},
      {"x": 770, "y": 1020},
      {"x": 170, "y": 1020},
    ],
  }, {
    "classIndex": 2,
    "layer": 0,
    "polygon": [
      {"x": 220, "y": 870},
      {"x": 220, "y": 970},
      {"x": 720, "y": 970},
      {"x": 720, "y": 870},
    ]
  }, {
    "classIndex": 3,
    "layer": 0,
    "polygon": [
      {"x": 250, "y": 615},
      {"x": 225, "y": 710},
      {"x": 705, "y": 840},
      {"x": 730, "y": 745},
    ]
  }, {
    "classIndex": 4,
    "layer": 0,
    "polygon": [
      {"x": 350, "y": 380},
      {"x": 300, "y": 465},
      {"x": 730, "y": 710},
      {"x": 780, "y": 630},
    ]
  }, {
    "classIndex": 5,
    "layer": 0,
    "polygon": [
      {"x": 505, "y": 180},
      {"x": 435, "y": 250},
      {"x": 790, "y": 605},
      {"x": 855, "y": 535},
    ]
  }, {
    "classIndex": 6,
    "layer": 0,
    "polygon": [
      {"x": 700, "y": 30},
      {"x": 615, "y": 80},
      {"x": 870, "y": 515},
      {"x": 950, "y": 465},
    ]
  }]
}

canvas = np.zeros((1000,1150))
for obj in json_dict["objects"]:
  pts = [(round(p["x"]),round(p["y"])) for p in obj["polygon"]]
  mp.fill_polygon(pts, canvas, obj["classIndex"])
plt.imshow(canvas.transpose())
plt.colorbar()
plt.show()

Output:

enter image description here

bousof
  • 1,241
  • 11
  • 13
  • Thank you for your help! Just went through the mahotas library. Thanks a lot. I got one problem when I was using the code for my puprose.Whenn I set my canvas to an array with the shape (1727,971) I get an Index error for `mp.fill_polygons` `IndexError: index 1728 is out of bounds for axis 0 with size 1728 ` would you know what the problem is ? – Jolanthan Jun 30 '20 at 17:29