0

I am trying to open an .img. I run the following code:

import matplotlib.pyplot as plt
from planetaryimage import PDS3Image
image = ('/Users/alyse/ldem_1024_00n_15n_150_180.img')
plt.imshow(image, cmap='gray')

I get the following error: TypeError: Image data of dtype <U46 cannot be converted to float

alyse
  • 33
  • 4

1 Answers1

0

You can also use PIL. To install: pip install pillow

    import numpy as np
    from PIL import Image
    import matplotlib.pyplot as plt

    image = Image.open('/Users/alyse/ldem_1024_00n_15n_150_180.img')
    image_gray = image.convert("L") # Where L is option for grayscale
    array_gray = np.asarray(image_gray)
    
    plt.imshow(array_gray, cmap="gray")
    plt.show()
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Thank you for your help. I still receive an error: UnidentifiedImageError: cannot identify image file '/Users/alyse/ldem_1024_00n_15n_150_180.img' – alyse Jul 23 '20 at 20:40
  • See this answer: https://stackoverflow.com/questions/902761/saving-a-numpy-array-as-an-image This shows how to save an np array as an image – Boomerang20thCentury Jul 24 '20 at 06:45