Created
April 4, 2019 15:04
-
-
Save epoz/627ab3699a2af61b9d79b7a743afdac0 to your computer and use it in GitHub Desktop.
Read a directory of images and convert them into a single sqlite3 db of thumbs
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
# sqlite> .schema | |
# CREATE TABLE thumbs(filename, data); | |
from PIL import Image | |
import os | |
from io import BytesIO | |
import sys | |
import sqlite3 | |
from progress.bar import Bar | |
db = sqlite3.connect('thumbs.db') | |
cursor = db.cursor() | |
PATH = sys.argv[1] | |
filenames = [x for x in os.listdir(PATH) if x.lower().endswith('.jpg')] | |
bar = Bar('Reading files', max=len(filenames)) | |
for x in filenames: | |
i = Image.open(os.path.join(PATH, x)) | |
size = 150, 150 | |
i.thumbnail(size) | |
b = BytesIO() | |
i.save(b, 'jpeg') | |
cursor.execute('INSERT INTO thumbs VALUES(?,?)', (x, b.getvalue())) | |
bar.next() | |
db.commit() | |
bar.finish() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment