2016-04-28 6 views
2

Ich habe eine Textdatei mit Messdaten, die so aussieht.Werte an Koordinaten zu Bild

x y z 
1 3 -2 
2 1 -3 
3 1 1 
2 2 3 
1 2 2 
2 3 0 

Dies würde die folgenden Mess implizieren (auf einer x, y-Gitter)

-2 0 
2 3 
    -3 1 

ich ein Bild aus diesen Werten erstellt werden soll, wo keine Messung, dass das Bild transparent ist, bedeuten würde. Wenn möglich, würde ich die Z-Werte (von zB -9.4 bis +3.2) zu einer Colormap wie colormap.jet

zuordnen Ich habe versucht, dies mit der Python Image Library und Putpixel tun, aber das ist sehr langsam und ich bin sicher, dass es einen besseren Weg geben muss, dies zu tun.

Mein aktueller Code: basePath = os.path.dirname (os.path.realpath (Datei)) # definiert das Verzeichnis, in dem die aktuelle Datei srcFiles = glob.glob befindet ('* Pkt.)

für filename in srcFiles:

data = pd.read_csv(os.path.join(basePath, fileName), names=['x', 'y', 'z'], delim_whitespace=True) 

print fileName 
maxX = data.x.max() 
minX = data.x.min() 
maxY = data.y.max() 
minY = data.y.min() 
minZ = data.z.min() 
maxZ = data.z.max() 

width = maxX-minX 
height = maxY-minY 

img = Image.new('L', (int(width), int(height))) 


for x in range(int(width)): 
    for y in range(int(height)): 
     value = data[(data['x'] == (minX+x)) & (data['y'] == (minY+y))]['z'] 
     if len(value) == 0: 
      value = 99.; 

     img.putpixel((x,y),int(value)) 

img.save('test.png') 

Antwort

2

Vielleicht sollten Sie nur eine numpy Matrix verwenden, um das Bild zu manipulieren. Ich habe den CSV-Teil nicht gelesen, wie du es schon hast. Das maskierte Array lässt transparente Pixel zu.

import numpy as np 
import matplotlib.pyplot as plt 

INPUT = np.array(
[[1, 3, -2] 
,[2, 1, -3] 
,[3, 1, 1] 
,[2, 2, 3] 
,[1, 2, 2] 
,[2, 3, 0]]) 

# get ranges 
xmin = INPUT[:,0].min() 
xmax = INPUT[:,0].max() 
ymin = INPUT[:,1].min() 
ymax = INPUT[:,1].max() 
zmin = INPUT[:,2].min() 
zmax = INPUT[:,2].max() 

# create array for image : zmax+1 is the default value 
shape = (xmax-xmin+1,ymax-ymin+1) 
img = np.ma.array(np.ones(shape)*(zmax+1)) 

for inp in INPUT: 
    img[inp[0]-xmin,inp[1]-ymin]=inp[2] 

# set mask on default value 
img.mask = (img==zmax+1) 

# set a gray background for test 
img_bg_test = np.zeros(shape) 
cmap_bg_test = plt.get_cmap('gray') 
plt.imshow(img_bg_test,cmap=cmap_bg_test,interpolation='none') 

# plot 
cmap = plt.get_cmap('jet') 
plt.imshow(img,cmap=cmap,interpolation='none',vmin=zmin,vmax=zmax) 
plt.colorbar() 

plt.imsave("test.png",img) 
plt.show() 
plt.close() 

output zur Kenntnis, dass der imsave nicht die Figur, die ich hier zeige nicht retten, aber das Bild, wie Sie wollen, das t wouldn mit 3x3 Pixel interessant sein.