{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":" <center>\n     <h1>View the images with gimp. (json to svg conversion) </h1>\n <center>","metadata":{"_uuid":"204ff3b5-8634-4771-bfbf-93635e588af7","_cell_guid":"51f70ba6-6485-4bfd-b931-a6b5efe1feec","trusted":true}},{"cell_type":"markdown","source":"It is useful if we can see the image data and annotation with image editor.</br>\nTo see the input image and annotation together with GIMP(https://www.gimp.org/), I wrote a script to convert json to svg. </br>\nGIMP can import paths data in svg format.","metadata":{"_uuid":"8c8d8e23-2ac7-4e9f-9788-08e7c715133a","_cell_guid":"a9b527ae-1a0d-43c0-9a82-dfdc20280df1","trusted":true}},{"cell_type":"markdown","source":"How to use\n1. Open the image(tiff) with gimp.\n2. In paths daialog, click right mouse button and select \"import path\"... and select the svg file. ","metadata":{"_uuid":"06f6fba9-0b3f-4604-98da-548adcf10aee","_cell_guid":"6c2b38e7-d8d9-4a93-98b1-402ab6dc177c","trusted":true}},{"cell_type":"markdown","source":"![](https://i.ibb.co/RhMG0ht/right-click.png)","metadata":{}},{"cell_type":"markdown","source":"3. Make the annotation paths visible by clicking left area of path icon","metadata":{}},{"cell_type":"markdown","source":"![](https://i.ibb.co/grGv7g8/visible.png)","metadata":{}},{"cell_type":"code","source":"import zipfile\nimport os\n\nannotation_dir = \"../input/hubmap-organ-segmentation/train_annotations/\"\ndst_dir = \"annotations_in_svg/\"\ncreate_zip_file = False","metadata":{"_uuid":"216f92b9-6e60-4a36-8085-8e4fcb361d17","_cell_guid":"92b56f12-2a9a-4ac6-852e-19a363bcc24b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-17T23:23:27.942961Z","iopub.execute_input":"2022-08-17T23:23:27.943353Z","iopub.status.idle":"2022-08-17T23:23:27.948722Z","shell.execute_reply.started":"2022-08-17T23:23:27.943323Z","shell.execute_reply":"2022-08-17T23:23:27.947702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def json2svg(fname):\n    header = '<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\\n\\\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 20010904//EN\"\\n\\\n              \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\\n\\n\\\n<svg xmlns=\"http://www.w3.org/2000/svg\">\\n\\\n  <path id=\"mask\"\\n\\\nfill=\"none\" stroke=\"black\" stroke-width=\"1\"\\n\\\n        d=\"'\n\n    footer = '\" />\\n\\</svg>\\n'\n\n    f = open(fname)\n    l = eval(f.read())\n    f.close\n\n    svg = header\n    for m in l:\n        first_point = True\n        for [x,y] in m:\n            if first_point:\n                svg += \"M  %d %d\"%(x,y)\n                first_point = False\n            else:\n                svg += \"L %d %d\"%(x,y)\n    svg += footer\n    return (svg)","metadata":{"_uuid":"92377015-4803-4528-a2b0-83528c786c0e","_cell_guid":"c02c5527-173d-450e-a5b8-99cbaaa58239","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-17T23:23:27.961373Z","iopub.execute_input":"2022-08-17T23:23:27.961745Z","iopub.status.idle":"2022-08-17T23:23:27.968863Z","shell.execute_reply.started":"2022-08-17T23:23:27.961713Z","shell.execute_reply":"2022-08-17T23:23:27.968059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfiles = os.listdir(annotation_dir)\n\nif create_zip_file:\n    with zipfile.ZipFile(dst_dir[:-1] + '.zip','w') as z:\n        for f in files:\n                if f.split('.')[1]=='json':\n                    svg = json2svg(annotation_dir + f)\n                    z.writestr(f.split('.')[0]+'.svg',svg)\nelse:\n    try:\n        os.mkdir(dst_dir)\n    except FileExistsError:\n        pass\n    for f in files:\n            if f.split('.')[1]=='json':\n                svg = json2svg(annotation_dir + f)\n                f = open(dst_dir +  f.split('.')[0]+'.svg','wt')\n                f.write(svg)\n                f.close()","metadata":{"_uuid":"94f3aad9-f9a3-4ba0-adca-aaf6bb10684e","_cell_guid":"04e55711-a1b1-4e80-9633-9045b12ad93d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-17T23:23:27.970805Z","iopub.execute_input":"2022-08-17T23:23:27.971181Z","iopub.status.idle":"2022-08-17T23:23:35.350172Z","shell.execute_reply.started":"2022-08-17T23:23:27.971150Z","shell.execute_reply":"2022-08-17T23:23:35.348979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"334b956f-5cc1-4785-8fad-4dc624e278ca","_cell_guid":"f5edc37e-8117-4534-b46e-4ecb12172444","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]}]}