Displaying images stored as binary in MySQL

B

Blaktyger

How can this be done? Images are stored in a LONGBLOB field. When I
try to display them, it prints out the binary data as it is...

Thank you
 
B

Benjamin Niemann

Blaktyger said:
How can this be done? Images are stored in a LONGBLOB field. When I
try to display them, it prints out the binary data as it is...
Display where? On local screen, in a webbrowser?
Assuming the first one, see post by Tom.
Assuming the second one (you're writing a cgi,mod_python... program),
did you set the content-type in the http header correctly?
 
B

Blaktyger

Benjamin Niemann said:
Display where? On local screen, in a webbrowser?
Assuming the first one, see post by Tom.
Assuming the second one (you're writing a cgi,mod_python... program),
did you set the content-type in the http header correctly?
In a webbrowser...
this is my code...

#!/usr/bin/python2.3

print 'Content-Type:image/jpeg\n\n'
import blakHtml, sys
sys.stderr = sys.stdout
import MySQLdb
import urllib
import cgi
import time
prevPg=blakHtml.HTMLDocument()
newConnection=MySQLdb.connect(host='localhost', user='blaktyger', \
passwd='linux4ever', db='photoFamille')
curseur=newConnection.cursor()

sqlCmdFr="""SELECT * FROM photo"""
curseur.execute(sqlCmdFr)
resultats = curseur.fetchall()
p=0
for val in resultats:
p=p+1
#print '<img src=" ' + val[5]+ ' " ' +' >' THIS DOES NOT WORK
print val[5]
newConnection.close()
I know there is no HTML code but this is just a test...
 
B

Benjamin Niemann

How can this be done? Images are stored in a LONGBLOB field. When I
In a webbrowser...
this is my code...

#!/usr/bin/python2.3

print 'Content-Type:image/jpeg\n\n'
You must use '\r\n' to terminate header lines. I'm not really sure, but
maybe there should also be a spacing between ':' and 'image/jpeg'...
If you are using e.g. FireFox as a browser, you can user 'View Page
Info' to see, if the browser correctly understands, what you are trying
to tell it ('Type' should say 'image/jpeg').
import blakHtml, sys
sys.stderr = sys.stdout
import MySQLdb
import urllib
import cgi
import time
prevPg=blakHtml.HTMLDocument()
newConnection=MySQLdb.connect(host='localhost', user='blaktyger', \
passwd='linux4ever', db='photoFamille')
curseur=newConnection.cursor()

sqlCmdFr="""SELECT * FROM photo"""
curseur.execute(sqlCmdFr)
resultats = curseur.fetchall()
p=0
for val in resultats:
p=p+1
#print '<img src=" ' + val[5]+ ' " ' +' >' THIS DOES NOT WORK
print val[5]
newConnection.close()
I know there is no HTML code but this is just a test...
val[5] contains the raw image data (not a path), correct? Does 'photo'
contains more than one image? Concatenating several images into one
bytestream, is definitly not what you want.
 
B

Blaktyger

Benjamin Niemann said:
In a webbrowser...
this is my code...

#!/usr/bin/python2.3

print 'Content-Type:image/jpeg\n\n'
You must use '\r\n' to terminate header lines. I'm not really sure, but
maybe there should also be a spacing between ':' and 'image/jpeg'...
If you are using e.g. FireFox as a browser, you can user 'View Page
Info' to see, if the browser correctly understands, what you are trying
to tell it ('Type' should say 'image/jpeg').
import blakHtml, sys
sys.stderr = sys.stdout
import MySQLdb
import urllib
import cgi
import time
prevPg=blakHtml.HTMLDocument()
newConnection=MySQLdb.connect(host='localhost', user='blaktyger', \
passwd='****', db='photoFamille')
curseur=newConnection.cursor()

sqlCmdFr="""SELECT * FROM photo"""
curseur.execute(sqlCmdFr)
resultats = curseur.fetchall()
p=0
for val in resultats:
p=p+1
#print '<img src=" ' + val[5]+ ' " ' +' >' THIS DOES NOT WORK
print val[5]
newConnection.close()
I know there is no HTML code but this is just a test...
val[5] contains the raw image data (not a path), correct? Does 'photo'
contains more than one image? Concatenating several images into one
bytestream, is definitly not what you want.

val[5] contains only one image but I saw that when printing out val[5]
is that the binary data is in a array. So this i what I did...

import array
[same code as before except for this... ]

print 'Content-Type: image/jpeg\r\n'
[...]
for val in resultats:
p=p+1
img=''
t=''
# t is an array object. the tostring() argument extracts the
binary data
t=val[5].tostring()
print t
and it works!
Thanks for your help Ben!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top