Last active
July 10, 2019 17:20
-
-
Save slarosa/f032b50833ff6aaabc95843d9287dcce to your computer and use it in GitHub Desktop.
Action for QGIS to show a stored image in a sqlite db
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from qgis.PyQt.QtCore import Qt | |
from qgis.PyQt.QtGui import QLabel, QImage, QPixmap | |
from qgis.utils import iface | |
import sqlite3 | |
UNIQUE_FIELD = "unique_field_name" | |
BLOB_FIELD = "picture_field_name" | |
FIELD_VALUE = '[%unique_field_name%]' | |
vl = iface.activeLayer() | |
pr = vl.dataProvider() | |
db = pr.dataSourceUri().split(' ')[0].replace('dbname=','').replace("'","") | |
conn = sqlite3.connect(db) | |
sql = "SELECT {0} FROM {1} WHERE {2}={3}".format(BLOB_FIELD, vl.name(), | |
UNIQUE_FIELD, FIELD_VALUE) | |
c = conn.cursor() | |
c.execute(sql) | |
rows = c.fetchone() | |
c.close() | |
conn.close() | |
qimg = QImage.fromData(rows[0]) | |
pixmap = QPixmap.fromImage(qimg) | |
label = QLabel() | |
h = label.height() | |
w = label.width() | |
label.setPixmap(pixmap.scaled(w,h,Qt.KeepAspectRatio)) | |
label.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What would be the catch for a postgresql database?