0

I am trying to upscale an geotiff, using deep learning network. I read in a color geo-tiff using gdal and process each band separately and write that band back into the new geo-tiff driver. The code snippet that does this is given below.

 # Remove the scale the image and create separate image for each Band
 bands = tiffimg.shape[-1]

 #Create TIFF File driver similar to input
 driver = gdal.GetDriverByName("GTiff")
 outdata = driver.Create(f'{save_dir}/{imgname}.{fileExtension}', tiffDS.RasterXSize*2, tiffDS.RasterYSize*2, bands, gdal.GDT_UInt16)
 outpng = np.ones((tiffimg.shape[0]*2,tiffimg.shape[1]*2,tiffimg.shape[2]),dtype=tiffimg.dtype)

        
        
    for band in range(bands):
        singleBand = tiffimg[:,:,band]
        bandMaxValue = np.ndarray.max(singleBand)
        print(f'Band {band+1} min and max values before processing:{np.ndarray.min(singleBand)} \t {np.ndarray.max(singleBand)}')
        scaledBand = (singleBand).astype(np.float32)/bandMaxValue
        img_lq = np.stack((scaledBand,)*3,axis=-1)
        resizedImg = getSRImage(args, img_lq, imgname) # gets the upscaled band
        resizedImg = (resizedImg[:,:,1]*bandMaxValue).round().astype(tiffimg.dtype)
        print(f'Band {band+1} min and max values After processing:{np.ndarray.min(resizedImg)} \t {np.ndarray.max(resizedImg)}')
        outdata.GetRasterBand(band+1).WriteArray(resizedImg)
        outdata.GetRasterBand(band+1).SetNoDataValue(100000)##if you want these values transparent
        outpng[band] = resizedImg

            
 outdata.SetGeoTransform(tiffDS.GetGeoTransform())##sets same geotransform as input
 outdata.SetProjection(tiffDS.GetProjection())##sets same projection as input
 outdata.SetMetadata(tiffDS.GetMetadata())
 outdata.FlushCache() ##saves to disk!!
 cv2.imwrite(f'{save_dir}/backup/{imgname}.png', outpng[:,:,0:3])

But as the title suggest the resulting image has all the same characteristics as the parent image (the data type and the number of bands) but when viewing it, the saved image is a gray scale image and not a color image. Not sure where I am going wrong.

Any help is greatly appreciated.

Update 1: The output of exiftool for the original image is

ExifTool Version Number         : 12.44
File Name                       : sample.tif
Directory                       : .
File Size                       : 6.6 MB
File Modification Date/Time     : 2022:08:31 19:23:36-06:00
File Access Date/Time           : 2022:08:31 21:51:47-06:00
File Inode Change Date/Time     : 2022:08:31 19:23:36-06:00
File Permissions                : -rw-r--r--
File Type                       : TIFF
File Type Extension             : tif
MIME Type                       : image/tiff
Exif Byte Order                 : Little-endian (Intel, II)
Subfile Type                    : Full-resolution image
Image Width                     : 2076
Image Height                    : 1053
Bits Per Sample                 : 8 8 8
Compression                     : Uncompressed
Photometric Interpretation      : RGB
Strip Offsets                   : (Binary data 8254 bytes, use -b option to extract)
Orientation                     : Horizontal (normal)
Samples Per Pixel               : 3
Rows Per Strip                  : 1
Strip Byte Counts               : (Binary data 5264 bytes, use -b option to extract)
X Resolution                    : 72
Y Resolution                    : 72
Planar Configuration            : Chunky
Resolution Unit                 : inches
Software                        : Adobe Photoshop CS6 (Windows)
Modify Date                     : 2022:06:15 14:25:22
XMP Toolkit                     : Adobe XMP Core 5.3-c011 66.145661, 2012/02/06-14:56:27
Create Date                     : 2022:06:15 14:13:45+08:00
Metadata Date                   : 2022:06:15 14:25:22+08:00
Format                          : image/tiff
Color Mode                      : RGB
Instance ID                     : xmp.iid:EF964DEF73ECEC11B05DA55541B9A9AE
Document ID                     : xmp.did:EE964DEF73ECEC11B05DA55541B9A9AE
Original Document ID            : xmp.did:EE964DEF73ECEC11B05DA55541B9A9AE
History Action                  : created, saved
History Instance ID             : xmp.iid:EE964DEF73ECEC11B05DA55541B9A9AE, xmp.iid:EF964DEF73ECEC11B05DA55541B9A9AE
History When                    : 2022:06:15 14:13:45+08:00, 2022:06:15 14:25:22+08:00
History Software Agent          : Adobe Photoshop CS6 (Windows), Adobe Photoshop CS6 (Windows)
History Changed                 : /
Pixel Scale                     : 1 1 0
Model Tie Point                 : 0 0 0 7671 3249 0
IPTC Digest                     : 00000000000000000000000000000000
Displayed Units X               : inches
Displayed Units Y               : inches
Print Style                     : Centered
Print Position                  : 0 0
Print Scale                     : 1
Global Angle                    : 30
Global Altitude                 : 30
URL List                        : 
Slices Group Name               : 
Num Slices                      : 1
Pixel Aspect Ratio              : 1
Photoshop Thumbnail             : (Binary data 10449 bytes, use -b option to extract)
Has Real Merged Data            : Yes
Writer Name                     : Adobe Photoshop
Reader Name                     : Adobe Photoshop CS6
Color Space                     : Uncalibrated
Exif Image Width                : 2076
Exif Image Height               : 1053
Geo Tiff Version                : 1.1.0
GT Model Type                   : Geographic
GT Raster Type                  : Pixel Is Area
Geographic Type                 : WGS 84
Geog Citation                   : WGS 84
Geog Angular Unit Size          : 4.84813681109536e-06
Geog Semi Major Axis            : 6378137
Geog Inv Flattening             : 298.257223563
Image Size                      : 2076x1053
Megapixels                      : 2.2

while the up-scaled one is

ExifTool Version Number         : 12.44
File Name                       : sample_upscaled.TIF
Directory                       : .
File Size                       : 52 MB
File Modification Date/Time     : 2022:09:01 12:12:57-06:00
File Access Date/Time           : 2022:09:01 12:12:59-06:00
File Inode Change Date/Time     : 2022:09:01 12:12:57-06:00
File Permissions                : -rw-r--r--
File Type                       : TIFF
File Type Extension             : tif
MIME Type                       : image/tiff
Exif Byte Order                 : Little-endian (Intel, II)
Image Width                     : 4152
Image Height                    : 2106
Bits Per Sample                 : 16 16 16
Compression                     : Uncompressed
Photometric Interpretation      : BlackIsZero
Strip Offsets                   : (Binary data 18508 bytes, use -b option to extract)
Samples Per Pixel               : 3
Rows Per Strip                  : 1
Strip Byte Counts               : (Binary data 12635 bytes, use -b option to extract)
Planar Configuration            : Chunky
Extra Samples                   : Unknown (0 0)
Sample Format                   : Unsigned; Unsigned; Unsigned
GDAL No Data                    : 100000
Image Size                      : 4152x2106
Megapixels                      : 8.7 

Solution Update:

As suggested in the comments, the "Photometric interpretation" was the problem. Added "Photometric interpretation=RGB" while creating the driver. which solved the issue.

driver = gdal.GetDriverByName("GTiff")
options = ['PHOTOMETRIC=RGB', 'PROFILE=GeoTIFF']
outdata = driver.Create(f'{save_dir}/{imgname}.{fileExtension}', tiffDS.RasterXSize*2, tiffDS.RasterYSize*2, bands, gdal.GDT_UInt16, options=options)
spacemanspiff
  • 113
  • 12
  • Try running `exiftool YOURCREATEDIMAGE.TIF` and then click [edit] and paste the output into your question so we can see exactly what has been created. – Mark Setchell Sep 01 '22 at 06:36
  • 1
    The *"Photometric Interpretation"* looks wrong. Have a look here... https://stackoverflow.com/a/73480008/2836621 – Mark Setchell Sep 01 '22 at 19:31
  • 1
    I also would properly close the `gdal.Dataset` once you're done using `outdata = None`, before viewing it elsewhere. Does "Samples per pixel" mean it does have the expected 3 bands? You can set the interpretation using `band.SetColorInterpretation(gdal.GCI_RedBand)`. – Rutger Kassies Sep 05 '22 at 06:44
  • Thanks @MarkSetchell and @Rutger. Looks like the "photometric interpretation" was the problem. I added `options = ['PHOTOMETRIC=RGB', 'PROFILE=GeoTIFF']` as options to driver.create `outdata = driver.Create(f'{save_dir}/{imgname}.{fileExtension}', tiffDS.RasterXSize*2, tiffDS.RasterYSize*2, bands, gdal.GDT_UInt16, options=options)` while creating the driver , which seems to have fixed it. Thanks Again!! – spacemanspiff Sep 06 '22 at 17:19

0 Answers0