Hello monks,

I've been reading about PNG files and pack/unpack for a little while, and thought I understood it enough to perform a simple operation, but apparently I was mistaken. I created a PNG file with another script, and now I'm trying to get to know pack/unpack so I can read/write the associated binary data. The script doesn't error, but only makes a single byte file with "very short lines (no magic)" as a result. Here is the code:

#!/usr/bin/perl use strict; use warnings; my $ifh = open("<:raw", "./file-bar.png"); my $data = unpack('a*', $ifh); my $outputfile = "newfile.png"; open (my $ofh, '>:raw', $outputfile) or die "Could not open '$outputfile' $!"; print $ofh pack('a*', $data); close $ofh; print("Image created.\n");
Here are the file types of the initial and created files:
$ file file-bar.png file-bar.png: PNG image data, 400 x 300, 4-bit colormap, interlaced $ file newfile.png newfile.png: very short file (no magic)
I've tried to piece together bits of code from various sources, so that's why it's not of the same style, but feel there's no point in cleaning up code that doesn't work yet. Any help is greatly appreciated.

In reply to Using pack/unpack on a PNG file by sierpinski

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.