diff --git a/Python/sqliteImages/imagetest.db b/Python/sqliteImages/imagetest.db new file mode 100644 index 0000000..84e71f8 Binary files /dev/null and b/Python/sqliteImages/imagetest.db differ diff --git a/Python/sqliteImages/sqliteImages.py b/Python/sqliteImages/sqliteImages.py new file mode 100644 index 0000000..bbf232d --- /dev/null +++ b/Python/sqliteImages/sqliteImages.py @@ -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() diff --git a/Python/sqliteImages/test-pattern.png b/Python/sqliteImages/test-pattern.png new file mode 100644 index 0000000..bbc2bb0 Binary files /dev/null and b/Python/sqliteImages/test-pattern.png differ diff --git a/Python/sqliteImages/test-pattern_converted.jpg b/Python/sqliteImages/test-pattern_converted.jpg new file mode 100644 index 0000000..7aee8e8 Binary files /dev/null and b/Python/sqliteImages/test-pattern_converted.jpg differ