Skip to content

Commit 2b46cf0

Browse files
authored
Add files via upload
1 parent 3da7c41 commit 2b46cf0

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

8Bit_Conversion.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env python2
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Created on Mon Nov 27 2017
6+
7+
@author: jshermeyer
8+
"""
9+
10+
import sys
11+
import subprocess
12+
import os
13+
import glob
14+
import gdal
15+
import numpy as np
16+
import datetime
17+
from tqdm import tqdm
18+
from fnmatch import fnmatch
19+
20+
21+
def display(image, display_min, display_max):
22+
image = np.array(image)
23+
image.clip(display_min, display_max, out=image)
24+
image -= display_min
25+
np.floor_divide(image, (display_max - display_min + 1) / 256,out=image, casting='unsafe')
26+
return image.astype(np.uint8)
27+
28+
def display_16(image, display_min, display_max):
29+
image = np.array(image)
30+
image.clip(display_min, display_max, out=image)
31+
image -= display_min
32+
np.floor_divide(image, (display_max - display_min + 1) / 65535,out=image, casting='unsafe')
33+
return image.astype(np.uint16)
34+
35+
36+
def convert_to_8Bit(inputRaster, outputRaster,
37+
outputPixType='Byte',
38+
outputFormat='GTiff',
39+
rescale_type='rescale',
40+
percentiles=[1, 99]):
41+
'''
42+
Convert 16bit image to 8bit
43+
rescale_type = [clip, rescale]
44+
if clip, scaling is done strictly between 0 65535
45+
if rescale, each band is rescaled to a min and max
46+
set by percentiles
47+
'''
48+
srcRaster = gdal.Open(inputRaster)
49+
driver = gdal.GetDriverByName(outputFormat)
50+
RasHolder=[]
51+
52+
# iterate through bands
53+
count=srcRaster.RasterCount
54+
for bandId in range(count):
55+
bandId = bandId+1
56+
band = srcRaster.GetRasterBand(bandId)
57+
geo=srcRaster.GetGeoTransform()
58+
proj=srcRaster.GetProjection()
59+
NoData=band.GetNoDataValue()
60+
#print NoData
61+
if NoData == None or NoData =="None":
62+
NoData=0
63+
#print NoData
64+
65+
band=band.ReadAsArray()
66+
shape=band.shape
67+
#band = np.ma.masked_equal(band,NoData)
68+
bandf=band.astype(float)
69+
bandf[np.where((band == NoData))] = np.nan
70+
if rescale_type == 'rescale':
71+
bmin = np.nanpercentile(bandf, percentiles[0])
72+
bmax = np.nanpercentile(bandf, percentiles[1])
73+
#print bmin, bmax
74+
75+
76+
else:
77+
bmin, bmax = 1, 7000
78+
79+
imout=display(bandf,bmin,bmax)
80+
imout[np.where((imout == 0))] = 1
81+
imout[np.where((band == NoData))] = 0
82+
RasHolder.append(imout)
83+
84+
rescale_out = driver.Create(outputRaster, RasHolder[0].shape[1], RasHolder[0].shape[0], 3, gdal.GDT_Byte)
85+
rescale_out.SetGeoTransform( geo )
86+
rescale_out.SetProjection( proj )
87+
rescale_out.GetRasterBand(1).WriteArray(RasHolder[2])
88+
rescale_out.GetRasterBand(1).SetNoDataValue(0)
89+
rescale_out.GetRasterBand(2).WriteArray(RasHolder[1])
90+
rescale_out.GetRasterBand(2).SetNoDataValue(0)
91+
rescale_out.GetRasterBand(3).WriteArray(RasHolder[0])
92+
rescale_out.GetRasterBand(3).SetNoDataValue(0)
93+
del rescale_out

0 commit comments

Comments
 (0)