Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

ChatGPT's solution (was: Re: Create email tracking image)

by Bod (Parson)
on Mar 21, 2023 at 22:56 UTC ( [id://11151112]=note: print w/replies, xml ) Need Help??


in reply to Create email tracking image

Just for fun I asked ChatGPT how do do this using the prompt: "What is the best way to create a 1x1 transparent image in Perl for use in email tracking?"

It came up with this solution:

use GD; # Create a new transparent image my $image = GD::Image->newTrueColor(1, 1); $image->alphaBlending(0); $image->saveAlpha(1); my $transparent = $image->colorAllocateAlpha(0, 0, 0, 127); $image->transparent($transparent); # Output the image print "Content-Type: image/png\n\n"; print $image->png;
And then went on to explain how it worked...

I prefer the solution put forward by afoken in Re: Create email tracking image

Replies are listed 'Best First'.
Re: ChatGPT's solution (was: Re: Create email tracking image)
by Anonymous Monk on Mar 22, 2023 at 23:45 UTC
    And then went on to explain how it worked...

    How very amazing. But what about absolutely irrelevant fact, that image produced is NOT transparent, bloody damn it, but 100% opaque? Should have been either:

    my $image = GD::Image->new(1, 1); my $transparent = $image->colorAllocate(0, 0, 0); $image->transparent($transparent);

    or:

    my $image = GD::Image->newTrueColor(1, 1); $image->alphaBlending(0); $image->saveAlpha(1); my $transparent = $image->colorAllocateAlpha(0, 0, 0, 127); $image->setPixel(0, 0, $transparent);

    What a sad scam. And yeah, I feel so very stupid to have to argue with bot. Bright future ahead.

      Thanks for noticing that the confident robot was wrong. I have some questions.

      Your first example works and makes sense to me because the transparent method is called with an allocated color and this causes it to return the index of the transparent color: 0 instead of -1.

      You second example works but since the transparent method is not called transparent continues to return -1 which according to the doc means transparency is disabled, so how is it transparent?

      The answer from gpt looks like it should work like your first example works: the allocated alpha "color" of 127 (transparent) is passed to the transparent method, but instead of returning 0 or -1 or something else, the transparent method now returns 2130706432 which is... two gigabytes?

      #!/usr/bin/perl use strict; use warnings; use GD; # First working example my $image = GD::Image->new(1, 1); my $transparent = $image->colorAllocate(0, 0, 0); warn $image->transparent; $image->transparent($transparent); warn $image->transparent; # Second working example $image = GD::Image->newTrueColor(1, 1); $image->alphaBlending(0); $image->saveAlpha(1); $transparent = $image->colorAllocateAlpha(0, 0, 0, 127); warn $image->transparent; $image->setPixel(0, 0, $transparent); warn $image->transparent; # Broken ChatGPT example $image = GD::Image->newTrueColor(1, 1); $image->alphaBlending(0); $image->saveAlpha(1); $transparent = $image->colorAllocateAlpha(0, 0, 0, 127); warn $image->transparent; $image->transparent($transparent); warn $image->transparent;
      Output:
      -1 at line 9.
      0 at line 11.
      -1 at line 18.
      -1 at line 20.
      -1 at line 27.
      2130706432 at line 29.
      

        To add to confusion -- even shorter code to generate transparent PNG (but not GIF) would be

        my $image = GD::Image->new(1, 1); $image->colorAllocateAlpha(0, 0, 0, 127); # output follows

        (return value ignored; no call to bot's beloved transparent)

        I am not an expert. For palette-based images, the colorAllocate (colorAllocateAlpha) would return palette entry index just added; -1 for failure (palette full?). For "TrueColor" images, these methods don't add anything anywhere (perhaps not quite so... I'll elaborate later if time's left). They are convenient shortcuts for pack/unpack as I see it:

        unpack 'V', pack 'C4', 0, 0, 0, 127

        which is... two gigabytes :) ? Because painting methods (such as setPixel) expect just an integer for color, be it palette index or 32-bit integer, where appropriate.

        GD creates images with zeroes-filled canvas. For palette-based images, it means that 1st palette entry (non-existent at the time i.e. yet to be added) would define "background". PNG palette can be thought of as "RGBA", though it's indirectly so (irrelevant detail): axillary tRNS chunk controls entries' partial transparency; it doesn't have to cover all palette; degenerate case would be binary GIF transparency; hence no real necessity to call transparent even if palette-based.

        0 for alpha usually designates full transparency. GD encodes as opposite, internally (not in PNG output, of course). 0 of newly created "TrueColor" canvas means "opaque". The '127' is fully transparent. And by the way, it follows that opening "TrueColor" semitransparent PNG and immediately saving leads to loss of information. I only vaguely remember the reason why 7-bit depth alpha-channel limit exists in GD. The "TrueColor" support is later addition on top of existing data-structures; backward compatibility compromise or something similar.

        To directly address another your question, 0-filled (i.e opaque) "TrueColor" canvas just has to be painted (e.g. setPixel) with transparent color (i.e. 32-bit integer with non-zero byte at big(? confused myself at this late time)-end byte) to introduce transparency. The transparent call would not change anything here.

      How very amazing. But what about absolutely irrelevant fact, that image produced is NOT transparent

      It is very amazing and also far from perfect.

      But...
      ...do not judge all of AI from what you see in ChatGPT and other early systems.

      The AI landscape is moving very fast and already, there are more capable systems. Just like the internet of today could not have been envisioned when the internet arrived in the 1990s (who could have thought about Alexa when we used dial-up?), so AI will improve rapidly and take us to places that we cannot imagine today. What we are seeing now is just the beginning.

      Being an optimist, I am very excited for the future of this technology and consider it an amazing time to be alive...

        What we are seeing now is just the beginning. -- universal truth and truism
Re: ChatGPT's solution (was: Re: Create email tracking image)
by Anonymous Monk on Mar 22, 2023 at 21:49 UTC
    Very interesting results, thanks for sharing. That's an impressive search engine! It's amazing that the code is written properly and that it even works. The presence of appropriate comments is over the top. You didn't include the generated explaination of how the code works so I have to ask, would you characterize it as documentation?

    I wonder what it thinks you mean by "the best way" when there are so many:

    my $i = Imager->new(xsize => 1, ysize => 1, channels => 4); $i->write(fd=>fileno(STDOUT), type=>'png') or die $i->errstr;
      You didn't include the generated explaination of how the code works so...

      Here you go!

      This code creates a new transparent image with a size of 1x1 pixel using the GD::Image module. It sets the alpha blending to 0 and saves the alpha channel to enable transparency. Then it allocates a transparent color and sets it as the transparent color for the image. Finally, it outputs the image as a PNG.

Re: ChatGPT's solution (was: Re: Create email tracking image)
by misc (Friar) on Mar 27, 2023 at 14:16 UTC
    :o it will be back
    it's already  f tracking us
    ______ <((((((\\\ / . }\ ;--..--._|} (\ '--/\--' ) \\ | '-' :'| \\ . -==- .-| \\ \.__.' \--._ [\\ __.--| // _/'--. \ \\ .'-._ ('-----'/ __/ \ \ \\ / __>| | '--. | \ \\ | \ | / / / \ '\ / \ | | _/ / \ \ \ | | / / snd \ \ \ /

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 06:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found