forked from GeostatsGuy/PythonNumericalDemos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage2GSLIB.py
65 lines (53 loc) · 1.64 KB
/
image2GSLIB.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import csv, sys
import os
import numpy as np
import pandas as pd
from scipy import misc
# Michael Pyrcz, Univ. of Texas at Austin, @GeostatsGuy
#
# Python Script to convert a JPG to intensity GSLIB File
# python filter.py input_filename output_filename
#
# Example:
#
# python image2GSLIB my_image_file.jpg my_GSLIB_file.dat
#
def Dataframe2GSLIB(data_file,df):
colArray = []
colArray = df.columns
ncol = len(df.columns)
nrow = len(df.index)
file_out = open(data_file, "w")
file_out.write(data_file + '\n')
file_out.write(str(ncol) + '\n')
for icol in range(0, ncol):
file_out.write(df.columns[icol] + '\n')
for irow in range(0, nrow):
for icol in range(0, ncol):
file_out.write(str(df.iloc[irow,icol])+ ' ')
file_out.write('\n')
file_out.close()
filename = sys.argv[1]
outname = sys.argv[2]
arr = misc.imread(filename)
nx = arr.shape[0]
ny = arr.shape[1]
arr2 = np.zeros((nx*ny,4))
count = 0
for ix in range(0, nx):
for iy in range(0, ny):
iix = int(nx-1) - int(ix)
arr2[count,0] = int(255) - (int(arr[iix,iy,0]) + int(arr[iix,iy,1]) + int(arr[iix,iy,2]))/3
arr2[count,1] = (int(arr[ix,iy,0]))
arr2[count,2] = (int(arr[ix,iy,1]))
arr2[count,3] = (int(arr[ix,iy,2]))
count = count + 1
df = pd.DataFrame(arr2)
colArray = []
colArray.append("Intensity")
colArray.append("Red")
colArray.append("Green")
colArray.append("Blue")
df.columns = colArray
print("Written file is " + str(ny) + " by " + str(nx) + " - v1.2")
Dataframe2GSLIB(outname,df)