Home > Archive > PERL Beginners > July 2007 > Read and Write Image Files
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Read and Write Image Files
|
|
| Cheater 2007-07-28, 3:59 am |
| I am attempting to load an image using the standard open function. I
then binmode the file handle. But when I attempt to write the image to
a new file (to try and create a duplicate), the new file becomes
unreadable. I know I could just copy the file itself without opening
it, but I need to do it this way. Any help would be greatly
appreciated. Here is my code.
open (IMAGE, $imgfile) || die "Can't Open $imgfile\n";
binmode(IMAGE);
open (OUTPUT, ">$outputfile") || die "Can't Open $outputfile\n";
binmode(OUTPUT);
print OUTPUT while (<IMAGE> );
close(IMAGE);
close (OUTPUT);
| |
| John W. Krahn 2007-07-28, 3:59 am |
| Cheater wrote:
> I am attempting to load an image using the standard open function. I
> then binmode the file handle. But when I attempt to write the image to
> a new file (to try and create a duplicate), the new file becomes
> unreadable. I know I could just copy the file itself without opening
> it, but I need to do it this way. Any help would be greatly
> appreciated. Here is my code.
>
> open (IMAGE, $imgfile) || die "Can't Open $imgfile\n";
> binmode(IMAGE);
>
> open (OUTPUT, ">$outputfile") || die "Can't Open $outputfile\n";
> binmode(OUTPUT);
> print OUTPUT while (<IMAGE> );
> close(IMAGE);
> close (OUTPUT);
My guess is that there is could be some end-of-line translation happening.
You could try it like this:
open IMAGE, '<:raw', $imgfile or die "Can't Open '$imgfile' $!";
open OUTPUT, '>:raw', $outputfile or die "Can't Open '$outputfile' $!";
$/ = \1024; # set input line length
print OUTPUT while <IMAGE>;
close OUTPUT;
close IMAGE;
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
| |
| Paul Lalli 2007-07-28, 6:59 pm |
| On Jul 27, 7:00 pm, MrChe...@gmail.com (Cheater) wrote:
> I am attempting to load an image using the standard open function. I
> then binmode the file handle. But when I attempt to write the image to
> a new file (to try and create a duplicate), the new file becomes
> unreadable. I know I could just copy the file itself without opening
> it, but I need to do it this way.
Says who? If you know a way to get it to work, what possible reason
is there to do it differently, unless of course this is homework. In
which case, go ask your teacher.
> Any help would be greatly
> appreciated. Here is my code.
>
> open (IMAGE, $imgfile) || die "Can't Open $imgfile\n";
> binmode(IMAGE);
>
> open (OUTPUT, ">$outputfile") || die "Can't Open $outputfile\n";
> binmode(OUTPUT);
> print OUTPUT while (<IMAGE> );
<IMAGE> reads using newlines. There is no such concept in a binary
file.
perldoc -f read
Paul Lalli
| |
| yaron@kahanovitch.com 2007-07-29, 6:59 pm |
| Hi...
open (IMAGE, $imgfile) || die "Can't Open $imgfile\n";
=09binmode(IMAGE);
open (OUTPUT, ">$outputfile") || die "Can't Open $outputfile\n";
=09binmode(OUTPUT);
my $buf;
my $bufSize =3D 4096;
while (read(IMAGE,$buf,$bufsize)) { print OUTPUT $buf;}
Yours,
Yaron Kahanovitch
----- Original Message -----
From: "Cheater" <MrCheatr@gmail.com>
To: beginners@perl.org
Sent: 01:00:32 (GMT+0200) Africa/Harare =D7=A9=D7=91=D7=AA 28 =D7=99=D7=95=
=D7=9C=D7=99 2007
Subject: Read and Write Image Files
I am attempting to load an image using the standard open function. I
then binmode the file handle. But when I attempt to write the image to
a new file (to try and create a duplicate), the new file becomes
unreadable. I know I could just copy the file itself without opening
it, but I need to do it this way. Any help would be greatly
appreciated. Here is my code.
open (IMAGE, $imgfile) || die "Can't Open $imgfile\n";
=09binmode(IMAGE);
open (OUTPUT, ">$outputfile") || die "Can't Open $outputfile\n";
=09binmode(OUTPUT);
=09print OUTPUT while (<IMAGE> );
close(IMAGE);
close (OUTPUT);
--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
|
|
|
|
|