Skip to content

Instantly share code, notes, and snippets.

@epoz
Created April 4, 2019 15:04
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 epoz/627ab3699a2af61b9d79b7a743afdac0 to your computer and use it in GitHub Desktop.
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
# 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