Skip to content

Instantly share code, notes, and snippets.

@hal1932
Last active March 7, 2020 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hal1932/268382f982a8d7ce27b8388aec195040 to your computer and use it in GitHub Desktop.
Save hal1932/268382f982a8d7ce27b8388aec195040 to your computer and use it in GitHub Desktop.
import os
import sqlite3
from contextlib import closing
from PySide2.QtCore import QByteArray, QBuffer, QIODevice
from PySide2.QtGui import QImage
import timeit
source = QImage('test.png')
# print(timeit.timeit(lambda: source.save('testaaa.png'), number=100))
bits = source.constBits()
width = source.width()
height = source.height()
bytes_per_line = source.bytesPerLine()
format = source.format()
db_file = 'test2.db'
if os.path.isfile(db_file):
os.remove(db_file)
with closing(sqlite3.connect(db_file)) as conn:
cursor = conn.cursor()
cursor.execute('create table images (width int, height int, bytes_per_line int, format int, bytes blob)')
for i in range(100):
cursor.execute(
'insert into images (width, height, bytes_per_line, format, bytes) values (?, ?, ?, ?, ?)',
[width, height, bytes_per_line, int(format), bits])
conn.commit()
with closing(sqlite3.connect(db_file)) as conn:
cursor = conn.cursor()
def load():
for i, (width, height, bytes_per_line, format, bytes) in enumerate(cursor.execute('select width, height, bytes_per_line, format, bytes from images')):
image = QImage(bytes, width, height, bytes_per_line, QImage.Format(format))
# print(len(image.bits()), image.format())
image.save(f'test_out/test_out_{i}.png')
print(timeit.timeit(load, number=10))
# load()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment