I've got a perl program that grabs a screenshot as a png and inserts it into a variable:
my $png_data = $s->chrome->render_content(format => 'png');
Currently, I save the $png_data
straight to a disk and then crop the resulting file with Imagemagick. Then I load the file back into a variable which I write to the database as a BLOB.
This is obviously wasteful.
Rather than saving it to disk, and then reading it back off disk, I'd just like to crop it while in memory and then save to the database.
How can I accomplish this?
* UPDATE * Here is the solution I ended up going with, which took a bit of time to find and noodle out as I'm not very familiar with Imagemagick:
use Image::Magick;
# screenshot grabbed with WWW::Mechanize::Chrome;
# returned value of $png_data is a "blob" which can be saved in db or to file
my $png_data = $s->chrome->render_content(format => 'png');
# create object to process image
my $img = Image::Magick->new(magick=>'png');
# BlobToImage functions converts data to image that IM can process
$img->BlobToImage($png_data);
# do some processing of image
my $x = $img->Crop(geometry => "1028x5000+370+880");
print "$x" if "$x";
# now, the tricky part is saving the image by writing the $img back into a Perl string like this:
open (my $fh, '>', \$png_data) || die 'Could not write to string';
$img->Write(filename => $fh, quality => 100, depth => 4);
close $fh;
# The Write() method basically converts it back to a blob. Then you can store the $png_data string into the database in a BLOB column.
# NOTE: I'm not 100% sure if last line worked as I decided to save the image to a file. It seemed to work as I didn't get any errors but I did not verify that the data was actually in the database. But some variation of that last line should work if that one doesn't.