Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: GD colorAllocate not changing colour -- working example

by Discipulus (Abbot)
on May 16, 2021 at 19:05 UTC ( #11132676=note: print w/replies, xml ) Need Help??


in reply to Re^2: GD colorAllocate not changing colour
in thread GD colorAllocate not changing colour

Hello Bod,

letting apart the humor (which incidentally make your post harder to understand for non native english speakers.. :), you never set $resize background to white! I didnt follow all your geometry calculus, but be aware the the default background for a GD image is black:

perl -MGD -e"$im = new GD::Image(100,100);open $fh,'>','out.jpg';binmo +de $f; print $fh $im->jpeg"

The above print a nice black image.

Simplfying the whole I think you can start from something like the following code, which does what you are looking for

use strict; use warnings; use GD; GD::Image->trueColor(1); # gray image 100x100 my $im_gray = new GD::Image(100,100); my $gray = $im_gray->colorAllocate(127,127,127); $im_gray->filledRectangle(0, 0, 100,100, $gray); open my $fh_gray,'>','gray.jpg'; binmode $fh_gray; print $fh_gray $im_gray->jpeg; # red image 100x100 my $im_red = new GD::Image(100,100); my $red = $im_red->colorAllocate(255,0,0); my $black = $im_red->colorAllocate(0,0,0); $im_red->filledRectangle(0, 0, 100,100, $red); # with a black circle $im_red->ellipse(50,50,100,100,$black); open my $fh_red,'>','red.jpg'; binmode $fh_red; print $fh_red $im_red->jpeg; # copy resized red image scaled 50% and centered inside the gray one $im_gray->copyResized($im_red,25,25,0,0,50,50,100,100); open my $fh_both,'>','both.jpg'; binmode $fh_both; print $fh_both $im_gray->jpeg;

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^4: GD colorAllocate not changing colour -- working example
by Bod (Deacon) on May 21, 2021 at 21:13 UTC
    make your post harder to understand for non native english speakers

    Sorry - so many of the non-English natives on this site use such good English that it is very, very easy to forget it's not your native tongue.

    you never set $resize background to white!

    No I don't. Why would I?
    $resize contains the picture, not the background. Setting the background of the picture turns it white which is not what I want.

    Thanks for the working example. It reinforces that I am (probaly) approaching the problem the right way. However, when I plug in a picture instead of a pattern build in GD it reverts to not setting the background. It seems to be a problem with actual pictures. Connecting that with what afoken has suggested, it seems likely that it is to do with the colour palette of the picture.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11132676]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2021-09-24 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?