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

I need to import a PNG image into a piddle. To no avail, I tried to use a bit of code that I have successfully used in the past to import TIFFs to piddles (see below).

use strict; use PDL; use PDL::IO::Pic; my $path = 'Z:\path\filename.png'; my $piddle = rim($path);
When I run this, I get the following error message:
can't figure out file type, specify explicitly at Basic/Core/Core.pm.PL (i.e. PDL::Core.pm) line 396
-----PDL::Core::barf('can\'t figure out file type, specify explicitly') called at C:/Perl/site/lib/PDL/IO/Pic.pm line 303
-----PDL::rpic('PDL', 'Z:\path\filename.png') called at C:/Perl/site/lib/PDL/IO/Pic.pm line 278
-----PDL::IO::Pic::rpic('Z:\path\filename.png') called at C:/Perl/site/lib/PDL/IO/Pic.pm line 576
-----PDL::IO::Pic::rim('Z:\path\filename.png') called at Z:\path2\test4.pl line 7
Any ideas? Ideally, I would like to stick with rim, but I am willing to move to something else if required or appropriate. Thanks, Perldough

Replies are listed 'Best First'.
Re: Importing a PNG image to a piddle
by syphilis (Archbishop) on Jul 18, 2012 at 00:26 UTC
    'can\'t figure out file type, specify explicitly'

    You need to specify the format. (Seems to be a Windows thing only.) So, I would do:
    my $piddle = PDL->rpic($path, {FORMAT => 'PNG'});
    which works fine for me (iff pngtopnm.exe/pnmtopng.exe are in my path). I'm not familiar with the rim() function - afaict
    my $piddle = rim($path, {FORMAT => 'PNG'});
    should also work, but for me that just produces:
    Can't locate object method "dim" via package "file.png" (perhaps you f +orgot to load "file.png"?) at C:/MinGW/perl516/site/lib/PDL/IO/Pic.pm + line 570.
    Update: the rim() function is broken in my build of PDL (version 2.4.11), and may well have been broken for quite some time.
    It just calls rpic() anyway, so use that instead.

    (For my test script, $path was set to file.png ... obviously.)

    Cheers,
      You are right, using rpic does get rid of the error, but when I look in my piddle, it's all black. I ended up using GD. Thanks very much for your time.
Re: Importing a PNG image to a piddle
by BrowserUk (Patriarch) on Jul 17, 2012 at 22:02 UTC

    It doesn't look like .png is a supported image type. You could try calling rpiccan() without arguments to see what it reports.

    If not, you could always convert it to a known format using (say) GD.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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.

    The start of some sanity?

      Thank you. I ended up using GD as you suggested in the way shown below.
Re: Importing a PNG image to a piddle
by zentara (Cardinal) on Jul 18, 2012 at 11:45 UTC
    I had similar troubles loading a png to a piddle a few years ago, and asked on the maillist. Here is a summary of the responses. With PNG there is alot of details, especially if it's a True Color class or Psuedo class, and a LUT table. The following is a copy of the email exchange, with the code suggestions and explanations. The best advice is that if you don't know what type of PNG you have, it is best to use PDL::IO::GD, ie, just use read_png() to read a greyscale image, or use read_true_png() to read an RGB image.
    #!/usr/bin/perl use warnings; use strict; use PDL::LiteF; use PDL::IO::GD; my $im = PDL::IO::GD->new( { filename => '1Zen16.png' } ); # Grab the PDL of the data: my $pdl = $im->to_pdl(); print dims $pdl,"\n"; # Allocate some colors: # my $black = $im->ColorAllocate( 0, 0, 0 ); my $red = $im->ColorAllocate( 255, 0, 0 ); my $green = $im->ColorAllocate( 0, 255, 0 ); my $blue = $im->ColorAllocate( 0, 0, 255 ); # Draw a rectangle: $im->Rectangle( 10, 10, 290, 290, $red ); # Add some text: $im->String( gdFontGetLarge(), 20, 20, "Test Large Font!", $green ); # Write the output file: $im->write_Png( "$0.png" );

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh