Use python to output image from cx_oracle blob


Question

I have stored images in a oracle table within a BLOB column. I use to read and output an image and write data using JAVA. I would like to do the same (getting my image and distributing it) with python. I am using Flask framework and cx_Oracle.

I manage to get my BLOB content into my application but I am not sure how to generate an image from that.

I know that in Java i used :

OutputStream out = response.getOutputStream();
response.setContentType(doc.getContentType());
IOUtils.copy( new ByteArrayInputStream(doc.getContent()),out);
out.flush();

where doc.getContent() is my BLOB content.

1
2
1/16/2012 3:14:25 AM

Using Flask and your help :

@app.route('/_photo/')
def gripur():

    conn = cx_Oracle.connect("*****","****",make_dsn_tns(get_config_string()))
    curs = conn.cursor()
    #find photo
    document=curs.execute('select myblob from mytable where id=34234')
    row = cursor.fetchone()
    imageBlob = row[0]

    blob= imageBlob.read()
    response = make_response(blob)
    response.headers["Content-type"] = "image/jpeg"
    conn.close()

    return response
2
6/17/2013 1:30:50 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon