Inserting and retrieving images into mysql through python

前端 未结 2 1610
醉话见心
醉话见心 2021-01-05 16:42

I used this code to insert an image into mysql database and retrieve back the image. This code works perfectly with no errors , but the problem is even after inserting an im

相关标签:
2条回答
  • 2021-01-05 17:02
    import mysql.connector
    import sys
    from PIL import Image
    import base64
    import cStringIO
    import PIL.Image
    
    db = mysql.connector.connect(user='root', password='abhi',
                                  host='localhost',
                                  database='cbir')
    
    image = Image.open('C:\Users\Abhi\Desktop\cbir-p\images.jpg')
    blob_value = open('C:\Users\Abhi\Desktop\cbir-p\images.jpg', 'rb').read()
    sql = 'INSERT INTO img(images) VALUES(%s)'    
    args = (blob_value, )
    cursor=db.cursor()
    cursor.execute(sql,args)
    sql1='select * from img'
    db.commit()
    cursor.execute(sql1)
    data=cursor.fetchall()
    print type(data[0][0])
    file_like=cStringIO.StringIO(data[0][0])
    img=PIL.Image.open(file_like)
    img.show()
    
    db.close()
    

    This code works fine

    0 讨论(0)
  • 2021-01-05 17:16
    import mysql.connector
    import base64
    import io
    
    import PIL.Image
    with open('lemonyellow_logo.jpg', 'rb') as f:
        photo = f.read()
    encodestring = base64.b64encode(photo)
    db= mysql.connector.connect(user="root",password="lyncup",host="localhost",database="demo")
    mycursor=db.cursor()
    sql = "insert into sample values(%s)"
    mycursor.execute(sql,(encodestring,))
    db.commit()
    sql1="select * from sample"
    mycursor.execute(sql1)
    data = mycursor.fetchall()
    data1=base64.b64decode(data[0][0])
    file_like=io.BytesIO(data1)
    img=PIL.Image.open(file_like)
    img.show()
    db.close()
    
    0 讨论(0)
提交回复
热议问题