#!/usr/bin/perl
use strict;
use warnings;
use strict;
use warnings;
use Tk;
#use File::Copy::Recursive qw(dircopy);
use File::Spec;
use File::Basename;
use File::Copy;
use Math::Trig;
use Math::Round;
use Image::Size qw/imgsize/;
use Image::Imlib2;
use Text::CSV_XS;
use feature "say";
use Data::Dumper qw(Dumper);
my @dir_stack = ('.');  # Stack to store directories navigated
my $selected_file;
my $selected_dir;
my @contents;
my $print_dir;
my @files;
my $text;
my $dir;
my $blank;
my $random_files=2;
my $ran_frac=4;
my $c=0;
#my $start_w=100;
my $new_w=400;
my $frames=50;
my $start_fr=10;
my $image_width;
my $image_height;
my $image_file;
my $image2_file;
my $xy_file;
my $wh_file;
my $new_suffix="newsuffix";
my $aspect;
my $save_directory="TEST";
my $change="";
my @keys_list=qw(image wh xy);
my $key="image";
my $mw = MainWindow->new;
$mw->title("MAKE LISTS FROM A DIRECTORY"); 
$mw->geometry("1100x860+1100+500");
my $frame = $mw->Frame()->grid(qw/ -row 2 -column 0  -sticky w/); 
my $frame3 = $mw->Frame()->grid(qw/ -row 2 -column 1  -sticky w/); 
my $frame2=$mw->Frame()->grid(qw/ -row 12 -column 0 -sticky w/);
my $label=$mw->Label(-font => '{Helvetica} 15', -text => "Applies to all in selected directory:", -width=>28)->grid(qw/ -row 0 -column 0 -sticky n/);
$label=$mw->Label(-font => '{Helvetica} 15', -text => "save directory:", -width=>20)->grid(qw/ -row 0 -column 1 -sticky w/);
my $entry =$mw->Entry(-font => '{Helvetica} 15', -textvariable => \$save_directory, -width=>12)->grid(qw/ -row 0 -column 1 -sticky e/);

my $listbox = $frame->Scrolled('Listbox', -font => '{Helvetica} 20', -scrollbars => 'se', -width=>30)->grid(qw/ -row 0 -column 0 -sticky w/);
my $listbox2 = $frame2->Scrolled('Listbox', -font => '{Helvetica} 20', -scrollbars => 'se', -width=>30, -height =>6)->grid(qw/ -row 0 -column 0  -sticky w/); 
$listbox->bind('<Double-Button-1>', \&select_item);
my $button = $frame3->Button(-font => '{Helvetica} 15', -text => 'list from all images in dir:', -justify =>'left', -width=>19, -command => sub{&make_csv()})->grid(qw/ -row 0 -column 0 -sticky n/);

$button = $frame3->Button(-font => '{Helvetica} 15', -text => 'shuffle images: lists, cuts', -justify =>'left', -width=>19, -command => sub{&randomize()})->grid(qw/ -row 2 -column 0 -sticky w/);
$entry =$frame3->Entry(-font => '{Helvetica} 15', -textvariable => \$random_files, -width=>5)->grid(qw/ -row 2 -column 1 -sticky w/);
$entry = $frame3->Entry(-font => '{Helvetica} 15', -textvariable => \$ran_frac, -width=>5)->grid(qw/ -row 2 -column 2 -sticky w/);


$button = $frame3->Button(-font => '{Helvetica} 15', -text => 'zoom: width, start f, end f', -justify =>'left', -width=>19, -command => sub{&zoom()})->grid(qw/ -row 4 -column 0 -sticky w/);
$entry = $frame3->Entry(-font => '{Helvetica} 15', -textvariable => \$new_w, -width=>5)->grid(qw/ -row 4 -column 1 -sticky w/);
$entry = $frame3->Entry(-font => '{Helvetica} 15', -textvariable => \$start_fr, -width=>5)->grid(qw/ -row 4 -column 2 -sticky w/);
$entry = $frame3->Entry(-font => '{Helvetica} 15', -textvariable => \$frames, -width=>5)->grid(qw/ -row 4 -column 3 -sticky w/);


$button = $frame3->Button(-font => '{Helvetica} 15', -text => 'add x,y to file:', -justify =>'left', -width=>19, -command => sub{&add_xy()})->grid(qw/ -row 6 -column 0 -sticky w/);
$button = $frame3->Button(-font => '{Helvetica} 15', -text => 'image,wh file', -width=>10, -command => sub{$image_file=$selected_file;&say();})->grid(qw/ -row 6 -column 1 -columnspan 2 -sticky w/);
$button = $frame3->Button(-font => '{Helvetica} 15', -text => 'x,y file', -width=>10, -command => sub{$xy_file=$selected_file;&say();})->grid(qw/ -row 6 -column 3 -columnspan 2 -sticky w/);

$button = $frame3->Button(-font => '{Helvetica} 15', -text => 'add w,h to file:', -justify =>'left', -width=>19, -command => sub{&add_wh()})->grid(qw/ -row 8 -column 0 -sticky w/);
$button = $frame3->Button(-font => '{Helvetica} 15', -text => 'image,x,y file', -width=>10, -command => sub{$image_file=$selected_file;&say();})->grid(qw/ -row 8 -column 1 -columnspan 2 -sticky w/);
$button = $frame3->Button(-font => '{Helvetica} 15', -text => 'w,h file', -width=>10, -command => sub{$wh_file=$selected_file;&say();})->grid(qw/ -row 8 -column 3 -columnspan 2 -sticky w/);

$button = $frame3->Button(-font => '{Helvetica} 15', -text => 'add images to file:', -justify =>'left', -width=>19, -command => sub{&add_image()})->grid(qw/ -row 10 -column 0 -sticky w/);
$button = $frame3->Button(-font => '{Helvetica} 15', -text => 'xy,wh file', -width=>10, -command => sub{$xy_file=$selected_file;&say();})->grid(qw/ -row 10 -column 1 -columnspan 2 -sticky w/);
$button = $frame3->Button(-font => '{Helvetica} 15', -text => 'image file', -width=>10, -command => sub{$image_file=$selected_file;&say();})->grid(qw/ -row 10 -column 3 -columnspan 2 -sticky w/);

$button = $frame3->Button(-font => '{Helvetica} 15', -text => 'add wh of images in file:', -justify =>'left', -width=>19, -command => sub{&add_self_wh()})->grid(qw/ -row 12 -column 0 -sticky w/);
$button = $frame3->Button(-font => '{Helvetica} 15', -text => 'change suffix', -justify =>'left', -width=>19, -command => sub{&change_suffix("no")})->grid(qw/ -row 14 -column 0 -sticky w/);
$entry = $frame3->Entry(-font => '{Helvetica} 15', -textvariable => \$new_suffix, -width=>12)->grid(qw/ -row 14 -column 1 -columnspan 2 -sticky w/);
$button = $frame3->Button(-font => '{Helvetica} 15', -text => 'change', -justify =>'left', -width=>10, -command => sub{&change_suffix("yes")})->grid(qw/ -row 14 -column 3 -sticky w/);

$button = $frame3->Button(-font => '{Helvetica} 15', -text => 'extract from file', -justify =>'left', -width=>19, -command => sub{&extract})->grid(qw/ -row 16 -column 0 -sticky w/);
$entry = $frame3->Entry(-font => '{Helvetica} 15', -textvariable => \$key, -width=>12)->grid(qw/ -row 16 -column 1 -columnspan 2 -sticky w/);
$button = $frame3->Button(-font => '{Helvetica} 15', -text => 'select ->', -justify =>'left', -width=>10, -command => sub {$c=$c+1;if ($c>2){$c=0};$key=$keys_list[$c];})->grid(qw/ -row 16 -column 3 -sticky w/);


$entry = $frame3->Label(-font => '{Helvetica} 15', -text => '', -justify =>'left', -width=>1)->grid(qw/ -row 17 -column 1 -sticky w/);
#$button = $frame3->Button(-font => '{Helvetica} 15', -text => '-----', -justify =>'left', -width=>19, -command => sub{&blank()})->grid(qw/ -row 12 -column 0 -sticky w/);
#$button = $frame3->Button(-font => '{Helvetica} 15', -text => '-------', -justify =>'left', -width=>19, -command => sub{&blank()})->grid(qw/ -row 14 -column 0 -sticky w/);
#$button = $frame3->Button(-font => '{Helvetica} 15', -text => 'change_colour', -justify =>'left', -width=>10, -command => sub{&change_colour()})->grid(qw/ -row 16 -column 0 -sticky w/);
$button = $frame3->Button(-font => '{Helvetica} 15', -text => 'EXIT', -justify =>'left', -width=>19, -command => sub{$mw->destroy()})->grid(qw/ -row 18 -column 0 -sticky w/);
$button = $mw->Button(-font => '{Helvetica} 15', -text => 'UP', -width=>4, -command => \&go_up)->grid(qw/ -row 10 -column 0 -sticky w/); 



&refresh_listbox;
MainLoop;


sub extract{

&check_dir();
my @temp;
my $ct=0;
my $text="";
my $ext1="";
my $ext2="";
if ($key eq "image"){$ext1="image";$ext2=""}
if ($key eq "xy"){$ext1="x";$ext2="y"}
if ($key eq "xy"){$ext1="w";$ext2="h"}
my $csv = Text::CSV_XS->new ();

open (my $fh , '<',"$selected_file")or die"$!";		
while (my $line = <$fh>) {
      $csv->parse($line);
      my %values=$csv->fields();

if($values{x} && $values{y} && $key eq "xy"){
$text="file has x and y";
$ext1="x";
$ext2="y";
$temp[$ct]{x}=$values{x};$text="file has x and y";
$temp[$ct]{y}=$values{y};
}

if($values{w} && $values{h} && $key eq "wh"){
$text="file has w and h";
$ext1="w";
$ext2="h";

$temp[$ct]{w}=$values{w};$text="file has w and h";
$temp[$ct]{h}=$values{h};
}

if($values{image} && $key eq "image"){
$text="! file has image";
$ext1="image";
$temp[$ct]{image}=$values{image};$text="file has image";
}

$ct++;

}
say $text;
#say Dumper @temp;

my $file="$save_directory/extracted_$key.list";
open($fh, '>', $file) or die "Could not open file '$file' $!";
for( my $i=0;$i<=$#temp;$i++){
print $fh "$ext1,$temp[$i]{$ext1}";
if ($ext2){
print $fh ",$ext2,$temp[$i]{$ext2}";
}
print $fh "\n";	
}

close $fh;
	}



sub blank{say "blank";}

sub refresh_listbox {
my $dir = $dir_stack[-1];
$listbox->delete(0, 'end');

    opendir(my $dh, $dir) or die "Can't open $dir: $!";
    @contents = readdir($dh);
    closedir($dh);
@contents = sort { lc($a) cmp lc($b) } @contents;
    foreach my $item (@contents) {
        next if $item =~ /^\./;  # skip hidden files/directories
        my $path = File::Spec->catfile($dir, $item);
        if (-d $path) {
            $listbox->insert('end', $item);
            $listbox->itemconfigure('end', -foreground => 'blue');  # Apply blue color to directory item
        } elsif ($item =~ /\.png$/i || $item =~ /\.jpg/i || $item =~ /\.list/i || $item =~ /\.imgs/i|| $item =~ /\.xy/i){
            $listbox->insert('end', $item);
        }
    }
}

sub select_item {
my $selection = $listbox->get('active');
 my $path = File::Spec->catfile($dir_stack[-1], $selection);

    if (-d $path) {
        push @dir_stack, $path;  # Push selected directory to the stack
        refresh_listbox();
        $selected_dir=$path;
 $text="Selected directory: $selected_dir";
 $listbox2->delete('0', 'end');
 $listbox2->insert('0', $text);
 &get_size();
   } elsif (-f $path) {
        $selected_file = $path;
        $text= "selected file = $selected_file";
        $listbox2->insert('end', $text);
    }
refresh_listbox();
}

sub go_up {
    pop @dir_stack if @dir_stack > 1;  # Pop the top directory from the stack if not already at the root
    refresh_listbox();
}

sub get_size{
if(!$selected_dir){$text="select directory of file";$listbox2->insert('end', $text);return;}
#$text="directory is not of jpeg or png";
opendir(my $dh, $selected_dir) or die "Could not open directory: $!";
@files = readdir($dh);
closedir($dh);
@files = grep { !/^\.\.?$/ } @files;
@files= sort numerically @files;
my $file="$selected_dir/$files[0]";	
if ($file =~ /\.png$/i || $file =~ /\.jpg$/i){
($image_width, $image_height) =imgsize($file);
$aspect=$image_height/$image_width;
$text="First file width= $image_width, height= $image_height";
}
$listbox2->insert('end', $text);	
@files=undef;
return;
	}
	
sub check_dir{
# makes directories to save the files in
#my $name="save_directory";
unless (-d "./$save_directory") {mkdir "./$save_directory";$text="made directory $save_directory";}
#else {$text= "$name directory exists"}
#if (! -d "./$name/$selected_dir"){mkdir "./$name/$selected_dir";$text="made directionary $name/$selected_dir";}	
return;
}

sub numerically {
    my ($a_num) = $a =~ /(\d+)/;
    my ($b_num) = $b =~ /(\d+)/;
$a_num //= 0;  # Set $a_num to 0 if it's uninitialized
$b_num //= 0;  # Set $b_num to 0 if it's uninitialized
return $a_num <=> $b_num;
}

sub zoom{
if(!$selected_dir){$text="select directory of file";$listbox2->insert('end', $text);return;}
#changes w,h in csv file
&check_dir();			
opendir(my $dh, $selected_dir) or die "Can't open $selected_dir: $!";
my  @files = readdir($dh);
closedir($dh);
@files = grep { !/^\.{1,2}$/ } @files;  # Remove "." and ".." entries
@files = sort numerically @files;
#my $file="./zoom_lists_/$selected_dir/$selected_dir.list";
my $file="./$save_directory/zoom.list";

my $pr=1-(($image_width**2)/($new_w**2))**(1/$frames);
if($image_width>$new_w){
$pr=1-(($new_w**2)/($image_width**2))**(1/$frames);
}
open(my $fh, '>', $file) or die "Could not open file '$file' $!";
my ($last,$w,$h)=(0,0,0);
for( my $i=0;$i<=$#files;$i++){
if($i<$start_fr){
$w=$image_width;
$h=$image_width*$aspect;	
	}
if($i>$start_fr && $i<=($start_fr+$frames)){
$w=int(sqrt(($image_width**2)/((1-$pr)**($i-$start_fr))));
$h=$w*$aspect;
$last=$w;
}

if($i>($start_fr+$frames)){
$w=$last;
$h=$w*$aspect;	
}	
print $fh "image,$selected_dir/$files[$i],w,$w,h,$h\n";
}
close $fh;
$text= "wrote csv $save_directory/zoom.list";
$listbox2->insert('end', $text);
$text="done";
$listbox2->insert('end', $text);

#@files=undef;
}

sub make_csv{
if(!$selected_dir){$text="select directory of file";$listbox2->insert('end', $text);return;}
#makes csv of images in a directory
&check_dir();			
my @temp;
opendir(my $dh, $selected_dir) or die "Can't open $selected_dir: $!";
my  @files = readdir($dh);
closedir($dh);
@files = grep { !/^\.{1,2}$/ } @files;  # Remove "." and ".." entries
@files = sort numerically @files;
#add w,h to array
for( my $i=0;$i<=$#files;$i++){
($image_width, $image_height) =imgsize("$selected_dir/$files[$i]");
$temp[$i]{image}=$files[$i];
$temp[$i]{w}=$image_width;
$temp[$i]{h}=$image_height;
}
my $file =join "","./$save_directory","/","$selected_dir","_image.list";
#my $file="./$save_directory/$selected_dir_image.list";
open(my $fh, '>', $file) or die "Could not open file '$file' $!";
for( my $i=0;$i<=$#files;$i++){
print $fh "image,$selected_dir/$temp[$i]{image},w,$temp[$i]{w},h,$temp[$i]{h}\n";
}
close $fh;	
@temp=undef;
@files=undef;
#$text= "wrote csv $save_directory/image.list";
$text= "wrote csv $file";
$listbox2->insert('end', $text);
$text="done";
$listbox2->insert('end', $text);
	}

sub randomize{
if(!$selected_dir){$text="select directory of file";$listbox2->insert('end', $text);return;}
# combines random segments from a list of files in a directory	
&check_dir("randomized_lists_");
my @part;
opendir(my $dh, $selected_dir) or die "Can't open $selected_dir: $!";
  my  @files = readdir($dh);
  closedir($dh);
@files = grep { !/^\.{1,2}$/ } @files;  # Remove "." and ".." entries
@files = sort numerically @files;
my $k=$#files;
my $chunk=int($k/$ran_frac);# length of kept order
my $ct=0;
while($ct<=$random_files-1){
my $t=0;
while ($t<=$k-1){
my $start = int(rand($k));
for (my $i=0;$i<=$chunk;$i++){
if(($start+$i)<=$k-1){;
$part[$ct][$t]=$files[$start+$i];
$t++;
}}
}
$ct++;
}
#say Dumper @part;
#my $num =scalar @{$part[0]};
#my $num1 =scalar @{$part[1]};
#my $num2 =scalar @{$part[2]};
#say "$num  $num1  $num2";
#my $levels=scalar @overlays;
$ct=$#files;
my $fh;
my $all=0;
while ($all<=$random_files-1){
#my $chk=@{$part[$all]};
my $file="./$save_directory/random$all.list";
open($fh, '>', $file) or die "Could not open file '$file' $!";
for( my $i=0;$i<$ct;$i++){
my $filepath=join "",$selected_dir,"/",$part[$all][$i];
($image_width, $image_height) =imgsize("$filepath");
print $fh "image,$filepath,w,$image_width,h,$image_height\n";
}
$all++;
close $fh;	
}
my $nw=$random_files;
$text="wrote $nw file lists to $save_directory/randomNum.list";
$listbox2->insert('end', $text);
$text="done";
$listbox2->insert('end', $text);
#@part=undef;
}
		
sub say{
if($image_file){
$text="image file = $image_file,"; 
$listbox2->insert('end', $text);
}
if ($xy_file){
$text="xy file = $xy_file,";
$listbox2->insert('end', $text);
	}		
if ($wh_file){
$text="wh file = $wh_file,";
$listbox2->insert('end', $text);
	}

}

sub add_xy{
if(!$selected_dir){$text="select directory of file";$listbox2->insert('end', $text);return;}
my @temp;
# adds xy from a list to images list
&check_dir();
say " image file =$image_file";
my $ct=0;
my $csv = Text::CSV_XS->new ();
open ( my $fh , '<',"$image_file")or die"$!";		
while (my $line = <$fh>) {
      $csv->parse($line);
      my %values=$csv->fields();
 if($values{image}){$temp[$ct]{image}=$values{image}}else{$temp[$ct]{image}="";$text="!there is no image";}
($image_width, $image_height) =imgsize($values{image});
 if($values{x}){$temp[$ct]{x}=$values{x}}else{$temp[$ct]{x}=""}
 if($values{y}){$temp[$ct]{y}=$values{y}}else{$temp[$ct]{y}=""}
 if($values{w}){$temp[$ct]{w}=$values{w}}else{$temp[$ct]{w}=$image_width}
 if($values{h}){$temp[$ct]{h}=$values{h}}else{$temp[$ct]{h}=$image_height}
 $ct++;
}
close $fh;	
$ct=0;
open ($fh , '<',"$xy_file")or die"$!";		
while (my $line = <$fh>) {
      $csv->parse($line);
      my %values=$csv->fields();
 if(!$values{x}){$text="! file does not have xy data !";$listbox2->insert('end', $text);return;}
 if($values{x}){$temp[$ct]{x}=$values{x}}
 if($values{y}){$temp[$ct]{y}=$values{y}}
 $ct++;
}	
@temp = grep { !/^\.{1,2}$/ } @temp;  # Remove "." and ".." entries
@temp = sort numerically @temp;
for( my $i=0;$i<=$#temp;$i++){
$temp[$i]{image}//=$temp[$i-1]{image};
$temp[$i]{w}//=$temp[$i-1]{w};
$temp[$i]{h}//=$temp[$i-1]{h};
$temp[$i]{x}//=$temp[$i-1]{x};
$temp[$i]{y}//=$temp[$i-1]{y};
#print $fh "image,$temp[$i]{image},w,$temp[$i]{w},h,$temp[$i]{h},x,$temp[$i]{x},y,$temp[$i]{y}\n";
}
my $file="./$save_directory/added_xy.list";
open($fh, '>', $file) or die "Could not open file '$file' $!";
for( my $i=0;$i<=$#temp;$i++){
print $fh "image,$temp[$i]{image},w,$temp[$i]{w},h,$temp[$i]{h},x,$temp[$i]{x},y,$temp[$i]{y}\n";
}
close $fh;	
#@temp=undef;
$listbox2->insert('end', $text);
$text= "wrote csv ./$save_directory/added_xy.list";
$listbox2->insert('end', $text);
$text="done";
$listbox2->insert('end', $text);
@temp=undef;
	}
	
sub add_wh{
if(!$selected_dir){$text="select directory of file";$listbox2->insert('end', $text);return;}
my @temp;
# adds xy from a list to images list
&check_dir();
say " wh file =$wh_file";
my $ct=0;
my $csv = Text::CSV_XS->new ();
say "image_file =$image_file";
open ( my $fh , '<',"$image_file")or die"$!";		
while (my $line = <$fh>) {
      $csv->parse($line);
      my %values=$csv->fields();

if($values{image}){$temp[$ct]{image}=$values{image}}else {$temp[$ct]{image}=""}
if($values{image}){($image_width, $image_height) =imgsize($values{image})}
if($values{x}){$temp[$ct]{x}=$values{x}}else {$temp[$ct]{x}=""}
if($values{y}){$temp[$ct]{y}=$values{y}}else {$temp[$ct]{y}=""}
if($values{w}){$temp[$ct]{w}=$values{w}}else {$temp[$ct]{w}=""}
if($values{h}){$temp[$ct]{h}=$values{h}}else {$temp[$ct]{h}=""}
 $ct++;
}
close $fh;	
$ct=0;
say "wh_file = $wh_file";
open ($fh , '<',"$wh_file")or die"$!";		
while (my $line = <$fh>) {
      $csv->parse($line);
      my %values=$csv->fields();
  if(!$values{w}){$text="! file does not have wh data !";$listbox2->insert('end', $text);return;}
 if($values{w}){$temp[$ct]{w}=$values{w}}
 if($values{h}){$temp[$ct]{h}=$values{h}}
 $ct++;
}	
@temp = grep { !/^\.{1,2}$/ } @temp;  # Remove "." and ".." entries
@temp = sort numerically @temp;
my $file="./$save_directory/added_wh.list";
open($fh, '>', $file) or die "Could not open file '$file' $!";
for( my $i=0;$i<=$#temp;$i++){
$temp[$i]{image}//=$temp[$i-1]{image};
$temp[$i]{w}//=$temp[$i-1]{w};
$temp[$i]{h}//=$temp[$i-1]{h};
$temp[$i]{x}//=$temp[$i-1]{x};
$temp[$i]{y}//=$temp[$i-1]{y};
print $fh "image,$temp[$i]{image},w,$temp[$i]{w},h,$temp[$i]{h},x,$temp[$i]{x},y,$temp[$i]{y}\n";
}
close $fh;	
@temp=undef;
$text= "wrote csv $save_directory/added_wh.list";
$listbox2->insert('end', $text);
$text="done";
$listbox2->insert('end', $text);
	}
	
sub add_image{
if(!$selected_dir){$text="select directory of file";$listbox2->insert('end', $text);return;}
my @temp;
# adds images from a list to xy list
&check_dir();
say " xy file =$xy_file";
my $ct=0;
my $csv = Text::CSV_XS->new ();
open ( my $fh , '<',"$xy_file")or die"$!";		
while (my $line = <$fh>) {
      $csv->parse($line);
      my %values=$csv->fields();
 if($values{image}){$temp[$ct]{image}=$values{image}}else{$temp[$ct]{image}="";$text="!there is no image";}
($image_width, $image_height) =imgsize($values{image});
 if($values{x}){$temp[$ct]{x}=$values{x}}else{$temp[$ct]{x}="";}
 if($values{y}){$temp[$ct]{y}=$values{y}}else{$temp[$ct]{y}="";}
 if($values{w}){$temp[$ct]{w}=$values{w}}else{$temp[$ct]{w}="";}
 if($values{h}){$temp[$ct]{h}=$values{h}}else{$temp[$ct]{h}="";}
 $ct++;
}
close $fh;	

$ct=0;
open ($fh , '<',"$image_file")or die"$!";		
while (my $line = <$fh>) {
      $csv->parse($line);
      my %values=$csv->fields();

 if(!$values{image}){$text="! file does not have image data !";$listbox2->insert('end', $text);return;}
 if($values{image}){$temp[$ct]{image}=$values{image}}
 $ct++;
}	
@temp = grep { !/^\.{1,2}$/ } @temp;  # Remove "." and ".." entries
@temp = sort numerically @temp;
for( my $i=0;$i<=$#temp;$i++){
$temp[$i]{image}//=$temp[$i-1]{image};
$temp[$i]{w}//=$temp[$i-1]{w};
$temp[$i]{h}//=$temp[$i-1]{h};
$temp[$i]{x}//=$temp[$i-1]{x};
$temp[$i]{y}//=$temp[$i-1]{y};
#print $fh "image,$temp[$i]{image},w,$temp[$i]{w},h,$temp[$i]{h},x,$temp[$i]{x},y,$temp[$i]{y}\n";
}
my $file="./$save_directory/added_image.list";
open($fh, '>', $file) or die "Could not open file '$file' $!";
for( my $i=0;$i<=$#temp;$i++){
print $fh "image,$temp[$i]{image},w,$temp[$i]{w},h,$temp[$i]{h},x,$temp[$i]{x},y,$temp[$i]{y}\n";
}
close $fh;	
#@temp=undef;
$listbox2->insert('end', $text);
$text= "wrote csv ./$save_directory/added_image.list";
$listbox2->insert('end', $text);
$text="done";
$listbox2->insert('end', $text);
@temp=undef;	
	}
		
sub add_self_wh{
say "here add self";

if(!$selected_file){$text="select a file";$listbox2->insert('end', $text);return}
#my $images= $selected_dir;
my @temp;
# adds wh of the images
&check_dir();
my $ct=0;
my $csv = Text::CSV_XS->new ();
open ( my $fh , '<',"$selected_file")or die"$!";		
while (my $line = <$fh>) {
      $csv->parse($line);
      my %values=$csv->fields();
say "x\b";
if($values{image}){$temp[$ct]{image}=$values{image}}
else {$text="! file does not have image data !";
	$listbox2->insert('end', $text);
	return;
	}
my $file=$temp[$ct]{image};
if (!-e $file){$text= "error with name of file, image file does not exist";$listbox2->insert('end', $text);}
say $file;
($image_width,$image_height) =imgsize($file);
#say "$image_width  $image_height";
$temp[$ct]{w}=$image_width;
$temp[$ct]{h}=$image_height;
if($values{x}){$temp[$ct]{x}=$values{x}}else{$temp[$ct]{x}=""};
if($values{y}){$temp[$ct]{y}=$values{y}}else{$temp[$ct]{y}=""};
$ct++;
}
close $fh;	
#@temp= sort numerically @temp;
@temp = grep { !/^\.{1,2}$/ } @temp;  # Remove "." and ".." entries
@temp = sort numerically @temp;
for( my $i=0;$i<=$#temp;$i++){
$temp[$i]{image}//=$temp[$i-1]{image};
$temp[$i]{w}//=$temp[$i-1]{w};
$temp[$i]{h}//=$temp[$i-1]{h};
$temp[$i]{x}//=$temp[$i-1]{x};
$temp[$i]{y}//=$temp[$i-1]{y};
say Dumper @temp;
my $file="./$save_directory/added_self_wh.list";
open($fh, '>', $file) or die "Could not open file '$file' $!";
for( my $i=0;$i<=$#temp-1;$i++){
print $fh "image,$temp[$i]{image},w,$temp[$i]{w},h,$temp[$i]{h},x,$temp[$i]{x},y,$temp[$i]{y}\n";
}
close $fh;	

$text= "wrote csv ./$save_directory/added_self_wh.list";
$listbox2->insert('end', $text);
$text="done";
$listbox2->insert('end', $text);
@temp=undef;	
	}		
}

sub change_suffix{
$change=$_[0];
if(!$selected_dir){return;}
opendir(my $dh, $selected_dir) or die "Could not open directory: $!";
@files = readdir($dh);
closedir($dh);
@files = grep { !/^\.\.?$/ } @files;
@files= sort numerically @files;
$listbox2->insert('end', @files);
if ($change eq "yes"){
for (my $i=0;$i<$#files+1;$i++){
my ($file,$directories,$suffix) = fileparse($files[$i], qr/\.[^.]*$/); 	
my $text2=join "","$selected_dir","/",$file,".",$new_suffix;
my $text=join "","$selected_dir","/",$file,$suffix;
say "$text  $text2";
rename "$text", "$text2";	
}
$text="change suffix done";
$listbox2->insert('end', $text);
}
}















