Browse Source

Added sqlite3 image storage with Pillow

master
Kashirigi 5 years ago
parent
commit
8a06aa7297
  1. BIN
      Python/sqliteImages/imagetest.db
  2. 32
      Python/sqliteImages/sqliteImages.py
  3. BIN
      Python/sqliteImages/test-pattern.png
  4. BIN
      Python/sqliteImages/test-pattern_converted.jpg

BIN
Python/sqliteImages/imagetest.db

Binary file not shown.

32
Python/sqliteImages/sqliteImages.py

@ -0,0 +1,32 @@ @@ -0,0 +1,32 @@
'''Images to and from sqlite'''
import io, sqlite3
from PIL import Image
#How to open a PIL image from a photostream
with open('test-pattern.png', 'rb') as x:
foto = x.read()
fotostream = io.BytesIO(foto)
image = Image.open(fotostream)
#And now for sqlite3
conn = sqlite3.Connection('imagetest.db')
cursor = conn.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS images ("name" TEXT, "file" BLOB);')
#make sure you insert the bytes from foto, not the io object
cursor.execute('INSERT INTO images VALUES (?,?)', ('testimage', foto))
#retrieval
cursor.execute('SELECT file from images')
f2stream = io.BytesIO(cursor.fetchone()[0]) #fetchone returns a tuple
f2img = Image.open(f2stream)
f2img = f2img.convert('RGB') # original is RGBA and generates an error
#see https://github.com/python-pillow/Pillow/issues/2609
f2img.save('test-pattern_converted.jpg')
#and save that data
conn.commit()
conn.close()

BIN
Python/sqliteImages/test-pattern.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

BIN
Python/sqliteImages/test-pattern_converted.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Loading…
Cancel
Save