PyQt5로 이미지 파일 조회

이미지를 저장하고 이를 조회하기 위한 코드

import sqlite3

# -- 사진 데이터를 DB에 넣는 코드
# conn = sqlite3.connect('./image.db')

# c = conn.cursor()
# c.execute("CREATE TABLE IF NOT EXISTS users(myimage BLOB, content text )") #AUTOINCREMENT
# pict = './test.jpg'
# # with open(pict,'rb') as input_file:
# #     ablob=input_file.read()
# #     c.execute("INSERT INTO users VALUES(?,?)",(ablob,'mytest'))
# #     conn.commit()

#  c.execute("SELECT * FROM users WHERE content=?",('mytest',))
#  answer, cont = c.fetchone()

#  qimg = QtGui.QImage.fromData(answer)
#  pixmap = QtGui.QPixmap.fromImage(qimg)

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QIcon, QPixmap, QImage
from PyQt5 import QtCore

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 image - pythonspot.com'
        self.left = 10
        self.top = 10
        self.width = 640
        self.height = 480
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        conn = sqlite3.connect('./image.db')
        c = conn.cursor()
        c.execute("SELECT * FROM users WHERE content=?",('mytest',))

        answer, cont = c.fetchone()

        # 이미지를 라벨에 보여주는 코드
        qimg = QImage.fromData(answer)
        pixmap = QPixmap.fromImage(qimg)
        pixmap = pixmap.scaled(200,200, QtCore.Qt.IgnoreAspectRatio)
        print('size',pixmap.width())

        # Create widget
        label = QLabel(self)
        # pixmap = QPixmap('image.jpeg')
        label.setPixmap(pixmap)
        self.resize(pixmap.width(),pixmap.height())
        # self.resize(50,50)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    ex.show()
    sys.exit(app.exec_())

Tags:

Updated:

Leave a Comment