使用python向cassandra插入图像

2024-06-17 15:59:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新来的卡桑德拉和Python。我尝试使用python在cassandra的表中插入图像。我想看到我插入的图像和从数据库检索后的相同图像。在

import cassandra; 
print cassandra.__version__  #3.4.1
from cassandra.cluster import Cluster
cluster = Cluster(['127.0.0.1:9042']) # TEST CLUSTER 127.0.0.1:9042

session = cluster.connect('prueba')

#Open the image file on lecture and binary mode 
file = open('gato.jpg', 'rb') #file = open('gato.jpg', 'r')
print file
print type(file) #file

imagen_gato = file.read()
print type(imagen_gato) #str

#r_data = binascii.unhexlify(imagen_gato.strip())
#print type(r_data) 

stream = io.BytesIO(imagen_gato) 
print type(stream) #<type '_io.BytesIO'>

我看这个形象很正确

^{pr2}$

我尝试在casandra表中插入文件并检索它(错误如下)

#guardamos dicho archivo en cassandra
print 'Last Successfull print'
session.execute(  #ON THIS LINE IS THE ERROR
    """
    INSERT INTO fotos (id_foto, imagen )
    VALUES (%s, %s)
    """,
    (1,imagen_gato)
)
print 'Success'


#imprimimos dicho archivo desde casandra
print 'retrive'
rows = session.execute('SELECT id_foto, imagen FROM fotos')
for foto_row in rows:
    #print foto_row.id_foto, foto_row.imagen
    file2 = foto_row.imagen
print 'Success2'

然后我尝试再次打印图像

#This creates the main window of an application
window = tk.Tk()
window.title("Join2")
window.geometry("300x300")
window.configure(background='grey')

#path = 'gato.jpg'

#Creates a Tkinter-compatible photo image, which can be used everywhere     Tkinter expects an image object.
img = ImageTk.PhotoImage(Image.open(file2))

#The Label widget is a standard Tkinter widget used to display a text or image on the screen.
panel = tk.Label(window, image = img)
#The Pack geometry manager packs widgets in rows or columns.
panel.pack(side = "bottom", fill = "both", expand = "yes")
#Start the GUI
window.mainloop()

但我有个错误

d4b4a173636c461a8911bb30cdcd9041aa7d4c8950f4239645bf335afe19fe55fc6ff008d31216f3
3066a6e3b8cb82727e2e72cd4434314266e453f73ffd9272c200909312c20277573756172696f203
127290a09'; java.nio.charset.MalformedInputException: Input length = 1">

我不知道怎么做,也不知道为什么会有错误。我认为这是图像的格式,卡桑德拉拿的是blob


Tags: the图像imagesessiontypeopenwindowfile