Kashirigi
5 years ago
4 changed files with 32 additions and 0 deletions
Binary file not shown.
@ -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() |
After Width: | Height: | Size: 45 KiB |
After Width: | Height: | Size: 35 KiB |
Loading…
Reference in new issue