{
  "id": 332681,
  "title": "Working with TIFF files",
  "url": "/competitions/hubmap-organ-segmentation/discussion/332681",
  "author_name": "",
  "post_date": "2022-06-22T21:45:03.488872300Z",
  "votes": 19,
  "comment_count": 5,
  "views": 0,
  "content": "<p><a href=\"https://en.wikipedia.org/wiki/TIFF\" target=\"_blank\">TIFF</a> (short for Tag Image File Format and sometimes shortened as TIF) is a file format used a lot in <strong>medical imaging</strong> (probably because it is the output of many scanning devices?).</p>\n<p>Here is a code snippet to read the competition's files into a <a href=\"https://pytorch.org/\" target=\"_blank\">PyTorch</a> tensor using the <br>\ngreat <a href=\"https://pypi.org/project/rasterio/\" target=\"_blank\">rasterio</a> library:</p>\n<pre><code>from torchvision.transforms import ToTensor\nimport rasterio\n\n\npath = \"/kaggle/input/hubmap-organ-segmentation/train_images/28189.tiff\"\n\nwith rasterio.open(path) as image:\n    image_array = image.read()\n\n\ntorch_image = ToTensor()(image_array)\n\nprint(torch_image.shape)\n# Should get: torch.Size([3000, 3, 3000])\n</code></pre>\n<p>You can do the same using <a href=\"https://pillow.readthedocs.io/en/stable/\" target=\"_blank\">PIL</a> of course:</p>\n<pre><code>from PIL import Image\npil_image = Image.open(path)\ntorch_image = ToTensor()(pil_image)\nprint(torch_image.shape)\n</code></pre>\n<p>You can also plot the image:</p>\n<pre><code>from rasterio.plot import show\nshow(image_array)\n\n# If you want some colors, you can use:\n# ds = rasterio.open(path)\n# show((ds, 1), cmap='Reds')\n</code></pre>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F172860%2Fb71a66419ca33e4f906bc4a96e5828b6%2Ftiff_sample.jpg?generation=1655934266138345&amp;alt=media\" alt=\"sample_tiff\"></p>\n<p>You can find some more details in the following <a href=\"https://www.kaggle.com/code/yassinealouini/working-with-tiff-files?scriptVersionId=99074068\" target=\"_blank\">notebook</a> (I will add more details later).</p>\n<p>Here is also a GIS <a href=\"https://automating-gis-processes.github.io/CSC18/lessons/L6/reading-raster.html\" target=\"_blank\">guide</a> that shows a lot of rasterio commands. I am still exploring it, so I will add more later.</p>\n<p>Hope you find this useful, enjoy this competition!</p>",
  "messages": [
    {
      "id": "1829727",
      "postDate": "06/22/2022 21:45:03",
      "content": "<p><a href=\"https://en.wikipedia.org/wiki/TIFF\" target=\"_blank\">TIFF</a> (short for Tag Image File Format and sometimes shortened as TIF) is a file format used a lot in <strong>medical imaging</strong> (probably because it is the output of many scanning devices?).</p>\n<p>Here is a code snippet to read the competition's files into a <a href=\"https://pytorch.org/\" target=\"_blank\">PyTorch</a> tensor using the <br>\ngreat <a href=\"https://pypi.org/project/rasterio/\" target=\"_blank\">rasterio</a> library:</p>\n<pre><code>from torchvision.transforms import ToTensor\nimport rasterio\n\n\npath = \"/kaggle/input/hubmap-organ-segmentation/train_images/28189.tiff\"\n\nwith rasterio.open(path) as image:\n    image_array = image.read()\n\n\ntorch_image = ToTensor()(image_array)\n\nprint(torch_image.shape)\n# Should get: torch.Size([3000, 3, 3000])\n</code></pre>\n<p>You can do the same using <a href=\"https://pillow.readthedocs.io/en/stable/\" target=\"_blank\">PIL</a> of course:</p>\n<pre><code>from PIL import Image\npil_image = Image.open(path)\ntorch_image = ToTensor()(pil_image)\nprint(torch_image.shape)\n</code></pre>\n<p>You can also plot the image:</p>\n<pre><code>from rasterio.plot import show\nshow(image_array)\n\n# If you want some colors, you can use:\n# ds = rasterio.open(path)\n# show((ds, 1), cmap='Reds')\n</code></pre>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F172860%2Fb71a66419ca33e4f906bc4a96e5828b6%2Ftiff_sample.jpg?generation=1655934266138345&amp;alt=media\" alt=\"sample_tiff\"></p>\n<p>You can find some more details in the following <a href=\"https://www.kaggle.com/code/yassinealouini/working-with-tiff-files?scriptVersionId=99074068\" target=\"_blank\">notebook</a> (I will add more details later).</p>\n<p>Here is also a GIS <a href=\"https://automating-gis-processes.github.io/CSC18/lessons/L6/reading-raster.html\" target=\"_blank\">guide</a> that shows a lot of rasterio commands. I am still exploring it, so I will add more later.</p>\n<p>Hope you find this useful, enjoy this competition!</p>",
      "rawMarkdown": "[TIFF](https://en.wikipedia.org/wiki/TIFF) (short for Tag Image File Format and sometimes shortened as TIF) is a file format used a lot in **medical imaging** (probably because it is the output of many scanning devices?).\n\nHere is a code snippet to read the competition's files into a [PyTorch](https://pytorch.org/) tensor using the \ngreat [rasterio](https://pypi.org/project/rasterio/) library:\n\n\n```\nfrom torchvision.transforms import ToTensor\nimport rasterio\n\n\npath = \"/kaggle/input/hubmap-organ-segmentation/train_images/28189.tiff\"\n\nwith rasterio.open(path) as image:\n    image_array = image.read()\n\n\ntorch_image = ToTensor()(image_array)\n\nprint(torch_image.shape)\n# Should get: torch.Size([3000, 3, 3000])\n```\n\nYou can do the same using [PIL](https://pillow.readthedocs.io/en/stable/) of course:\n\n```\nfrom PIL import Image\npil_image = Image.open(path)\ntorch_image = ToTensor()(pil_image)\nprint(torch_image.shape)\n\n\n```\n\n\n\nYou can also plot the image:\n\n\n```\nfrom rasterio.plot import show\nshow(image_array)\n\n# If you want some colors, you can use:\n# ds = rasterio.open(path)\n# show((ds, 1), cmap='Reds')\n```\n\n![sample_tiff](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F172860%2Fb71a66419ca33e4f906bc4a96e5828b6%2Ftiff_sample.jpg?generation=1655934266138345&alt=media)\n\n\n\n\nYou can find some more details in the following [notebook](https://www.kaggle.com/code/yassinealouini/working-with-tiff-files?scriptVersionId=99074068) (I will add more details later).\n\nHere is also a GIS [guide](https://automating-gis-processes.github.io/CSC18/lessons/L6/reading-raster.html) that shows a lot of rasterio commands. I am still exploring it, so I will add more later.\n\nHope you find this useful, enjoy this competition!",
      "votes": null
    },
    {
      "id": "1835518",
      "postDate": "06/27/2022 19:45:20",
      "content": "<p>Notice that you can also use the <a href=\"https://pypi.org/project/tifffile/\" target=\"_blank\">https://pypi.org/project/tifffile/</a> library.</p>\n<p>Here is how to read an image:</p>\n<pre><code>import tifffile\n\ntifffile.imread(path)\n</code></pre>",
      "rawMarkdown": "Notice that you can also use the https://pypi.org/project/tifffile/ library.\n\nHere is how to read an image:\n\n```\nimport tifffile\n\ntifffile.imread(path)\n```",
      "votes": null
    },
    {
      "id": "1885843",
      "postDate": "08/05/2022 12:03:46",
      "content": "<p>One Hint: Using PIL library, the orientation tag is read out. This could lead to an orientation mismatch between annotation and the Image. If someone is using PIL, then please use this code snippet:</p>\n<pre><code>img = Image.open(PATH)\nimg = ImageOps.exif_transpose(img)\n</code></pre>",
      "rawMarkdown": "One Hint: Using PIL library, the orientation tag is read out. This could lead to an orientation mismatch between annotation and the Image. If someone is using PIL, then please use this code snippet:\n\n```\nimg = Image.open(PATH)\nimg = ImageOps.exif_transpose(img)\n```",
      "votes": null
    },
    {
      "id": "1885866",
      "postDate": "08/05/2022 12:09:19",
      "content": "<p>I'm actually trying to work with a Geojson file and I'm breaking my head</p>",
      "rawMarkdown": "I'm actually trying to work with a Geojson file and I'm breaking my head",
      "votes": null
    },
    {
      "id": "1885933",
      "postDate": "08/05/2022 12:47:08",
      "content": "<p><a href=\"https://www.kaggle.com/agustin222\" target=\"_blank\">@agustin222</a> What are you trying to do and what is your exact problem?</p>",
      "rawMarkdown": "agustin222 What are you trying to do and what is your exact problem?",
      "votes": null
    },
    {
      "id": "1892719",
      "postDate": "08/10/2022 09:27:48",
      "content": "<p>That's a good tip, thanks for sharing. 👍</p>",
      "rawMarkdown": "That's a good tip, thanks for sharing. 👍",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1835518,
      "author_name": "yassinealouini",
      "author_url": "",
      "post_date": "06/27/2022 19:45:20",
      "content": "<p>Notice that you can also use the <a href=\"https://pypi.org/project/tifffile/\" target=\"_blank\">https://pypi.org/project/tifffile/</a> library.</p>\n<p>Here is how to read an image:</p>\n<pre><code>import tifffile\n\ntifffile.imread(path)\n</code></pre>",
      "votes": null,
      "replies": []
    },
    {
      "id": 1885843,
      "author_name": "fabianhorst",
      "author_url": "",
      "post_date": "08/05/2022 12:03:46",
      "content": "<p>One Hint: Using PIL library, the orientation tag is read out. This could lead to an orientation mismatch between annotation and the Image. If someone is using PIL, then please use this code snippet:</p>\n<pre><code>img = Image.open(PATH)\nimg = ImageOps.exif_transpose(img)\n</code></pre>",
      "votes": null,
      "replies": [
        {
          "id": 1892719,
          "author_name": "yassinealouini",
          "author_url": "",
          "post_date": "08/10/2022 09:27:48",
          "content": "<p>That's a good tip, thanks for sharing. 👍</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 1885866,
      "author_name": "agustin222",
      "author_url": "",
      "post_date": "08/05/2022 12:09:19",
      "content": "<p>I'm actually trying to work with a Geojson file and I'm breaking my head</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 1885933,
      "author_name": "fabianhorst",
      "author_url": "",
      "post_date": "08/05/2022 12:47:08",
      "content": "<p><a href=\"https://www.kaggle.com/agustin222\" target=\"_blank\">@agustin222</a> What are you trying to do and what is your exact problem?</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1829727": "[TIFF](https://en.wikipedia.org/wiki/TIFF) (short for Tag Image File Format and sometimes shortened as TIF) is a file format used a lot in **medical imaging** (probably because it is the output of many scanning devices?).\n\nHere is a code snippet to read the competition's files into a [PyTorch](https://pytorch.org/) tensor using the \ngreat [rasterio](https://pypi.org/project/rasterio/) library:\n\n\n```\nfrom torchvision.transforms import ToTensor\nimport rasterio\n\n\npath = \"/kaggle/input/hubmap-organ-segmentation/train_images/28189.tiff\"\n\nwith rasterio.open(path) as image:\n    image_array = image.read()\n\n\ntorch_image = ToTensor()(image_array)\n\nprint(torch_image.shape)\n# Should get: torch.Size([3000, 3, 3000])\n```\n\nYou can do the same using [PIL](https://pillow.readthedocs.io/en/stable/) of course:\n\n```\nfrom PIL import Image\npil_image = Image.open(path)\ntorch_image = ToTensor()(pil_image)\nprint(torch_image.shape)\n\n\n```\n\n\n\nYou can also plot the image:\n\n\n```\nfrom rasterio.plot import show\nshow(image_array)\n\n# If you want some colors, you can use:\n# ds = rasterio.open(path)\n# show((ds, 1), cmap='Reds')\n```\n\n![sample_tiff](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F172860%2Fb71a66419ca33e4f906bc4a96e5828b6%2Ftiff_sample.jpg?generation=1655934266138345&alt=media)\n\n\n\n\nYou can find some more details in the following [notebook](https://www.kaggle.com/code/yassinealouini/working-with-tiff-files?scriptVersionId=99074068) (I will add more details later).\n\nHere is also a GIS [guide](https://automating-gis-processes.github.io/CSC18/lessons/L6/reading-raster.html) that shows a lot of rasterio commands. I am still exploring it, so I will add more later.\n\nHope you find this useful, enjoy this competition!",
    "1835518": "Notice that you can also use the https://pypi.org/project/tifffile/ library.\n\nHere is how to read an image:\n\n```\nimport tifffile\n\ntifffile.imread(path)\n```",
    "1885843": "One Hint: Using PIL library, the orientation tag is read out. This could lead to an orientation mismatch between annotation and the Image. If someone is using PIL, then please use this code snippet:\n\n```\nimg = Image.open(PATH)\nimg = ImageOps.exif_transpose(img)\n```",
    "1885866": "I'm actually trying to work with a Geojson file and I'm breaking my head",
    "1885933": "agustin222 What are you trying to do and what is your exact problem?",
    "1892719": "That's a good tip, thanks for sharing. 👍"
  },
  "source": "meta"
}