File: AMC-getimages.pl

package info (click to toggle)
auto-multiple-choice 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,520 kB
  • sloc: perl: 23,839; xml: 20,920; cpp: 1,791; makefile: 472; ansic: 186; sh: 80
file content (567 lines) | stat: -rwxr-xr-x 15,097 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#! /usr/bin/perl -w
#
# Copyright (C) 2012-2017 Alexis Bienvenue <paamc@passoire.fr>
#
# This file is part of Auto-Multiple-Choice
#
# Auto-Multiple-Choice is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 2 of
# the License, or (at your option) any later version.
#
# Auto-Multiple-Choice is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Auto-Multiple-Choice.  If not, see
# <http://www.gnu.org/licenses/>.

use AMC::Basic;
use AMC::Gui::Avancement;

use File::Spec::Functions qw/splitpath catpath splitdir catdir catfile rel2abs tmpdir/;
use File::Temp qw/ tempdir /;
use File::Copy;

use Getopt::Long;

use_gettext;
binmode(STDOUT, ":utf8");

my $list_file='';
my $progress_id='';
my $copy_to='';
my $debug='';
my $vector_density=300;
my $orientation="";
my $rotate_direction="90";
my $force_convert=0;
my %use=(pdfimages=>1,pdftk=>1,qpdf=>1);

GetOptions("list=s"=>\$list_file,
	   "progression-id=s"=>\$progress_id,
	   "copy-to=s"=>\$copy_to,
	   "debug=s"=>\$debug,
	   "vector-density=s"=>\$vector_density,
	   "orientation=s"=>\$orientation,
	   "rotate-direction=s"=>\$rotate_direction,
	   "use-pdfimages!"=>\$use{pdfimages},
	   "use-pdftk!"=>\$use{pdftk},
           "use-qpdf!"=>\$use{qpdf},
	   "force-convert!"=>\$force_convert,
	  );

set_debug($debug);

$use{pdfimages}=0 if($force_convert);

# cancels use of pdfimages/pdftk if these commands are not available
# on the system

for my $cmd (qw/pdfimages pdftk qpdf/) {
  if($use{$cmd} && !commande_accessible($cmd)) {
    debug "WARNING: command $cmd not found";
    $use{$cmd}=0;
  }
}

# delete trailing / in the --copy-to directory option

$copy_to =~ s:(?<=.)/+$::;

# filenames of scan files are handled by a hashref that reminds if the
# file has already been processed: in this hash,
#
# * path is the whole path of the scan,
# * dir is the directory part of the path
# * file is the file part of the path
# * orig is 1 if the file is the original scan

# original_file builds a hashref for a original scan file (not yet
# processed or split or moved)

sub original_file {
  my ($file_path)=@_;
  utf8::downgrade($file_path);
  return({ path=>$file_path,orig=>1 });
}

# derivative_file builds a hashref for a file that has already been
# processed in some way by AMC-getimages

sub derivative_file {
  my ($file_path)=@_;
  return({ path=>$file_path });
}

# check_split_path($f,$force) computes (if $force is set, or if not
# yet already done) the dir and file parts of the path

sub check_split_path {
  my ($f,$force)=@_;
  if($f->{path} && ($force || ! $f->{file})) {
    my ($fxa,$fxb,$file)=splitpath($f->{path});
    $f->{file}=$file;
    $f->{dir}=catpath($fxa,$fxb,'');
  }
}

# variables to show progress

my $dp;
my $p;
$p=AMC::Gui::Avancement::new(1,'id'=>$progress_id)
  if($progress_id);

# image_size computes the image size (width,height) using 'identify'
# from ImageMagick/GraphicsMagick

sub image_size {
  my ($file)=@_;
  my @r=();
  my @cmd=(magick_module("identify"),"-format","%w,%h\n",$file);
  if(open(IDF,"-|",@cmd)) {
    while(<IDF>) {
      chomp();
      @r=($1,$2) if(/([^,]+),(.*)/);
    }
    close(IDF);
  } else {
    debug("CMD: ".join(' ',@cmd));
    debug("ERROR: $!");
  }
  return(@r);
}

# image_orientation returns "portrait", "landscape" or "" (in
# indetermined cases) depending on the image orientation.

sub image_orientation {
  my ($file)=@_;
  my ($w,$h)=image_size($file);
  if(defined($h) && defined($w)) {
    return( $h>1.1*$w ? "portrait" : $w>1.1*$h ? "landscape" : "");
  } else {
    debug "WARNING: undefined image orientation for $file";
    return(undef);
  }
}

# move_derivative($origin,$derivative) moves the file descried by
# $derivative to the same directory as $origin (changing its name if a
# file with this name already exists), and then updates $derivative to
# point to the new location, and returns it.

sub move_derivative {
  my ($origin,$derivative)=@_;
  if(!$copy_to) {
    check_split_path($origin);
    check_split_path($derivative);
    my $dest=new_filename($origin->{dir}."/".$derivative->{file});
    debug "Moving $derivative->{path} to $dest";
    if(!move($derivative->{path},$dest)) {
      debug_and_stderr "File move failed: $dest";
    }
    $derivative->{path}=$dest;
    check_split_path($derivative,1);
  }
  return($derivative);
}

# replace_by($origin,@derivative_paths) is called after a scan file
# described by $origin has been split in several files. It moves the
# derivative files to the same directory has the origin, deletes the
# $origin file if it is not an original scan file (has already been
# processed in some way), and returns the derivatives files
# descriptions array.

sub replace_by {
  my ($origin,@derivative_paths)=@_;
  my @fd=map { move_derivative($origin,derivative_file($_)) }
    @derivative_paths;
  unlink $origin->{path}
    if(!$origin->{orig});
  return(@fd);
}

###################################################################
# STEP 0: collects all original scan provided as arguments of the
# command, or in a file

my @f=map { original_file($_) } (@ARGV);

if(-f $list_file) {
  open(LIST,$list_file);
  while(<LIST>) {
    chomp;
    push @f,original_file($_);
  }
  close(LIST);
}

###################################################################
# STEP 1: split multi-page PDF with pdfimages, pdftk or qpdf, which use
# less memory than ImageMagick

if($use{pdfimages} || $use{pdftk} || $use{qpdf}) {

  # @pdfs is the list of PDF files

  my @pdfs=grep { $_->{path} =~ /\.pdf$/i } @f;

  # @fs will collect all split pages from PDF files. It is initialized
  # with the list of non-PDF files.

  @fs=grep { $_->{path} !~ /\.pdf$/i } @f;

  # starts PDFs processing...

  if(@pdfs) {
    $dp=1/(1+$#pdfs);
    $p->text(__("Splitting multi-page PDF files...")) if($p);

  PDF:for my $file (@pdfs) {

      $p->progres($dp) if($p);

      # makes a temporary directory to extract images from the PDF

      my $temp_loc=tmpdir();
      my $temp_dir;

      check_split_path($file);

      # First, try pdfimages, which is much more judicious

      if($use{pdfimages}) {
        $temp_dir = tempdir( DIR=>$temp_loc,
                             CLEANUP => (!get_debug()) );
        debug "PDF split tmp dir / pdfimages: $temp_dir";

	if(system("pdfimages","-p",$file->{path},
		  $temp_dir.'/'.$file->{file}.'-page')==0) {

	  opendir(my $dh, $temp_dir)
	    || debug "can't opendir $temp_dir: $!";
	  my @images=map { "$temp_dir/$_" }
	    sort { $a cmp $b } grep { /-page-/ } readdir($dh);
	  closedir $dh;

	  if(@images) {
	    # pdfimages produced some files. Check that the page
	    # numbers follow each other starting from 1

	    my $ok=1;
	  PDFIM: for my $i (0..$#images) {
	      if($images[$i] =~ /-page-([0-9]+)/) {
		my $pp=$1;
		if($pp != $i+1) {
		  debug "INFO: missing page ".($i+1)." from pdfimages";
		  $ok=0;
		  last PDFIM;
		}
	      }
	    }
	    if($ok) {
	      debug "pdfimages ok for $file->{file}";
	      push @fs,replace_by($file,@images);
	      next PDF;
	    }
	  } else {
	    debug "INFO: pdfimages produced no file";
	  }

	} else {
	  debug "ERROR while trying pdfimages: [$?] $!";
	}
      }

      # Second, try qpdf
      if($use{qpdf}) {
        $temp_dir = tempdir( DIR=>$temp_loc,
                             CLEANUP => (!get_debug()) );
        debug "PDF split tmp dir / qpdf: $temp_dir";

        if(system("qpdf",$file->{path},"--split-pages",$temp_dir.'/'.$file->{file}.'-page-%d.pdf')==0) {

          opendir(my $dh, $temp_dir)
            || debug "can't opendir $temp_dir: $!";
          my @images=map { "$temp_dir/$_" }
            sort { $a cmp $b } grep { /-page-/ } readdir($dh);
          closedir $dh;

          if(@images) {

            debug "qpdf ok for $file->{file}";
            push @fs,replace_by($file,@images);
            next PDF;

          } else {
            debug "INFO: qpdf produced no file";
          }

        } else {
          debug "ERROR while trying qpdf: [$?] $!";
        }
      }

      # If not successful with pdfimages and qpdf, use pdftk

      if($use{pdftk}) {
        $temp_dir = tempdir( DIR=>$temp_loc,
                             CLEANUP => (!get_debug()) );
        debug "PDF split tmp dir / pdftk: $temp_dir";

	if(system("pdftk",$file->{path},"burst","output",
		  $temp_dir.'/'.$file->{file}.'-page-%04d.pdf')==0) {

	  opendir(my $dh, $temp_dir)
	    || debug "can't opendir $temp_dir: $!";
	  my @burst=replace_by($file,
			       map { "$temp_dir/$_" }
			       sort { $a cmp $b } grep { /-page-/ } readdir($dh));
	  closedir $dh;

	  if(@burst) {
	    push @fs,@burst;
	    next PDF;
	  } else {
	    debug "WARNING: pdftk produced no file";
	  }

	} else {
	  debug "ERROR while trying pdftk burst: [$?] $!";
	}
      }

      # no success... keep the PDF to be processed by magick

      push @fs,$file;

    }

    $p->text('') if($p);
  }

  # To end, replaces the file list

  @f=@fs;
}

###################################################################
# STEP 2: split other multi-page images (such as TIFF) with
# ImageMagick, and convert vector to bitmap

@fs=();
for my $fich (@f) {
  check_split_path($fich);

  if($fich->{file} =~ /\.(pdf|eps|ps)$/i
     && commande_accessible('gs')) {
    # ghostscript is preferred for PDF, EPS and PS files.

    my $temp_loc=tmpdir();
    my $temp_dir = tempdir( DIR=>$temp_loc,
			    CLEANUP => (!get_debug()) );

    debug "GS split tmp dir: $temp_dir";
    my $fb = $fich->{file};
    if(! ($fb =~ s/\.([^.]+)$/_%04d.$1/)) {
      $fb .= '_%04d';
    }
    $fb.=".png";

    my @cmd=("gs","-sDEVICE=png16m","-sOutputFile=$temp_dir/$fb",
	     "-r$vector_density",
	     "-dNOPAUSE","-dSAFER","-dBATCH");
    push @cmd,"-dQUIET" if(!$debug);
    system(@cmd,$fich->{path});

    opendir(my $dh, $temp_dir) || debug "can't opendir $temp_dir: $!";
    push @fs,replace_by($fich,
			grep { -f "$_" } map { "$temp_dir/$_" }
			sort { $a cmp $b } readdir($dh));
    closedir $dh;
    
  } else {
    # Image files, that maybe need some processing...

    my $suffix_change='';
    my @pre_options=();

    # number of pages :
    my $np=0;
    # any scene with number > 0 ? This may cause problems with OpenCV
    my $scene=0;
    open(NP,"-|",magick_module("identify"),"-format","%s\n",$fich->{path});
    while(<NP>) {
      chomp();
      if(/[^\s]/) {
	$np++;
	$scene=1 if($_ > 0);
      }
    }
    close(NP);
    # Is this a vector format file? If so, we have to convert it
    # to bitmap
    my $vector='';
    if($fich->{file} =~ /\.(pdf|eps|ps)$/i) {
      $vector=1;
      $suffix_change='.png';
      @pre_options=('-density',$vector_density)
	  if($vector_density);
    }

    # With ImageMagic, remove alpha channel to get white background
    if(!use_gm_command()) {
      push @pre_options,"-alpha","remove";
    }

    debug "> Scan $fich->{path}: $np page(s)".($scene ? " [has scene>0]" : "");
    if($np>1 || $scene || $vector || $force_convert) {
      # split multipage image into 1-page images, and/or convert
      # to bitmap format

      if($p) {
	if($vector) {
	  # TRANSLATORS: Here, %s will be replaced with the path of a file that will be converted.
	  $p->text(sprintf(__("Converting %s to bitmap..."),$fich->{file}));
	} elsif($np>1) {
	  # TRANSLATORS: Here, %s will be replaced with the path of a file that will be splitted to several images (one per page).
	  $p->text(sprintf(__("Splitting multi-page image %s..."),$fich->{file}));
	} elsif($scene || $force_convert) {
	  # TRANSLATORS: Here, %s will be replaced with the path of a file that will be splitted to several images (one per page).
	  $p->text(sprintf(__("Processing image %s..."),$fich->{file}));
	}
      }

      my $temp_loc=tmpdir();
      my $temp_dir = tempdir( DIR=>$temp_loc,
			      CLEANUP => (!get_debug()) );

      debug "Image split tmp dir: $temp_dir";

      my $fb = $fich->{file};
      if(! ($fb =~ s/\.([^.]+)$/_%04d.$1/)) {
	$fb .= '_%04d';
      }
      $fb.=$suffix_change;

      system(magick_module("convert"),
	     @pre_options,$fich->{path},"+adjoin","$temp_dir/$fb");
      opendir(my $dh, $temp_dir) || debug "can't opendir $temp_dir: $!";
      push @fs,replace_by($fich,
			  grep { -f "$_" } map { "$temp_dir/$_" }
			  sort { $a cmp $b } readdir($dh));
      closedir $dh;

      $p->text('') if($p);
    } else {
      # no coversion needed...
      push @fs,$fich;
    }
  }
}

@f=@fs;

###################################################################
# STEP 3: check files orientation (if requested) and rotate them 90°
# if needed.

if($orientation) {

  my $temp_dir = tempdir( DIR=>tmpdir(),
			  CLEANUP => (!get_debug()) );

  @fs=();
  for my $fich (@f) {
    my $o=image_orientation($fich->{path});
    if($o && $o ne $orientation) {
      check_split_path($fich);

      debug "Rotate scan file $fich->{path} to orientation $orientation";
      my $dest=new_filename($temp_dir.'/rotated-'.$fich->{file});
      my @cmd=(magick_module("convert"),
	       $fich->{path},
	       "-rotate",$rotate_direction,
	       $dest);
      debug "CMD: ".join(' ',@cmd);

      if(system(@cmd)==0) {
	push @fs,replace_by($fich,$dest);
      } else {
	debug "Error while rotating $fich->{path}: $?";
	push @fs,$fich;
      }
    } else {
      push @fs,$fich;
    }
  }
  @f=@fs;

}

###################################################################
# STEP 4: if requested, copy files to project directory

if($copy_to && @f) {
  $p->text(__"Copying scans to project directory...") if($p);

  $dp=1/(1+$#f);

  my @fl=();
  my $c=0;
  for my $fich (@f) {
    check_split_path($fich);

    # no accentuated or special characters in filename, please!
    # this could break the process somewere...
    my $fb=string_to_filename($fich->{file},'scan');

    my $dest=$copy_to."/".$fb;
    utf8::downgrade($dest);

    my $deplace=0;

    if($fich->{path} ne $dest) {
      if(-e $dest) {
	# dest file already exists: change name
	debug "File $dest already exists";
	$dest=new_filename($dest);
	debug "--> $dest";
      }
      if(copy($fich->{path},$dest)) {
	push @fl,derivative_file($dest);
	$deplace=1;
      } else {
	debug "$fich->{path} --> $dest";
	debug "COPY ERROR: $!";
      }
    }
    $c+=$deplace;
    push @fl,derivative_file($fich->{path})
      if(!$deplace);

    $p->progres($dp) if($p);
  }
  debug "Copied scan files: ".$c."/".(1+$#f);
  @f=@fl;

  $p->text('') if($p);
}

###################################################################
# STEP 5: updates the files list with processed files names

if($list_file) {
  open(LIST,">",$list_file);
  for(@f) {
    print LIST $_->{path}."\n";
  }
  close(LIST);
} else {
  debug "WARNING: no output list file requested";
}