0

I have tried to create a geotiff image after extracting texture feature of a grayscale geotiff image using the below codes.

#Creating texture images
Texture_Contrast = Image.fromarray(Contrast)

#image coordinates corner
(left_x_min,bottom_y_min,right_x_max,top_y_max) = im_geo.bounds

# Obtain the projection system
CRS = im_geo.crs

# set geotransform
nx = width
ny = height
xres = (right_x_max - left_x_min) / float(nx)
yres = (top_y_max - bottom_y_min) / float(ny)
geotransform = (left_x_min, xres, 0, top_y_max , 0, -yres)

dst_format = 'Gtiff'
dst_datatype = gdal.GDT_Byte
dst_options = ['COMPRESS=LZW']
dst_file = 'myGEOTIFF.tif'
dst_nbands = 1

driver = gdal.GetDriverByName(dst_format)
dst_ds = driver.Create(dst_file, nx, ny, dst_nbands, dst_datatype, dst_options)
dst_ds.GetRasterBand(1).WriteArray(Texture_Contrast)

dst_ds.SetGeoTransform(geotransform)    # specify coords
srs = osr.SpatialReference()            # establish encoding
srs.ImportFromEPSG(32752)                # WGS84 lat/long
dst_ds.SetProjection(srs.ExportToWkt()) # export coords to file

dst_ds.GetRasterBand(1).WriteArray(Texture_Contrast)   # write r-band to the raster

dst_ds.FlushCache()                     # write to disk
dst_ds = None

However, after running the WriteArray(Texture_Contrast) the following error was arise.

  Input In [47] in <module>
    out_band.WriteArray(Texture_Contrast)

  File ~\miniconda3\envs\spyder-env\lib\site-packages\osgeo\gdal.py:4090 in WriteArray
    return gdal_array.BandWriteArray(self, array, xoff, yoff,

  File ~\miniconda3\envs\spyder-env\lib\site-packages\osgeo\gdal_array.py:509 in BandWriteArray
    if array is None or len(array.shape) != 2:

  File ~\miniconda3\envs\spyder-env\lib\site-packages\PIL\Image.py:546 in __getattr__
    raise AttributeError(name)

AttributeError: shape

Could anyone give an enlightenment and the solution of this? Thank you in advance.

1 Answers1

0

Are the shapes (a,b) and nx, ny of the array you extracted the same? nx and ny must be the same shape as the array you created.

or

You said you wrote a gray image, but the result of Texture_Contrast.shape might be (x, y, z). If so, you need to select a channel and make it 2dim.

The error message seems to be the case below. Please confirm it.

Urban87
  • 243
  • 1
  • 8
  • Thank you for the answer. Actually the Contrast image was created by deriving texture GLCM of the original grayscale geotiff image. The codes above is now working by not using Texture_Contrast in " dst_ds.GetRasterBand(1).WriteArray(Texture_Contrast)" and only using the Contrast directly – Hendra Febriawan Feb 23 '22 at 06:56