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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | from tkinter import * from tkinter.simpledialog import * from tkinter.filedialog import * import math import os import os.path import pymysql import datetime ####################### #######전역 변수######## ####################### IP_ADDR = '192.168.56.107' USER_NAME = "root" USER_PW = "1234" DB_NAME = "BigData_DB" CHAR_SET = 'utf8' ####################### #######함수 모음######## ####################### def selectFile(): filename = askopenfilename(parent=window, filetypes=(("RAW 파일", "*.raw"), ("모든 파일", "*.*"))) if filename == '' or filename == None: return edt1.insert(0, str(filename)) def selectfolder(): global rawFileList foldername = askdirectory(parent=window) if foldername == '' or foldername == None: return edt1.insert(0, str(foldername)) # 파일 목록 뽑기 rawFileList = [] for dirName, subDirList, fnames in os.walk(foldername) : for fname in fnames : filename, extname = os.path.basename(fname).split(".") if extname.upper().strip() == 'RAW' : rawFileList.append(os.path.join(dirName,fname)) def uploadData(): con = pymysql.connect(host=IP_ADDR, user = USER_NAME, password = USER_PW, db = DB_NAME, charset = CHAR_SET) cur = con.cursor() fullname = edt1.get() with open(fullname, 'rb') as rfp: binData = rfp.read() print(type(binData)) fname = os.path.basename(fullname) fsize = os.path.getsize(fullname) height = width = int(math.sqrt(fsize)) now = datetime.datetime.now() upDate = now.strftime('%Y-%m-%d') upUser = USER_NAME sql = "INSERT INTO rawImage_TBL2(id , raw_height , raw_width" sql += ", raw_name , raw_update , raw_uplaoder , raw_avg , raw_data) " sql += " VALUES(NULL," + str(height) + "," + str(width) + ",'" sql += fname + "','" + upDate +"','" + upUser + "',0 , " sql += " %s )" tupleData = (binData,) # place holder에 넣을 인자들을 모두 cur.execute(sql, tupleData) con.commit() cur.close() con.close() def malloc(h, w, initValue = 0) : l = [] for i in range(h) : tmpList = [] for j in range(w) : tmpList.append(initValue) l.append(tmpList) return l def findStat(fname): # 파일 열고, 읽기. fsize = os.path.getsize(fname) # 파일의 크기(바이트) inH = inW = int(math.sqrt(fsize)) # 핵심 코드 ## 입력영상 메모리 확보 ## inImage=[] inImage=malloc(inH,inW) # 파일 --> 메모리 with open(fname, 'rb') as rFp: for i in range(inH) : for k in range(inW) : inImage[i][k] = int(ord(rFp.read(1))) sum = 0 for i in range(inH): for k in range(inW) : sum += inImage[i][k] avg = sum // (inW * inH) maxVal = minVal = inImage[0][0] for i in range(inH): for k in range(inW): if inImage[i][k] < minVal: minVal = inImage[i][k] elif inImage[i][k] > maxVal: maxVal = inImage[i][k] return avg, maxVal, minVal def uploadDataFromFolder(): global rawFileList con = pymysql.connect(host=IP_ADDR, user = USER_NAME, password = USER_PW, db = DB_NAME, charset = CHAR_SET) cur = con.cursor() try : sql = ''' CREATE TABLE rawImage_TBL2 ( raw_id INT AUTO_INCREMENT PRIMARY KEY, raw_fname VARCHAR(30), raw_extname CHAR(5), raw_height SMALLINT, raw_width SMALLINT, raw_avg TINYINT UNSIGNED , raw_max TINYINT UNSIGNED , raw_min TINYINT UNSIGNED , raw_data LONGBLOB ); ''' cur.execute(sql) except : pass for fullname in rawFileList: with open(fullname, 'rb') as rfp: binData = rfp.read() fname, extname = os.path.basename(fullname).split(".") fsize = os.path.getsize(fullname) height = width = int(math.sqrt(fsize)) now = datetime.datetime.now() upDate = now.strftime('%Y-%m-%d') upUser = USER_NAME avgVal, maxVal, minValue = findStat(fullname) # 평균 최대, 최소 sql = "INSERT INTO rawImage_TBL2(raw_id , raw_fname,raw_extname," sql += "raw_height,raw_width,raw_avg,raw_max,raw_min,raw_data) " sql += " VALUES(NULL,'" + fname + "','" + extname +"'," sql += str(height) + "," + str(width) + "," sql += str(avgVal)+ "," + str(maxVal) + "," + str(minValue) sql += ", %s )" tupleData = (binData,) # place holder에 넣을 인자들을 모두 cur.execute(sql, tupleData) con.commit() cur.close() con.close() import tempfile def downloadData(): con = pymysql.connect(host=IP_ADDR, user = USER_NAME, password = USER_PW, db = DB_NAME, charset = CHAR_SET) cur = con.cursor() sql = "select raw_fname, raw_data from rawImage_tbl where id = 1" cur.execute(sql) fname, binData = cur.fetchone() fullPath = tempfile.gettempdir() + "/" + fname with open(fullPath, 'wb') as wfp: wfp.write(binData) print(fullPath) cur.close() con.close() #메인 코드부 window = Tk() window.geometry("500x500") window.title("raw --> DB Ver 0.0.2") edt1 = Entry (window, width = 50); btnFile = Button(window, text = "file select", command = selectFile) btnDir = Button(window, text = "folder select", command = selectfolder) btnUpload = Button(window, text = "file upload", command = uploadData) btnUpload = Button(window, text = "folder upload", command = uploadDataFromFolder) btnDownload = Button(window, text = "download", command = downloadData) edt1.pack() btnFile.pack() btnDir.pack() btnUpload.pack() btnDownload.pack() window.mainloop() | cs |
'딥러닝 모델 설계 > Python 공통' 카테고리의 다른 글
MatPlotlib를 이용한 히스토그램 그리기 (0) | 2019.06.17 |
---|---|
트리뷰 활용 엑셀만들기 (0) | 2019.06.17 |
Day 2. List,Tuple,dictionary (0) | 2019.06.11 |
Day 1. 기본 조건 , 반복, 연산자 (0) | 2019.06.05 |
SqlLite and mySQL 연동 + 간단 쿼리문 (0) | 2019.06.05 |