473,416 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Corrupted images after attempting to store PNG images as BLOBs inMySQL?

Hi all,

I've run into a strange error while trying to store some PNG images in
a MySQL database using MySQLdb. When I try to insert smaller images (<
64kb or so) everything seems to work fine. When I start trying to
insert larger images (~150kb), however, the images get corrupted along
the way.

The result is that only part of the image, e.g. the top 30% of the
image, is stored in the database, and the rest is simply transparent.
Furthermore, if I attempt to view the image in mysql-query-browser, it
does not display and simply states "Cannot display as image data,"
which seems to further suggest the idea that the data is being
corrupted somewhere along the way.

To store the image I'm using:

blob = open(img, 'rb').read()
sql = "INSERT INTO table VALUES('%s')" % (MySQLdb.escape_string(blob))

Anyone have any ideas?

Thanks,
Keith
Aug 13 '08 #1
6 5076
Keith Hughitt wrote:
I've run into a strange error while trying to store some PNG images in
a MySQL database using MySQLdb. When I try to insert smaller images (<
64kb or so) everything seems to work fine. When I start trying to
insert larger images (~150kb), however, the images get corrupted along
the way.

The result is that only part of the image, e.g. the top 30% of the
image, is stored in the database, and the rest is simply transparent.
Furthermore, if I attempt to view the image in mysql-query-browser, it
does not display and simply states "Cannot display as image data,"
which seems to further suggest the idea that the data is being
corrupted somewhere along the way.

To store the image I'm using:

blob = open(img, 'rb').read()
sql = "INSERT INTO table VALUES('%s')" % (MySQLdb.escape_string(blob))
Ouch. Please use parameters instead of explicit escapes and string
formatting; Python's not PHP.
Anyone have any ideas?
Silently truncating or otherwise mangling columns is a standard MySQL
feature. What does the table definition look like?

</F>

Aug 13 '08 #2
Fredrik Lundh wrote:
Silently truncating or otherwise mangling columns is a standard MySQL
feature. What does the table definition look like?
Oh, you did write BLOB in the subject. BLOB columns hold 64k (minus 2
bytes for housekeeping), and excess data is discarded, by default:

"If strict SQL mode is not enabled and you assign a value to a BLOB or
TEXT column that exceeds the column's maximum length, the value is
truncated to fit and a warning is generated."

http://dev.mysql.com/doc/refman/5.0/en/blob.html

Are you sure you *need* to use MySQL ;-)

</F>

Aug 13 '08 #3
Thanks Fredrik.

That certainly explains things :) I also appreciate the suggestion on
coding guidelines: I'm
still becoming familiar with python. Originally we were not using the
database to store images,
but we started testing out storing images there as well as meta-data.
We may end up switching back
though for efficiency. For now though I think I will try mediumblob to
at least see if I can fix
the problem as things are.

Thanks and take care!
Keith

On Aug 13, 10:46 am, Fredrik Lundh <fred...@pythonware.comwrote:
Fredrik Lundh wrote:
Silently truncating or otherwise mangling columns is a standard MySQL
feature. What does the table definition look like?

Oh, you did write BLOB in the subject. BLOB columns hold 64k (minus 2
bytes for housekeeping), and excess data is discarded, by default:

"If strict SQL mode is not enabled and you assign a value to a BLOB or
TEXT column that exceeds the column's maximum length, the value is
truncated to fit and a warning is generated."

http://dev.mysql.com/doc/refman/5.0/en/blob.html

Are you sure you *need* to use MySQL ;-)

</F>
Aug 13 '08 #4
Keith,
still becoming familiar with python. Originally we were not using the
database to store images,
but we started testing out storing images there as well as meta-data.
just a remark: I am using PostgreSQL to store BLOB-Data as there are
"Images", "PDFs", "Microsoft Office Files".

The system (application, network, database) is working quite okay up
to around 10 Megabytes per file. And that for around 7 years now.

The database itself comfortably works with up to 1Gig per object, just
the database drivers / network stack / bandwith / users patiance for
answers really gets... unreliable above 10 Meg per file.
Best wishes,

Harald

Aug 13 '08 #5
*** Fredrik Lundh escribió/wrote (Wed, 13 Aug 2008 16:46:04 +0200):
Oh, you did write BLOB in the subject. BLOB columns hold 64k (minus 2
bytes for housekeeping), and excess data is discarded, by default:

"If strict SQL mode is not enabled and you assign a value to a BLOB or
TEXT column that exceeds the column's maximum length, the value is
truncated to fit and a warning is generated."

http://dev.mysql.com/doc/refman/5.0/en/blob.html
Apart from this, MySQL features another delightful data truncator called
the "max_allowed_packet" server parameter:

http://dev.mysql.com/doc/refman/5.0/...too-large.html
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor en cubitos: http://www.demogracia.com
--
Aug 13 '08 #6
In article <ma*************************************@python.or g>,
Fredrik Lundh <fr*****@pythonware.comwrote:
>
Ouch. Please use parameters instead of explicit escapes and string
formatting; Python's not PHP.
How would you recommend upgrading an application that is more than ten
years old and contains something like 100K lines of code?
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

Adopt A Process -- stop killing all your children!
Aug 14 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mat | last post by:
Hi, I have a system that uploads images as BLOBs in a database. I also have a function that I use to resize uploaded images before saving as files. I would like to combine these two by resising...
4
by: yawnmoth | last post by:
i have a script that accesses images on a fairly frequent basis, however, i'm thinking that it would be better to have them in an sql database, instead, for the same reason that it is generally...
7
by: Benoit St-Jean | last post by:
I am looking at options/ways to store 12 million gif/jpg images in a database. Either we store a link to the file or we store the image itself in the database. Images will range from 4k to 35k in...
7
by: gemel | last post by:
I am developing an application that uses SQL 2000 as my source of images. I have successfully created the code to load the images onto the SQL Server and also to retrieve the images into a dataset....
16
by: Michal Hlavac | last post by:
Hello, I am working on web portal. There are some ads. We have about 200 000 ads. Every ad have own directory called ID, where is 5 subdirectories with various sizes of 5 images. Filesystem...
3
by: meyvn77 | last post by:
Hello - I am looking for the best way to store images in a Access DB. My Idea - I have a table with 150,000 records. These recoreds represent a Crash (Traffic Accident). I have 50 different...
10
by: eholz1 | last post by:
Hello Members, I am setting up a photo website. I have decided to use PHP and MySQL. I can load jpeg files into the table (medium blob, or even longtext) and get the image(s) to display without...
1
by: LongJohnBaldry | last post by:
My database is bloated, and it's not the beer. The users wanted pictures on their forms, so I set up some OLE object fields and plonked them on the forms. Lo and behold the users went mad pasting jpg...
7
by: Keith Hughitt | last post by:
Hi all, I am having trouble preloading images in a javascript application, and was wondering if anyone had any suggestions. Basically I have a bunch of images stored in a database as BLOBs. At...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.