Octave script bug for checking jpg image validity

In this posting Octave script - detecting bad images - error running - Unix & Linux Stack Exchange the script continues to give “error: invalid empty index expression” in line 18 (f = fname{}; ) Cannot see where it is going wrong. Grateful for help.

Usage eg ./badimage a.jpg

The script is:

#!/usr/bin/octave -qf

threshold = 0.25;

usage = "Usage is: badimage <file...> OR badfiles <errdir> <baddir> <file...>\n";
assert(nargin>0, usage);
dryrun = isfile(argv{1});
if !dryrun
  errdir = argv{1};
  baddir = argv{2};
  assert(isfolder(errdir) && isfolder(baddir) && isfile(argv{3}), usage);
endif

start = 1 + 2*(!dryrun);
for fname = argv()(start:end)'
  q = [];
  f = fname{};

  warning error
  try
    q = imread(fname{});
  catch err
  end_try_catch
  warning on

  if isempty(q)
    printf("error\t");
    dryrun || movefile(f, errdir);
  else
    qt = all(q == q(end,1,:) ,2);
    qtt = squeeze(all(qt, 3));
    r = 1 - find(qtt==0, 1, 'last') / size(q, 1);
    if (r > threshold)
      printf("bad--%02d\t", ceil(100*r));
      dryrun || movefile(f, baddir);
    else
      printf("good-%02d\t", ceil(100*r));
    endif
  endif

  disp(f);
endfor

I don’t remember which version exactly, but probably since Octave 5 or 6, index expressions can’t be empty.
You’d probably need to replace the respective lines in your script with something like the following:

  f = fname{1};
  
  warning error
  try
    q = imread(fname{1});
  catch err
  end_try_catch

The program runs successfully (with thanks). As a corollary to this, the Octave GUI steals focus in Mac Terminal window each file check. Is there a way to stop this behaviour within the program code?

Please file a bug report on the bug tracker so that this doesn’t get lost

1 Like

Does Octave start with GUI by default on Mac?
Does it make any difference if you change the shebang to the following?

#!/usr/bin/octave -qf --no-gui

Many thanks - yes, adding --no-gui-libs to the shebang fixed it