The database stores images in varbinary format. I am getting data like this b’xffxd8xffxe0x00x10JFIFx00x01x01x01x00x00
x00x00xffxe1x00ZExifx… I can save them if I use the code
JavaScript
x
12
12
1
photo_path = r'C:1' + '\'
2
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=DESKTOP-8EKCG28RUSGUARD;DATABASE=RUSGUARDDB;UID=sa;PWD=123')
3
cursor = cnxn.cursor()
4
cursor.execute("SELECT Photo FROM [RusGuardDB].[dbo].[EmployeePhoto]")
5
retrieved_bytes = cursor.execute("SELECT Photo FROM [RusGuardDB].[dbo].[EmployeePhoto]").fetchall()
6
cursor.close()
7
sum = numpy.array(retrieved_bytes)
8
for a in range(len(sum)):
9
sum1 = sum[a]
10
with open(photo_path + 'new' + str(a) + '.jpg', 'wb') as new_jpg:
11
new_jpg.write(sum1)
12
I don’t want to save pictures, i want show them directly in QLabel. How can i do this?
Advertisement
Answer
I finded solution for my problem.
JavaScript
1
7
1
retrieved_bytes = cursor.execute("SELECT Photo FROM [RusGuardDB].[dbo].[EmployeePhoto]").fetchone()
2
for row in retrieved_bytes:
3
row_to_list = [elem for elem in row]
4
pixmap = QPixmap()
5
pixmap.loadFromData(bytearray(row_to_list))
6
self.label1.setPixmap(pixmap)
7