Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

resampling an image using GD not working on Linux

by Discipulus (Canon)
on Dec 07, 2016 at 09:36 UTC ( [id://1177374]=perlquestion: print w/replies, xml ) Need Help??

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

Hello estimated monks!

I recently released picwoodpecker which latest version with a minor fix is on github.

It seems the program does not run well on Linux (it was developped and tested on win7 only).

With a bit of pain i'v set up a Linux Ubuntu 16.04 LTS 32 bit as a virtual machine using virtualbox.

The problem arise when the application try to use the copyResampled GD method:

GD Warning: one parameter to a memory allocation multiplication is neg +ative or zero, failing operation gracefully line 618

The Ubuntu machine has perl 5.22 and GD version is 2.53

I've tryed to reduce the problem to the simplest one and I ended with the following:

use strict; use warnings; use GD; #UPDATE: need the following line to have a real 'smooth' resampling GD::Image->trueColor(1); my $file = $ARGV[0]; die "Please feed a jpg file.." unless -e $file; my $orig_gd = GD::Image->new($file); my $photo_ratio = 0.3; my $small_w = int($orig_gd->width * $photo_ratio); my $small_h = int($orig_gd->height * $photo_ratio); draw_photo ($file); ###################################################################### +########## sub draw_photo { my $file_path = shift; # create the resized but still empty GD image my $resized = GD::Image->new($small_w,$small_h); # copy from source into resized on $resized->copyResampled($orig_gd,0,0,0,0, $small_w, $small_h, $orig_gd->width, $orig_gd->height); # save open my $out, '>', time.'.jpg' or die "unable to open for write"; binmode $out; print $out $resized->jpeg or die "unable to write jpg data!"; close $out; }

The above runs fine on my win7 machine (perl 5.14 GD 2.46) and creates a resampled image.

It also creates a resampled image on Ubuntu so it does not complains about the zeros! So i'm stucked and seek for your wisdom.

The relevant part of the original code that fails on Linux is the following:

# @files is ArrayOfArray # each element contains pic data as follow: # 0 path # 1 x # 2 y # 3 orientation # 4 datetime joined with underscores # 5 GD object of THUMB # 6 [ GD object of PHOTO] # the last field [6] will be filled only for current file ( which inde +x is hold in $ph_index) # and for elelments to be preloaded: from ($ph_index - $preload) to ( +$ph_index + $preload) # thumb data [5] will be empty if $nothumbs is defined via -nothumbs c +ommandline switch ###################################################################### +########## sub draw_photo { my $ph_index = shift; print "\tdraw_photo got:\n\t",(join '|',map{defined $_ ? $_ : 'undef +'} (@{$files[$ph_index]}[0..4], $files[$ph_index]->[5]?'THUMB':'NO DATA', $files[$ph_index]->[6]?'PHOTO':'NO DATA', )),"\n" if $debug; $tk_ph_image->delete if $tk_ph_image->blank; # some tk stuff removed my $small_w = int($files[$ph_index]->[1] * $ph_ratio); my $small_h = int($files[$ph_index]->[2] * $ph_ratio); # create the resized but still empty GD image my $resized = GD::Image->new($small_w,$small_h); # copy from source into resized on # NOTE $files[$ph_index]->[6] containf the GD object $resized->copyResampled($files[$ph_index]->[6],0,0,0,0, $small_w, $small_h, $files[$ph_index]->[6]->width, $files[$ph_index]->[6]->height); $tk_ph_image->configure( -file => undef, -data => MIME::Base64::encode($resized->jpe +g()) ); # configure the Tk::Label to use the Tk::Photo as image $photo_label->configure(-image => $tk_ph_image ); # some display and tk stuff removed } ###################################################################### +##########

Thanks for your patience

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re: resampling an image using GD not working on Linux
by stefbv (Curate) on Dec 07, 2016 at 12:39 UTC

    I played a little with the program (from GitHub) and the warning is printed when one of the parameters is 0 (zero).

    Considering files[0] .../picwoodpecker/2016_..._0.jpg 568 0 GD Warning: one parameter to a memory allocation multiplication is neg +ative or zero, failing operation gracefully

    The first parameter number is '$small_w' and the second is '$small_h', from the code line:

    my $resized = GD::Image->new($small_w,$small_h);

    Update: made a clarification.

    Best regards, Stefan

      thanks stefbv for the feedback!

      If I understand you added a print for field 1 and 2 of the current photo and you got a zero for the heigth?

      This is probably a big design error from my part: I get width and heigth of the original pic from exif tags. Tags are read during the build list phase: i probably need to add a fall back method (using GD) to get anyway sizes if exif tags ones are not found.

      If using the same photo you launch the program with the -debug flag you also view the current photo array dumped to the screen as received by the draw_photo sub.

      In the following output the photo shows a width of 2560 and an heigth of 1920: you get a zero with your photo?

      Considering files[0] C:\ulisse\img-renamer\2008_09_22_17_56_21_12.jpg photo data NOT defined for files[0] filled photo data for current: files[0] HERE draw_photo got: ------> C:\ulisse\path\photo.jpg|2560|1920|undef|undef|THUMB|PHOTO current 0 preload [1] filled files[1] photo data zero or other non significant increment for nex_pic OK loaded 2 photo data for files[0,1]

      I added an issue to the github project.

      While i still have many problems (no photos displayed) on Ubuntu I setup a Centos with perl 5.10 and GD 2.44 picwoodpecker runs quite well..

      thanks again for your time and sorry for the late reply (here is big holiday weekend)!

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
        If I understand you added a print for field 1 and 2 of the current photo and you got a zero for the heigth?

        Yes, that is what I did.

        Forgot to mention that my OS is Slackware64 and GD is v2.56. With the '-debug' flag, the output is:

        Considering files[0] /home/.../picwoodpecker/2016_11_11_12_50_13_0.jpg photo data NOT defined for files[0] filled photo data for current: files[0] draw_photo got: /home/.../picwoodpecker/2016_11_11_12_50_13_0.jpg|2272|1704|un +def|undef|THUMB|PHOTO GD Warning: one parameter to a memory allocation multiplication is neg +ative or zero, failing operation gracefully

        Regards, Stefan

Re: resampling an image using GD not working on Linux
by 1nickt (Canon) on Dec 07, 2016 at 13:19 UTC

    Maybe you just need

    my $resized = GD::Image->new($small_w || 1, $small_h || 1);
    ?


    The way forward always starts with a minimal test.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1177374]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-24 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found