컴퓨터/파이썬

tiff-csv converter

snowall 2012. 8. 8. 16:52

TIFF-CSV 변환기. Python 2.7에서 작성되었고 ImageMagick과 wxPython이 필요하다.

# -*- coding: utf-8 -*-

# This program converts 16-bit TIFF files from XCAP of EPIX CCD to .csv and .png.
# This program depends on ImageMagick and wxPython.
# Please make sure 'convert' command is possible at the program path.
# Copyright 2012, Kee Hwan Nam, APRI, GIST.

# This program can be used under Gnu Public Licence version 3.

# Refer to http://www.gnu.org/copyleft/gpl.html


import wx
import os
import csv
import sys
reload(sys)
sys.setdefaultencoding('cp949')

def convertfilter(p):
    a = csv.reader(open(p.replace("\\\\","\\"), 'r'), delimiter=',')
    pp = p[:-3] + "csv"
    ppp=open(pp, 'w')

    index = 1
    flag = 0
    for i in a:
        if flag == 0:
            d=int(i[0].split(":")[1])
            flag+=1
        else:
            ppp.write(i[2])
            if index%d==0:
                ppp.write("\n")
            else:
                ppp.write(",")
            index+=1

def converting(a, dirn,filen):
    for filens in filen:
        #~ print(filens)
        print(filens[-3:].lower())
        if filens[-3:].lower()=="tif":
            filename = dirn+"\\"+filens
            print(("convert \"" + filename + "\" \"" + filename + ".txt\""))
            os.system("convert \"" + filename + "\" \"" + filename + ".txt\"")
            os.system("convert \"" + filename + "\" \"" + filename + ".png\"")
            convertfilter(filename + ".txt")
            os.system("del \"" + filename + ".txt\"")

class mainframe(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)

        dig = wx.DirDialog(None, message="Choose data files", style = wx.FD_MULTIPLE)
        if dig.ShowModal() == wx.ID_OK:
            pp = dig.GetPath()
        else:
            exit()
        os.path.walk(dig.GetPath(), converting, 0)

class TIFFconv(wx.App):
    def OnInit(self):
        wx.InitAllImageHandlers()
        main = mainframe(None, -1, "")
        self.SetTopWindow(main)
        main.Show(True)
        main.Show(False)
        exit()
        return 1

if __name__ == "__main__":
    TIFFconv = TIFFconv(0)
    TIFFconv.MainLoop()