[melbourne-pug] Inserting images into MSSQL using Python

Juan Nunez-Iglesias jni.soma at gmail.com
Mon Jan 20 06:03:52 CET 2014


Hey David,

Look at scikit-image <http://scikit-image.org> (API
documentation<http://scikit-image.org/docs/dev/api/api.html>).
You can read a png into a numpy array, which can be easily abstracted as a
(shape, data) tuple. (shape is itself a tuple, containing height and width.)

I don't know anything about databases, but assuming a write(db, row, col,
data) function that writes binary data to the db:

>>> from skimage import io
>>> im = io.imread('file.png')
>>> write(db, row, col, im.data)
>>> write(db, row, 'png-width', im.shape[0])
>>> write(db, row, 'png-height', im.shape[1])

Then, if you want to get the png back out again (and again assuming a
read(db, ...) function):

>>> import numpy as np
>>> data, width, height = read(db, row, col), read(db, row, 'png-width'),
read(db, row, 'png-height')
>>> im = np.array(data, dtype=np.uint8).reshape((width, height))
>>> io.imsave('file-out.png', im)

Note that your image may be 16-bit grayscale, in which case you'll need to
use dtype=np.uint16.

Hope that helps and that I'm not being too naive about the db procedures...

Juan.



On Mon, Jan 20, 2014 at 3:47 PM, David Crisp <dcrisp at netspace.net.au> wrote:

> Using Python, What is the best method for reading a small PNG file from
> disk then writing it to a MS SQL database (2008)   and then retrieiving the
> binary data and writing it out to disk as a valid png file.
>
> THis is for writing VERY small (700 byte) monocrhome PNG file to the
> database.
>
> The prefered Python MSSQL engine Im using is PYMSSQL.   its the one I have
> used for the rest of the text so its the one I need to use for the binary
> data.
>
> I have fiddled with the PIL / PILLOW library
>
> I think my problem is im looking in the wrong directions!
>
> How do other people do this?
> Regards,
> David
>
> _______________________________________________
> melbourne-pug mailing list
> melbourne-pug at python.org
> https://mail.python.org/mailman/listinfo/melbourne-pug
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/melbourne-pug/attachments/20140120/7bb0777e/attachment.html>


More information about the melbourne-pug mailing list