tobby48 před 4 roky
rodič
revize
70b4b0ee49

+ 34
- 0
src/kr/co/swh/lecture/python/pyqt5/drawing/blob_insert.py Zobrazit soubor

@@ -0,0 +1,34 @@
1
+import pymysql.cursors
2
+
3
+def convertToBinaryData(filename):
4
+    # Convert digital data to binary format
5
+    with open(filename, 'rb') as file:
6
+        binaryData = file.read()
7
+    return binaryData
8
+
9
+def insertBLOB(id, name, photo, biodataFile):
10
+    try:
11
+        conn = pymysql.connect(host='dev-swh.ga',
12
+                                             database='market',
13
+                                             user='root',
14
+                                             password='swhacademy!',
15
+                                             charset='utf8')
16
+
17
+        with conn.cursor() as cursor:
18
+            sql_insert_blob_query = """ INSERT INTO image
19
+                          (id, name, photo, data) VALUES (%s,%s,%s,%s)"""
20
+
21
+            picture = convertToBinaryData(photo)
22
+            file = convertToBinaryData(biodataFile)
23
+
24
+            # Convert data into tuple format
25
+            insert_blob_tuple = (id, name, picture, file)
26
+            result = cursor.execute(sql_insert_blob_query, insert_blob_tuple)
27
+            conn.commit()
28
+            print("Image and file inserted successfully as a BLOB into image table", result)
29
+
30
+    finally:
31
+        conn.close()
32
+        print("MySQL connection is closed")
33
+
34
+insertBLOB(1, "Family", "family.jpg", "family_data.txt")

+ 28
- 0
src/kr/co/swh/lecture/python/pyqt5/drawing/blob_select.py Zobrazit soubor

@@ -0,0 +1,28 @@
1
+import pymysql.cursors
2
+from io import BytesIO
3
+from PIL import Image
4
+
5
+
6
+
7
+def selectBLOB(id):
8
+    try:
9
+        conn = pymysql.connect(host='dev-swh.ga',
10
+                                             database='market',
11
+                                             user='root',
12
+                                             password='swhacademy!',
13
+                                             charset='utf8')
14
+
15
+        with conn.cursor() as cursor:
16
+            sql_select_blob_query = "SELECT photo FROM image where id = %d" % id
17
+            cursor.execute(sql_select_blob_query)
18
+            data=cursor.fetchone()
19
+            if data:
20
+                print(type(data))
21
+                getImage = Image.open(BytesIO(data[0]))
22
+                getImage.show()
23
+
24
+    finally:
25
+        conn.close()
26
+        print("MySQL connection is closed")
27
+
28
+selectBLOB(1)

+ 40
- 0
src/kr/co/swh/lecture/python/pyqt5/drawing/blob_select_flask.py Zobrazit soubor

@@ -0,0 +1,40 @@
1
+import pymysql.cursors
2
+import base64
3
+from flask import Flask, render_template
4
+app = Flask(__name__)
5
+
6
+
7
+def selectBLOB(id):
8
+    try:
9
+        conn = pymysql.connect(host='dev-swh.ga',
10
+                                             database='market',
11
+                                             user='root',
12
+                                             password='swhacademy!',
13
+                                             charset='utf8')
14
+
15
+        with conn.cursor() as cursor:
16
+            sql_select_blob_query = "SELECT photo FROM image where id = %d" % id
17
+            cursor.execute(sql_select_blob_query)
18
+            data=cursor.fetchone()
19
+            if data:
20
+                print(type(data[0]))
21
+#                 바이트를 이미지로 표현하기 위해 Data URL형태로 표현
22
+#                 웹 페이지로 바이트를 단독으로 데이터형태로 옮길 수 없기에, 문자열 형태로 변환
23
+
24
+#                 Base64란 소리나 이미지 영상 등 (Binary Data)를 Text로 바꾸는 Encoding(binary-to-text encoding schemes)
25
+#                 Binary Data를 Character set에 영향을 받지 않는 공통 ASCII 영역의 문자로만 이루어진 문자열로 바꾸는 Encoding
26
+                getImage = base64.b64encode(data[0])
27
+                getImage = getImage.decode("UTF-8")
28
+                return getImage
29
+
30
+    finally:
31
+        conn.close()
32
+        print("MySQL connection is closed")
33
+
34
+@app.route('/<int:id>')
35
+def hello(id):
36
+    getImage = selectBLOB(id)
37
+    return render_template('image.html', image=getImage)
38
+
39
+if __name__ == "__main__":
40
+    app.run(host="127.0.0.1", port="8080")

+ 21
- 0
src/kr/co/swh/lecture/python/pyqt5/drawing/drawing1.py Zobrazit soubor

@@ -0,0 +1,21 @@
1
+import sys
2
+from PyQt5.QtWidgets import *
3
+
4
+class Window(QMainWindow):
5
+    def __init__(self):
6
+        super().__init__()
7
+        self.title = "PyQt5 Drawing Tutorial"
8
+        self.top= 150
9
+        self.left= 150
10
+        self.width = 500
11
+        self.height = 500
12
+        self.InitWindow()
13
+
14
+    def InitWindow(self):
15
+        self.setWindowTitle(self.title)
16
+        self.setGeometry(self.top, self.left, self.width, self.height)
17
+        self.show()
18
+        
19
+App = QApplication(sys.argv)
20
+window = Window()
21
+sys.exit(App.exec())        

+ 41
- 0
src/kr/co/swh/lecture/python/pyqt5/drawing/drawing2.py Zobrazit soubor

@@ -0,0 +1,41 @@
1
+import sys
2
+from PyQt5.QtWidgets import *
3
+from PyQt5.QtGui import QPainter, QBrush, QPen
4
+from PyQt5.QtCore import Qt
5
+
6
+class Window(QMainWindow):
7
+    def __init__(self):
8
+        super().__init__()
9
+        self.title = "PyQt5 Drawing Tutorial"
10
+        self.top= 150
11
+        self.left= 150
12
+        self.width = 500
13
+        self.height = 500
14
+        self.InitWindow()
15
+
16
+    def InitWindow(self):
17
+        self.setWindowTitle(self.title)
18
+        self.setGeometry(self.top, self.left, self.width, self.height)
19
+        self.show()
20
+        
21
+#    Painter는 기본 도형을 그리는 다양한 기능을 제공. 선, 직사각형 등.
22
+#    
23
+    def paintEvent(self, event):
24
+        painter = QPainter(self)
25
+        
26
+#         QPen은 QPainter가 모양을 디자인하거나 스타일을 지정하는 방법 인 QPainter의 작동을 정의
27
+#         https://likegeeks.com/wp-content/uploads/2019/01/04-1.png
28
+        painter.setPen(QPen(Qt.green,  8, Qt.DashLine))
29
+#         painter.setPen(QPen(Qt.green,  8, Qt.SolidLine))
30
+
31
+#         QBrush 모듈은 모양에 대한 색상, 질감 등을 제공
32
+#         https://likegeeks.com/wp-content/uploads/2019/01/07-1.png
33
+        painter.setBrush(QBrush(Qt.red, Qt.SolidPattern))
34
+        
35
+#         더 자세한 내용은 https://likegeeks.com/pyqt5-drawing-tutorial/
36
+        painter.drawEllipse(40, 40, 400, 400)
37
+        
38
+        
39
+App = QApplication(sys.argv)
40
+window = Window()
41
+sys.exit(App.exec())        

binární
src/kr/co/swh/lecture/python/pyqt5/drawing/family.jpg Zobrazit soubor


+ 3
- 0
src/kr/co/swh/lecture/python/pyqt5/drawing/family_data.txt Zobrazit soubor

@@ -0,0 +1,3 @@
1
+못생긴 하마
2
+귀여운 유키
3
+착한 레종

+ 41
- 0
src/kr/co/swh/lecture/python/pyqt5/drawing/mouse1.py Zobrazit soubor

@@ -0,0 +1,41 @@
1
+from PyQt5.QtWidgets import QMainWindow, QApplication, QDesktopWidget
2
+from PyQt5.QtCore import Qt
3
+import sys
4
+
5
+class Main(QMainWindow):
6
+    def __init__(self):
7
+        super().__init__()
8
+
9
+        self.setMouseTracking(True)
10
+
11
+        self.setWindowTitle('mouse')
12
+        self.resize(320, 240)
13
+        self.show()
14
+
15
+    def mouseButtonKind(self, buttons):
16
+        if buttons & Qt.LeftButton:
17
+            print('LEFT')
18
+        if buttons & Qt.MidButton:
19
+            print('MIDDLE')
20
+        if buttons & Qt.RightButton:
21
+            print('RIGHT')
22
+
23
+    def mousePressEvent(self, e):  # e ; QMouseEvent
24
+        print('BUTTON PRESS')
25
+        self.mouseButtonKind(e.buttons())
26
+
27
+    def mouseReleaseEvent(self, e):  # e ; QMouseEvent
28
+        print('BUTTON RELEASE')
29
+        self.mouseButtonKind(e.buttons())
30
+
31
+    def wheelEvent(self, e):  # e ; QWheelEvent
32
+        print('wheel')
33
+        print('(%d %d)' % (e.angleDelta().x(), e.angleDelta().y()))
34
+
35
+    def mouseMoveEvent(self, e):  # e ; QMouseEvent
36
+        print('(%d %d)' % (e.x(), e.y()))
37
+
38
+if __name__ == '__main__':
39
+    app = QApplication(sys.argv)
40
+    win = Main()
41
+    sys.exit(app.exec_())

+ 8
- 0
src/kr/co/swh/lecture/python/pyqt5/drawing/templates/image.html Zobrazit soubor

@@ -0,0 +1,8 @@
1
+<html>
2
+    <head>
3
+        <title>SWH Sample</title>
4
+    </head>
5
+    <body>
6
+        <img height="280" width="180" src="data:image/jpg;charset=utf-8;base64, {{image}}" />
7
+    </body>
8
+</html>