0

I am trying to read an image using PIL (I think it needs to be PIL because I am working with a filehandle and OpenCV was throwing an error when I tried using it to read in the image).

I would like to read in the image like so;

pil_image = Image.open('sample_image.tif').convert('RGB') # it is in tiff format

# and convert to numpy array
img = np.asarray(pil_image)

plt.imshow(img)

enter image description here

However, whenever I try to convert the image and plot it, I get a blank result (as shown above). I need the NumPy array to be a uint8 for preprocessing reasons. But every method I use to convert the original image from PIL to a NumPy array with dtype=uint8 (the other method I tried is this..

pil_image = Image.open('Sample_image.tif')

img = np.array(pil_image) / 255
img = img.astype(np.uint8)

), I either get a blank screen or undesired results. Finally, I tried following some suggestions from here Convert image from PIL to openCV format, and I get no luck. Any suggestions on how to solve this would be really helpful cheers.

Caesar
  • 117
  • 10
  • Can you make it work with any other TIFF image? With any other *image*? – Karl Knechtel Jun 04 '22 at 00:55
  • Hey, Check the Third comment on [How to open TIFF using PIL on Windows?](https://stackoverflow.com/questions/55815303/how-to-open-tiff-using-pil-on-windows) – script0 Jun 04 '22 at 01:00
  • @KarlKnechtel yeah I experienced the same problems with other images of the same format (I'm working with multiple tif files) – Caesar Jun 04 '22 at 21:07
  • Are you able to load and verify the contents of the .tif files in any other way? – Karl Knechtel Jun 04 '22 at 22:11
  • @KarlKnechtel Yeah the first line works without applying the convert method. I found a workaround to the problem though but thanks a lot for your help. – Caesar Jun 05 '22 at 00:00
  • The easiest way to get a proper answer is to click [edit] and share your TIFF. – Mark Setchell Jun 05 '22 at 20:57

1 Answers1

0

I can't put code all this code in comment, so if it doesn't answer/help you, I'll delete it.

I ran the test below with the sample tiff (1MB) which you can find here:

https://file-examples.com/index.php/sample-images-download/sample-tiff-download/

Put the sample in your content directory then try the code below:

import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import gdal

img = mpimg.imread('sample.tiff') #change to your directory path

tif = gdal.Open('sample.tiff') #change to your directory path
tifArray = tif.ReadAsArray()
band1 = tif.GetRasterBand(1)
band1Array = band1.ReadAsArray()

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12,12))
for a in fig.get_axes():
  a.axis('off')

ax1.imshow(img)
ax2.imshow(band1Array)

The result should be:

enter image description here

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
Drakax
  • 1,305
  • 3
  • 9
  • Thanks a lot for your help, but I'm just not keen on using Gdal because I'm already using three other image processing libraries and downloading gdal/osgeo is a real pain. I found a workaround but thanks anyway again. – Caesar Jun 05 '22 at 00:02
  • You already use matplotlib, would you at least try the example with "mpimg" from matplotlib? Like this you could open AND plot with 1 module. – Drakax Jun 05 '22 at 00:09