Serene Hacker has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks!

I’ve been away from perl (and programming in general) for a looong time — so I’m a bit rusty :-(

I have an SQLite database containing jpg blobs.

I’d like to extract them and then pass each jpg data blob (one at a time) into Image::Epeg to create thumbnails.

Unfortunately, Image::Epeg-new(\$jpg_data); isn’t working. (It works ok when reading from a file, but not from a “data ref”).

What I’d like to avoid is extracting the blob to a scalar, writing that to disk and then opening the file from Epeg. Is it possible to extract into a memory-mapped file (one created in a virtual file system) directly? Or something like that?

Replies are listed 'Best First'.
Re: Extracting a blob from DBI to a buffer
by choroba (Cardinal) on Oct 30, 2019 at 18:12 UTC
    What do you mean by "isn't working"? The source seems to support data references.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Extracting a blob from DBI to a buffer
by haj (Vicar) on Oct 30, 2019 at 19:30 UTC
    Unfortunately, Image::Epeg-new(\$jpg_data); isn’t working.
    What makes you write that? I'm too lazy to whip up a database, but the following code pulls a JPEG image into a scalar and thumbnails it successfully.
    use 5.020; use warnings; use Image::Epeg qw(:constants); use LWP::Simple; # Copyright notice for the image: # By Randal Schwartz from Portland, OR, USA - Flickr, CC BY-SA 2.0, # https://commons.wikimedia.org/w/index.php?curid=2938351 my $image = get("https://upload.wikimedia.org/wikipedia/commons/b/b3/L +arry_Wall_YAPC_2007.jpg"); die "Failed to load the image" unless defined $image; my $epeg = Image::Epeg->new(\$image); $epeg->resize(150,150,MAINTAIN_ASPECT_RATIO); $epeg->write_file("thumbnail.jpg");
    Side note: You need to have LWP::Protocol::https installed for this code.
      Thanks everyone!

      OK, Image::Epeg-new(\$jpg_data); does work. The $epg->resize function fails occasionally -- even though they're valid jpegs and larger than the crop size. (And the call is more likely to fail, the larger the crop size). And $epg->get_data occasionally fails (sticking exactly 64k zeroes at the beginning of the data, and then ever larger amounts of junk (?) on the end) -- even though resize returned 1.

      There's not much point in posting code yet. I'm still trying to figure out if there's anything wrong with the input files. Or whether there's something wrong with my installation -- or even the hardware! Anyway -- whatever's going on, it has nothing to do with extracting a blob from DBI. :-)

      I'm running perl 5.28.2 on Mac OS X 10.14.6 with MacPorts. I recently re-installed Xcode (and the command line tools, and MacPorts and Perl etc.) -- a software update to Xcode had changed some directories to from 10.14 to 10.15 (!) Some builds still fail (arm-none-eabi-gdb in MacPorts, and Image::Magick in CPAN) soooo, not sure what (if anything) is wrong...If I manage to narrow things down to a nice test I'll start a different thread.

Re: Extracting a blob from DBI to a buffer
by jcb (Parson) on Oct 31, 2019 at 01:52 UTC

    Study the documentation carefully; I remember some gotchas around SQLite's blob handling and possible confusion with strings. (In other words, are you actually passing the correct JPEG stream in?)

Re: Extracting a blob from DBI to a buffer
by GrandFather (Saint) on Oct 30, 2019 at 20:03 UTC

    Show us some sample code that demonstrates the problem. Note that the code can create and populate a trivial database with something easy to verify that probably isn't an image blob.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond