Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening/Writing Images

Status
Not open for further replies.

SCelia

Programmer
Feb 27, 2002
82
CA
This file is an attempt to find the unique images in a directory and write them to a new directory. Unfortuantly one of its problems is that the output file is a garbled thing that is not readable as a jpg. How can I fix that?


my $skip = 0; #must be set to zero intially

my @all_files = ();
my $pHandle = '';
my $vHandle = '';
my $image = '';

my $dir = 'D:\Earth\digits\\';

opendir (DIR, $dir) or die "cannot opendir $dir";
foreach my $file (readdir(DIR)) {
unless ($file eq '.' or $file eq '..') {
push(@all_files, $file);
}
}
closedir (DIR);

print "I made it past the dirtectory read\n";

#print "The files in the directory are:\n\n";
#foreach (@all_files) {
#print $_."\n";
#}


my %filehash = ();
foreach my $filename (@all_files) {
my $filesize = (stat('D:\Earth\digits\\'.$filename))[7];
if(defined $filehash{$filesize}) { $filehash{$filesize} += ", $filename"; }
else { $filehash{$filesize} = $filename; }
}

print "I made it past the first bit of Tumi's code\n";

print "The hash created with all the files in it is:\n\n";

foreach my $testkey (keys %filehash) {
print "$filehash{$testkey}\n";
}

foreach my $key (keys %filehash) {
unless ($filehash{$key} =~ /, /) {

open(PROD, &quot;< D:\\Earth\\digits\\$filehash{$key}.jpg&quot;) or die &quot;Can't read the production file $!&quot;;
# write to a scalar
$image = '';
while(<PROD>)
{
$image .=$_
}
# Close the filehandle
close(PROD) or die &quot;Can't close the production file.&quot;;

open(IMAGE, &quot;>D:\\Earth\\unique\\$filehash{$key}.jpg&quot;) || die&quot;$filehash{$key}: $!&quot;;
binmode IMAGE;
print IMAGE $image;
close IMAGE;
print &quot;$filehash{$key} is unique and is saved away\n&quot;;
}
else { # do the unique read & compare check
my @files = split(/, /,$filehash{$key});
my @copyfiles = @files;
foreach my $fileA (@files) {
my $pPath = 'D:\Earth\digits\\'.$fileA.'.jpg';
open(PROD, &quot;< $pPath&quot;) or die &quot;Can't read the production file&quot;;

# write to a scalar
$pHandle = '';
while(<PROD>)
{
$pHandle .=$_
}
# Close the filehandle
close(PROD) or die &quot;Can't close the production file.&quot;;

foreach my $fileB (@copyfiles) {
my $vPath = 'D:\Earth\unique\\'.$fileB.'.jpg';

# Open the versioned file Read Only.
open(VERS, &quot;< $vPath&quot;) or $skip=1;
# write to a scalar
unless ($skip) {
$vHandle = '';
while(<VERS>)
{
$vHandle .=$_
}
# Close the filehandle
close(VERS) or die &quot;Can't close the versioned file.&quot;;

# Compare the scalars
###### NOTE: this is a -- BINARY -- compare.
if($vHandle eq $pHandle)
{
#file already exists in unique directory and therefore is not unique
last;
}
else
{
# Continue -?
}
#if your still going through the loop then $vHandle never equaled $pHandle so the file is unique
open(IMAGE, &quot;>D:\\Earth\\unique\\$fileA.jpg&quot;) || die&quot;$pHandle: $!&quot;;
binmode IMAGE;
print IMAGE $pHandle;
close IMAGE;
print &quot;$pHandle is unique and is saved away\n&quot;;

}
$skip = 0; #reset skip
print &quot;I made it through the loop\n&quot;;
}
}
}
}
Celia
 
Have you tried escaping all the backslashes in the directory name? e.g.

my $dir = 'D:\\Earth\\digits\\'; C:\DOS:>
C:\DOS:>RUN
RUN DOS RUN!!
 
The files are read in fine, in fact it even outputs new files to the specified directory. The trouble is its not outputting anything of use. The files are garbled beyond recognition. Celia
 
How about opening the original file in binary mode as well?

# write to a scalar
binmode PROD;
$image = '';
while(<PROD>)
{
$image .=$_
}
# Close the filehandle
close(PROD) or die &quot;Can't close the production file.&quot;;
C:\DOS:>
C:\DOS:>RUN
RUN DOS RUN!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top