Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Merging png by using GD.

by birdy (Initiate)
on Mar 29, 2011 at 18:21 UTC ( #896244=perlquestion: print w/ replies, xml ) Need Help??
birdy has asked for the wisdom of the Perl Monks concerning the following question:

Hello everybody. I need your help with GD::Graph::bars that i am using on a code. I am doing something like that:
...$copy = $myimage->clone; $myimage -> copyMerge($copy,100,100,100,100,100,100,100); open(PICTURE, ">$GraphTempDir/$GeneName.png") or d +ie("Cannot open file for writing"); binmode PICTURE; print PICTURE $myimage->png; print "[<a href=../stavros/$ArrayFile[6]/$GeneName +.png TARGET=new>$GeneName</a>]"; close PICTURE;
and according to what i read on cpan i will be expecting to see on $GeneName.png the two pngs ($copy,$myimage).Probably the two pngs should be the one next to the other on the final $GeneName.png. Am i right?Because my point is to merge the two pngs $copy,$myimage the one next to other on a final $GeneName.png. Really many many thanks in advance.

Comment on Merging png by using GD.
Download Code
Re: Merging png by using GD.
by BrowserUk (Pope) on Mar 29, 2011 at 18:55 UTC
    • You start with an image: $myimage
    • You clone that image into another image: $copy
    • You then 'merge' (using 100%, which makes it a copy operation)
    • a 100x100 rectangle starting a offset x:100 y:100 of the clone
    • to an offset of x:100 y:100 in the original image.

    Net result: an expensive noop. You copied a 100x100 rectangle from a copy of the image back to the original in exactly the same place.


    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.
      Thanks for replying. > You start with an image: $myimage > You clone that image into another image: $copy The only reason that i clone that is to take the finally $genename.png with two png the one next to other.When i succeed on the finnaly $genename.png i will replace $copy with another image according to my goal on the script. Also i have tried to many values (such as $myimage->copyMerge($copy,10,10,0,0,25,25,50)) in order to take the two images, the one next to the other, as one.Any other ideas?

        It sounds to me like you are trying to create one large image that contains several smaller ones.

        The process to do that should be fairly simple:

        1. Create a large empty GD Image that is big enough to contain all your smaller images. You might want to set the background colour at this point.
        2. Load each image in turn, and paste it into your big image at the correct location.
        3. Save the big image in your prefered file format.

        Overall your code might look something like this: (Untested)

        use strict; use warnings; use GD; my $outImage = GD::Image->new(1000,1000); # Assume this image is 500x500 pixels my $imageTopLeft = GD::Image->newFromPng('/path/to/file/top/left.png') +; $outImage->copyResized($imageTopLeft, # Src Image; 0,0, # $dstX,$dstY, 0,0, # $srcX,$srcY, 500, 500, # $destW,$destH, 500, 500 ); # $srcW,$srcH, # Assume this image is 200x300 pixels my $imageBottomRight = GD::Image->newFromPng('/path/to/file/bottom/rig +ht.png'); $outImage->copyResized($imageBottomRight, # Src Image; 800,700, # $dstX,$dstY, 0,0, # $srcX,$srcY, 200, 300, # $destW,$destH, 200, 300 ); # $srcW,$srcH, open OUTIMAGE, ">", '/path/to/output/filename.png' or die "Error, unab +le to write output $!"; binmode OUTIMAGE; print OUTIMAGE $outImage->png; close OUTIMAGE;

        Given the names of two image files as arguments, this script will create a third image with the two side by side:

        #! perl -slw use strict; use GD; my $im1 = GD::Image->new( $ARGV[ 0 ] ) or die; my $im2 = GD::Image->new( $ARGV[ 1 ] ) or die; my( $x1, $y1 ) = $im1->getBounds(); my( $x2, $y2 ) = $im2->getBounds(); my( $x3, $y3 ) = ( $x1 + $x2, $y1 > $y2 ? $y1 : $y2 ); my $im3 = GD::Image->new( $x3, $y3, 1 ); $im3->copy( $im1, 0, 0, 0, 0, $x1, $y1 ); $im3->copy( $im2, $x1, 0, 0, 0, $x2, $y2 ); open PNG, '>:raw', 'new.png' or die $!; print PNG $im3->png; close PNG; system 'new.png';;

        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://896244]
Approved by Sinistral
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-09-21 16:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (172 votes), past polls