MUstar

IoT Python3 1124 - MySQL Image Uploader

Nov 23rd, 2017
43
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys,os,pymysql
  2. import sys
  3. from PIL import Image
  4. import base64
  5. import io
  6. import PIL.Image
  7.  
  8. serverip = None
  9. username = None
  10. password = None
  11. database = None
  12. conn = None
  13. cur = None
  14.  
  15. def connect():
  16.     global serverip, username, password, database, conn, cur
  17.     serverip = input("ServerIP>")
  18.     username = input("username>")
  19.     password = input("password>")
  20.     database = input("Database>")
  21.     try:
  22.         conn = pymysql.connect(host=serverip,user=username, password=password, db=database, charset = 'utf8mb4',autocommit=True)
  23.         cur = conn.cursor()
  24.     except pymysql.Error as err:
  25.         print(err)
  26.         print("App>DB서버연결에 실패했습니다.")
  27.         sys.exit(1)
  28.            
  29. def input_value(): # Input Value
  30.     global cur
  31.     _id = "" #input("idvl>")
  32.     name = input("name>")
  33.     info = input("info>")
  34.     query = "insert into pytable (id,name,info) value('NULL','"  +name+ "','" +info+ "');"
  35.     cur.execute(query)
  36.    
  37. def print_row(): # Print Tables
  38.     global cur
  39.     cur.execute("select * from pytable");
  40.     row = cur.fetchall()
  41.     print(row)
  42.  
  43. def input_image() :
  44.     f = open("/home/pi/testimg/imglist.txt",'r')
  45.     url = "/home/pi/testimg/"
  46.     while True:
  47.         line = f.readline()
  48.         if not line : break;
  49.         tmp_g = line.split('/')
  50.         filename = tmp_g[1]
  51.         line = line.rstrip()
  52.         filename = filename.rstrip()
  53.         image = Image.open(url+line)
  54.         blob_v = open(url+line,'rb').read()
  55.         width, height = image.size
  56.         size = os.path.getsize(url+line)
  57.         print("[",filename,"/",width,"/",height,"/",size,"]")
  58.         print("["+filename+"] uploaded")
  59.         query = "insert into image_pixel(data,name,info,width,height,size) value(%s,'"+filename+"','"+line+"',"+str(width)+","+str(height)+","+str(size)+");"
  60.         cur.execute(query,(blob_v,))
  61.     f.close()
  62.  
  63. def output_image() :
  64.     cur.execute("select * from image_pixel")
  65.     data=cur.fetchall()
  66.     print(type(data[0][0]))
  67.     file = io.BytesIO(data[0][0])
  68.     for row in data:
  69.         print("["+row[1]+"] Download!")
  70.         f = open("/home/pi/downimg/"+row[1],"wb")
  71.         f.write(row[0])
  72.         f.close()
  73.     #img = PIL.Image.open(file)
  74.     #img.show()
  75.    
  76. connect()
  77. print("App>",serverip ,"에 연결 성공")
  78.  
  79. select = None
  80.  
  81. while True:
  82.     print("\n\nMenu>1)Input 2>Print 3>Exit")
  83.     select = input("select>")
  84.  
  85.     if select == '' : print("App>Invalid Value")
  86.     elif select == '1':
  87.         input_image()
  88.     elif select == '2':
  89.         output_image()
  90.     elif select == '3':
  91.         break
  92.     else : print("App>Invalid Value")
  93. conn.close()
  94. cur.close()
  95. print("App>byebye")
RAW Paste Data