Skip to content

Instantly share code, notes, and snippets.

@hal1932
Last active March 7, 2020 19:34
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/42e8c9403d8a0ab1d5b422f38f11f695 to your computer and use it in GitHub Desktop.
Save hal1932/42e8c9403d8a0ab1d5b422f38f11f695 to your computer and use it in GitHub Desktop.
import os
import sqlite3
from contextlib import closing
from PySide2.QtCore import QByteArray, QBuffer, QIODevice, QFile
from PySide2.QtGui import QImage
import timeit
source = QImage('test.png')
# print(timeit.timeit(lambda: source.save('testaaa.png'), number=100))
image_format = 'PNG'
bits = QByteArray()
buffer = QBuffer(bits)
buffer.open(QIODevice.WriteOnly)
source.save(buffer, image_format)
print(len(bits.data()))
f = QFile('test111.png')
f.open(QIODevice.WriteOnly)
f.write(bits)
f.close()
db_file = 'test1.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 (format text, bytes blob)')
for i in range(100):
cursor.execute('insert into images (format, bytes) values (?, ?)', [image_format, bits.data()])
conn.commit()
with closing(sqlite3.connect(db_file)) as conn:
cursor = conn.cursor()
def load():
for i, (format, blob) in enumerate(cursor.execute('select format, bytes from images')):
image = QImage()
image.loadFromData(QByteArray(blob), 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