Images uploaded with Via Perl Net::FTP get corrupted - perl

Why am I always getting corrupted image file when uploading to FTP server? .gif image doesn't get corrupted, only .jpeg/jpg and .png get corrupted.
sub png{
my $ftp=Net::FTP->new($fhost)or die &ftpErr;
$ftp->login($hostname, $hostpass);
my $img=$ftp->put("$file");
$ftp->get($img);
$ftp->quit;
our $image="$img";
our $shot=$window->Photo(-format=>'png',-file=>"$image");
$window->Label(-relief=>'ridge',-image=>$shot,-width=>50,-height=>50)->pack(-anchor=>'n');
}
sub jpeg{
my $ftp=Net::FTP->new($fhost)or die &ftpErr;
$ftp->login($hostname, $hostpass);
my $img=$ftp->put("$file");
$ftp->get($img);
$ftp->quit;
our $image="$img";
our $shot=$window->Photo(-format=>'jpeg',-file=>"$image");
$window->Label(-relief=>'ridge',-image=>$shot,-width=>50,-height=>50)->pack(-anchor=>'n');
}

You are transferring the files in the default mode, which is ASCII. This mode translates line ends. To transfer binary files use binary mode:
$ftp->binary;
$ftp->put(...);
$ftp->get(...);

Related

Read multiple h5 files but there is an os error couldn't find these files

When I am trying to read many h5 files in one shoot. There is an OS error states like this:
OSError: Unable to open file (unable to open file: name = '/scratch-lustre/hpc-0227/deepcpgData/c{1,2,3,4,5,7,9,11,13}_*.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)
I am sure all of these files exist and files' name corresponded with c{1,2,3,4,5,7,9,11,13}_*.h5 rightly. I am also using absolute path. My bash script looks like this:
data_dir="/scratch-lustre/hpc-0227/deepcpgData"
train_files="$data_dir/c{1,2,3,4,5,7,9,11,13}_*.h5"
It works when I use the full name of a single file, for example, c16_1572864-1605632.h5. However, I need to read a lot of files. So I have to read them in one round. I have searched many other answers but none of them deal with multiple files and the os error at the same time.

Error when trying to use custom tessdata file

I have generated a box file from a png image then I followed this tutorial:
https://pretius.com/how-to-prepare-training-files-for-tesseract-ocr-and-improve-characters-recognition/ to generate custom traineddata file.
I encountered an error when I tried to use the generated traineddata alongside with Pytesseract.
and i got this kind of error:
raise TesseractError(proc.returncode, get_errors(error_string))
pytesseract.pytesseract.TesseractError: (-4, "read_params_file:
Can't open txt read_params_file: Can't open txt read_params_file: Can't open txt read_params_file: Can't open txt Error: LSTM requested, but not present!! Loading tesseract. mgr->GetComponent(TESSDATA_NORMPROTO, &fp)
:Error:Assert failed:in file adaptmatch.cpp, line 552")
I'm using Tesseract version 5.0
This is my config options
traineddata = f'+eng+lav+lav2'
config = f'-l {traineddata} --oem 1 --psm 3 {tessdata_dir}'
I followed the same tutorial and encountered the exact same error. At my first tries the ***.traineddata didn't generated well, and I findout that one file was missing (normproto). So I just cleaned all the generated files (except the corrected .box files) and rerun the train process, and everything worked fine on the second attempt.

Read and compare images in folder using Perl

I am trying to read and compare large number of image files in a folder. But I am not able to read the image file.
Code snippet :
use strict;
use warnings;
use Image::Compare;
opendir my $ref_dir, "./" or die "Cannot open directory: $!";
my #ref_files = readdir $ref_dir;
closedir $ref_dir;
print $#ref_files;
my($cmp) = Image::Compare->new();
$cmp->set_image1(
img => './'.$ref_files[0], #one instance - reading first file in the folder
type => 'bmp',
);
The code is throwing the following error - "Unable to read image data from file './.': 'Could not open ./.: Permission denied' at C:/Perl64/site/lib/Image/Compare.pm line 162. "
The code works fine if I provide the file name directly - img => './image.bmp'. So it can't be a permission issue.
The error message seems pretty clear.
Unable to read image data from file './.': 'Could not open ./.: Permission denied
The first file you're getting back from opendir() is the current directory (.) - and it's no surprise that Image::Compare can't get the image data that it wants from that file!
Perhaps you should add a filter to only return the files you're interested in.
my #ref_files = grep { -f } readdir $ref_dir;

Download multiple files from SFTP using perl Net::SFTP::Foreign

I am using the below code to download files from SFTP and I am getting error message after one file download. Then I am rerunning the program and then it downloads only one file again and when trying to download the next file in the array it get failed. I couldn't understand the reasons for this failure and kindly review the below code and share opinions.
#!/usr/bin/perl
use strict;
use Net::SFTP::Foreign::Constants qw(:error :status);
use Net::SFTP::Foreign;
use Net::SFTP::Foreign::Attributes;
my $SFTP_CON=Net::SFTP::Foreign->new("sftpserver.test.com",user=>"testuser",password=>"pass");
my #SFTP_Files=('/test/1.txt','/test/2.txt','/test/3.txt');
my $root_dir='/test/';
$local_dir='/u/test/';
foreach my $file_to_dwn(#SFTP_Files){
my $file_to=$file_to_dwn;
$file_to=~s/$root_dir/$local_dir/igs;
unless($SFTP_CON->get("$file_to_dwn","$file_to")){
if (($SFTP_CON->error == SFTP_ERR_REMOTE_STAT_FAILED or
$SFTP_CON->error == SFTP_ERR_REMOTE_OPEN_FAILED) and
$SFTP_CON->status == SSH2_FX_NO_SUCH_FILE) {
print "Remote file does not exist!";
exit;
}
else {
print "Transfer Failed";
exit;
}
print $SFTP_CON->error."\n";
exit;
}
}
I am getting "Transfer Failed", when the program tries to download the one after another (second file download).
Please do needful.

Error reason from Perl Net::FTP

I'm using Net::FTP to transfer files up to a mainframe and I'm testing failure conditions.
My code is basically along the following lines:
my $ftp = Net::FTP->new ("mainframe.com", Timeout => 20);
if (! $ftp) {
logMessage ("Could not connect to host: $!");
return;
}
if (! $ftp->login ("paxdiablo", "demigodemporeroftheuniverse")) {
logMessage ("Could not log in to host: $!");
$ftp->quit ();
return;
}
if (! $ftp->put ("myfile.txt", "'CANT.WRITE.TO.THIS'")) {
logMessage ("Could not put file: $!");
$ftp->quit ();
return;
}
I know I can't create the data set CANT.WRITE.TO.THIS since I don't have the required permissions but, when I try, the only message I see is:
Could not put file:
There is no indication in $! as to what the problem was. I've looked in the Net::FTP doco and all it says is:
put ( LOCAL_FILE [, REMOTE_FILE ] )Put a file on the remote server. LOCAL_FILE may be a name or a filehandle. If LOCAL_FILE is a filehandle then REMOTE_FILE must be specified. If REMOTE_FILE is not specified then the file will be stored in the current directory with the same leafname as LOCAL_FILE.Returns REMOTE_FILE or the generated remote filename if REMOTE_FILE is not given.
I also cannot find anything there about retrieving the specific error (like $ftp->getLastError() or something similar).
How can I indicate to the user why the transfer failed?
On an earlier iteration, I resorted to putting the file, then getting it again and checking contents locally. I'd really rather not have to inflict such kludgy code on people again.
From Net::FTP:
$ftp = Net::FTP->new("some.host.name", Debug => 0)
or die "Cannot connect to some.host.name: $#"
Note $#.
$ftp->cwd("/pub")
or die "Cannot change working directory ", $ftp->message;
Note $ftp->message