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

Hello,

Probably a stupid error, but after 2 hours, am asking you :)

Want to copy a part of an image to a new image. The original image is 250x320 and I want to copy for example a rectangle that begins 40 pixels from the left, with the same height. So the new image will be 250-40=210 width and 320 high.

Current image is $source
New image is $dest


use GD
GD::Image->trueColor(1);
my $bildle = newFromJpeg GD::Image($source, 1) || die "could not open image.";
my $bild=newTrueColor GD::Image(210,320);
$bild->GD::Image::copy($bildle,0,0,40,0,210,320);
$bild->interlaced('true');
open(DATEI ,">$dest") || die "could not write image: $!";
binmode DATEI;
print DATEI ($bild->jpeg($jpeg_quality));
close(DATEI);


It creates and saves a pic that is 210x320 but there all black, with quite nothing inside... I tried to change the size data, same result.


Thank you for your help!

Replies are listed 'Best First'.
Re: Perl GD imagecopy
by BrowserUk (Patriarch) on Dec 28, 2010 at 08:28 UTC

    If $jpeg_quality doesn't exist, you are effectively asking for zero quality output. If the copied section of source image has little colour or detail, that could result in poor, perhaps even apparently black output.

    Use the default or set $jpeg_quality = 100 to see a big difference.

    quality 0 versus quality 100.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Voila! Stupid error as I thought, moreover the $jpeg_quality parameter was set in my other script! Thanks a lot :)

        use strict; would have caught this error, or at least forced you to declare $jpeg_quality, and then hopefully you would have thought about giving it a value too.