开发者

Savetxt file for images properties in a loop in python

I create a program that calculates number os pixels of certain value of temperature in a Satellite image. I used:

for infile in glob.glob(*.tif) 

for open all .tif images in a loop.The program is working fine, and print in the terminal de name and de pixels number of all images. But when i use:

np.savetxt("pixels.txt", (File, pixelstemp1, pixelstemp2), fmt="%1s", delimiter=' ') 

only saves the pixels number of the last image in the txt file. How can i do for save in a txt file the number os pixels of all images?

from pylab import *

import numpy

import glob, os 


for infile in glob.glob("*04.tif"):
# pretend the rest of this is indented another level

dir, file = os.path.split(infile)
file, ext = os.path.splitext(infile)
sat=imread(infile)
satno开发者_JS百科vo=where(logical_and((418.-sat)-273.15>=-90,(418.-sat)-273.15<=-31),(418.-sat)-273.15,(660.-sat)/2-273.15)
temperatura=[-80,-70,-60,-50,-40,-30]
mask1=where(logical_and(satnovo<-30,satnovo>-80), 1,0)
mask2=where(logical_and(satnovo<-40 ,satnovo>-80),1,0)
mask3=where(logical_and(satnovo<-50, satnovo>-80),1,0)
mask4=where(logical_and(satnovo<-60, satnovo>-80),1,0)
mask5=where(logical_and(satnovo<-70, satnovo>-80),1,0)
x1=ma.masked_object(mask1,0)
pixels1=ma.count(x1)

x2=ma.masked_object(mask2,0)
pixels2=ma.count(x2)

x3=ma.masked_object(mask3,0)
pixels3=ma.count(x3)

x4=ma.masked_object(mask4,0)
pixels4=ma.count(x4)

x5=ma.masked_object(mask5,0)
pixels5=ma.count(x3)

print infile,pixels1,pixels2,pixels3,pixels4,pixels5
a=np.array([infile,pixels1,pixels2,pixels3,pixels4,pixels5],dtype=object)
np.savetxt("contapixels.txt",a[None],fmt='%1s', delimiter=' ')


As agf notes, there isn't any error as such in the few lines you've provided, but from your description I think you are calling np.savetxt for each image? If so, you'll overwrite pixels.txt on each pass. You want to either store all the data in some temporary list and write it at the end:

pixeldata = []
for infile in glob.glob("*.tif"):
    # Do processing
    pixeldata.append((infile, pixelstemp1, pixelstemp2))
np.savetxt("pixels.txt", pixeldata, fmt="%1s", delimiter=' ')

Or you could open the file and then append to it in every pass through the loop. However, then you can't use np.savetxt, so the method above is probably better.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜