{
  "id": 162633,
  "title": "Displaying image files",
  "url": "/competitions/prostate-cancer-grade-assessment/discussion/162633",
  "author_name": "paulreiners",
  "post_date": "2020-06-29T15:18:54.654000",
  "votes": 1,
  "comment_count": 1,
  "views": 0,
  "content": "<p>I have the following code for displaying some image files:</p>\n\n<p>```\nimport cv2</p>\n\n<h1>Extract numpy values from Image column in data frame</h1>\n\n<p>images = train_df['image_id'].values</p>\n\n<h1>Extract 9 random images from it</h1>\n\n<p>random_images = [np.random.choice(images) for i in range(9)]</p>\n\n<h1>Location of the image dir</h1>\n\n<p>img_dir = '/kaggle/input/prostate-cancer-grade-assessment/train_images/'</p>\n\n<p>print('Display Random Images')</p>\n\n<h1>Adjust the size of your images</h1>\n\n<p>plt.figure(figsize=(20,10))</p>\n\n<h1>Iterate and plot random images</h1>\n\n<p>for i in range(9):\n    plt.subplot(3, 3, i + 1)\n    image_file = os.path.join(img_dir, random_images[i] + \".tiff\")\n    img = cv2.imread(image_file, cv2.IMREAD_GRAYSCALE)\n    plt.imshow(img, cmap='gray')\n    plt.axis('off')</p>\n\n<h1>Adjust subplot parameters to give specified padding</h1>\n\n<p>plt.tight_layout()   </p>\n\n<p>```</p>\n\n<p>However, I get this exception:</p>\n\n<h2>```</h2>\n\n<p>TypeError                                 Traceback (most recent call last)\n in \n     20     image_file = os.path.join(img_dir, random_images[i] + \".tiff\")\n     21     img = cv2.imread(image_file, cv2.IMREAD_GRAYSCALE)\n---&gt; 22     plt.imshow(img, cmap='gray')\n     23     plt.axis('off')\n     24 </p>\n\n<p>/opt/conda/lib/python3.7/site-packages/matplotlib/pyplot.py in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, data, *<em>kwargs)\n   2649         filternorm=filternorm, filterrad=filterrad, imlim=imlim,\n   2650         resample=resample, url=url, *</em>({\"data\": data} if data is not\n-&gt; 2651         None else {}), **kwargs)\n   2652     sci(__ret)\n   2653     return __ret</p>\n\n<p>/opt/conda/lib/python3.7/site-packages/matplotlib/<strong>init</strong>.py in inner(ax, data, *args, **kwargs)\n   1563     def inner(ax, *args, data=None, **kwargs):\n   1564         if data is None:\n-&gt; 1565             return func(ax, *map(sanitize_sequence, args), **kwargs)\n   1566 \n   1567         bound = new_sig.bind(ax, *args, **kwargs)</p>\n\n<p>/opt/conda/lib/python3.7/site-packages/matplotlib/cbook/deprecation.py in wrapper(*args, **kwargs)\n    356                 f\"%(removal)s.  If any parameter follows {name!r}, they \"\n    357                 f\"should be pass as keyword, not positionally.\")\n--&gt; 358         return func(*args, **kwargs)\n    359 \n    360     return wrapper</p>\n\n<p>/opt/conda/lib/python3.7/site-packages/matplotlib/cbook/deprecation.py in wrapper(*args, **kwargs)\n    356                 f\"%(removal)s.  If any parameter follows {name!r}, they \"\n    357                 f\"should be pass as keyword, not positionally.\")\n--&gt; 358         return func(*args, **kwargs)\n    359 \n    360     return wrapper</p>\n\n<p>/opt/conda/lib/python3.7/site-packages/matplotlib/axes/_axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)\n   5613                               resample=resample, **kwargs)\n   5614 \n-&gt; 5615         im.set_data(X)\n   5616         im.set_alpha(alpha)\n   5617         if im.get_clip_path() is None:</p>\n\n<p>/opt/conda/lib/python3.7/site-packages/matplotlib/image.py in set_data(self, A)\n    692                 not np.can_cast(self._A.dtype, float, \"same_kind\")):\n    693             raise TypeError(\"Image data of dtype {} cannot be converted to \"\n--&gt; 694                             \"float\".format(self._A.dtype))\n    695 \n    696         if not (self._A.ndim == 2</p>\n\n<p>TypeError: Image data of dtype object cannot be converted to float\n```</p>\n\n<p>What am I doing wrong here?</p>",
  "messages": [
    {
      "id": 906868,
      "postDate": "2020-06-29T15:18:54.653Z",
      "content": "<p>I have the following code for displaying some image files:</p>\n\n<p>```\nimport cv2</p>\n\n<h1>Extract numpy values from Image column in data frame</h1>\n\n<p>images = train_df['image_id'].values</p>\n\n<h1>Extract 9 random images from it</h1>\n\n<p>random_images = [np.random.choice(images) for i in range(9)]</p>\n\n<h1>Location of the image dir</h1>\n\n<p>img_dir = '/kaggle/input/prostate-cancer-grade-assessment/train_images/'</p>\n\n<p>print('Display Random Images')</p>\n\n<h1>Adjust the size of your images</h1>\n\n<p>plt.figure(figsize=(20,10))</p>\n\n<h1>Iterate and plot random images</h1>\n\n<p>for i in range(9):\n    plt.subplot(3, 3, i + 1)\n    image_file = os.path.join(img_dir, random_images[i] + \".tiff\")\n    img = cv2.imread(image_file, cv2.IMREAD_GRAYSCALE)\n    plt.imshow(img, cmap='gray')\n    plt.axis('off')</p>\n\n<h1>Adjust subplot parameters to give specified padding</h1>\n\n<p>plt.tight_layout()   </p>\n\n<p>```</p>\n\n<p>However, I get this exception:</p>\n\n<h2>```</h2>\n\n<p>TypeError                                 Traceback (most recent call last)\n in \n     20     image_file = os.path.join(img_dir, random_images[i] + \".tiff\")\n     21     img = cv2.imread(image_file, cv2.IMREAD_GRAYSCALE)\n---&gt; 22     plt.imshow(img, cmap='gray')\n     23     plt.axis('off')\n     24 </p>\n\n<p>/opt/conda/lib/python3.7/site-packages/matplotlib/pyplot.py in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, data, *<em>kwargs)\n   2649         filternorm=filternorm, filterrad=filterrad, imlim=imlim,\n   2650         resample=resample, url=url, *</em>({\"data\": data} if data is not\n-&gt; 2651         None else {}), **kwargs)\n   2652     sci(__ret)\n   2653     return __ret</p>\n\n<p>/opt/conda/lib/python3.7/site-packages/matplotlib/<strong>init</strong>.py in inner(ax, data, *args, **kwargs)\n   1563     def inner(ax, *args, data=None, **kwargs):\n   1564         if data is None:\n-&gt; 1565             return func(ax, *map(sanitize_sequence, args), **kwargs)\n   1566 \n   1567         bound = new_sig.bind(ax, *args, **kwargs)</p>\n\n<p>/opt/conda/lib/python3.7/site-packages/matplotlib/cbook/deprecation.py in wrapper(*args, **kwargs)\n    356                 f\"%(removal)s.  If any parameter follows {name!r}, they \"\n    357                 f\"should be pass as keyword, not positionally.\")\n--&gt; 358         return func(*args, **kwargs)\n    359 \n    360     return wrapper</p>\n\n<p>/opt/conda/lib/python3.7/site-packages/matplotlib/cbook/deprecation.py in wrapper(*args, **kwargs)\n    356                 f\"%(removal)s.  If any parameter follows {name!r}, they \"\n    357                 f\"should be pass as keyword, not positionally.\")\n--&gt; 358         return func(*args, **kwargs)\n    359 \n    360     return wrapper</p>\n\n<p>/opt/conda/lib/python3.7/site-packages/matplotlib/axes/_axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)\n   5613                               resample=resample, **kwargs)\n   5614 \n-&gt; 5615         im.set_data(X)\n   5616         im.set_alpha(alpha)\n   5617         if im.get_clip_path() is None:</p>\n\n<p>/opt/conda/lib/python3.7/site-packages/matplotlib/image.py in set_data(self, A)\n    692                 not np.can_cast(self._A.dtype, float, \"same_kind\")):\n    693             raise TypeError(\"Image data of dtype {} cannot be converted to \"\n--&gt; 694                             \"float\".format(self._A.dtype))\n    695 \n    696         if not (self._A.ndim == 2</p>\n\n<p>TypeError: Image data of dtype object cannot be converted to float\n```</p>\n\n<p>What am I doing wrong here?</p>",
      "rawMarkdown": "I have the following code for displaying some image files:\n\n```\nimport cv2\n\n# Extract numpy values from Image column in data frame\nimages = train_df['image_id'].values\n\n# Extract 9 random images from it\nrandom_images = [np.random.choice(images) for i in range(9)]\n\n# Location of the image dir\nimg_dir = '/kaggle/input/prostate-cancer-grade-assessment/train_images/'\n\nprint('Display Random Images')\n\n# Adjust the size of your images\nplt.figure(figsize=(20,10))\n\n# Iterate and plot random images\nfor i in range(9):\n    plt.subplot(3, 3, i + 1)\n    image_file = os.path.join(img_dir, random_images[i] + \".tiff\")\n    img = cv2.imread(image_file, cv2.IMREAD_GRAYSCALE)\n    plt.imshow(img, cmap='gray')\n    plt.axis('off')\n    \n# Adjust subplot parameters to give specified padding\nplt.tight_layout()   \n\n```\n\nHowever, I get this exception:\n\n```\n---------------------------------------------------------------------------\nTypeError                                 Traceback (most recent call last)\n",
      "votes": 1
    },
    {
      "id": 907487,
      "postDate": "2020-06-30T02:20:03.847Z",
      "content": "<p>Each tiff file is a container consisting of 3 images. You can't just read it with \"cv2.imread\". You can use <a href=\"https://scikit-image.org/docs/stable/api/skimage.io.html#multiimage\">MultiImage</a> or <a href=\"https://openslide.org/api/python/\">OpenSlide</a> to extract the sequence of those images from the container.</p>",
      "rawMarkdown": "Each tiff file is a container consisting of 3 images. You can't just read it with \"cv2.imread\". You can use [MultiImage](https://scikit-image.org/docs/stable/api/skimage.io.html#multiimage) or [OpenSlide](https://openslide.org/api/python/) to extract the sequence of those images from the container."
    }
  ],
  "comments": [
    {
      "id": 907487,
      "author_name": "Raibek",
      "author_url": "",
      "post_date": "2020-06-30T02:20:03.847000",
      "content": "<p>Each tiff file is a container consisting of 3 images. You can't just read it with \"cv2.imread\". You can use <a href=\"https://scikit-image.org/docs/stable/api/skimage.io.html#multiimage\">MultiImage</a> or <a href=\"https://openslide.org/api/python/\">OpenSlide</a> to extract the sequence of those images from the container.</p>",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "906868": "I have the following code for displaying some image files:\n\n```\nimport cv2\n\n# Extract numpy values from Image column in data frame\nimages = train_df['image_id'].values\n\n# Extract 9 random images from it\nrandom_images = [np.random.choice(images) for i in range(9)]\n\n# Location of the image dir\nimg_dir = '/kaggle/input/prostate-cancer-grade-assessment/train_images/'\n\nprint('Display Random Images')\n\n# Adjust the size of your images\nplt.figure(figsize=(20,10))\n\n# Iterate and plot random images\nfor i in range(9):\n    plt.subplot(3, 3, i + 1)\n    image_file = os.path.join(img_dir, random_images[i] + \".tiff\")\n    img = cv2.imread(image_file, cv2.IMREAD_GRAYSCALE)\n    plt.imshow(img, cmap='gray')\n    plt.axis('off')\n    \n# Adjust subplot parameters to give specified padding\nplt.tight_layout()   \n\n```\n\nHowever, I get this exception:\n\n```\n---------------------------------------------------------------------------\nTypeError                                 Traceback (most recent call last)\n",
    "907487": "Each tiff file is a container consisting of 3 images. You can't just read it with \"cv2.imread\". You can use [MultiImage](https://scikit-image.org/docs/stable/api/skimage.io.html#multiimage) or [OpenSlide](https://openslide.org/api/python/) to extract the sequence of those images from the container."
  }
}