ImageMagick quits when input file absent

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
RandomTroll
Posts: 15
Joined: 2017-12-28T11:38:17-07:00
Authentication code: 1152

ImageMagick quits when input file absent

Post by RandomTroll »

I wrote a program that, among other things, makes a thumbnail
of an image. I use the code from
/usr/doc/ImageMagick-6/www/source/core.c. It returns:

unable to open image `camtwo.jpg': No such file or directory @ error/blob.c/OpenBlob/2589.

and quits. I don't understand why the program quits just because this
subroutine fails. This bit of code runs *only* when camtwo.jpg exists
- is ImageMagick telling me that something else doesn't exist? Or has
camtwo.jpg disappeared in a few microseconds. Not getting a thumbmail
made is tolerable; the program exiting (it runs continually, ideally)
is a drag. Is there a way to stop this? Does MagickError do this? Can I remove it?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: ImageMagick quits when input file absent

Post by snibgo »

What version of IM? The core.c distributed with v6.9.9-50 doesn't check the input file exists, so I don't understand "This bit of code runs *only* when camtwo.jpg exists". If ReadImage fails, the program calls exit(1). You can make it do something else if you want.

I suggest you paste the code here.
snibgo's IM pages: im.snibgo.com
RandomTroll
Posts: 15
Joined: 2017-12-28T11:38:17-07:00
Authentication code: 1152

Re: ImageMagick quits when input file absent

Post by RandomTroll »

Whoops! I forgot the version. ImageMagick 6.9.10-3 Q16 x86_64 2018-06-25 https://www.imagemagick.org

Webcams upload images asynchronously. I use inotify to detect new images then create a thumbnail of each new image after it arrives. This program has run continually for years. Recently an updated webcam is making the error I report in this message error this way every day or two; the others are running as usual. The real problem is probably that something funny is happening with the new webcam's image, but this program shouldn't quit just because making a thumbnail fails.

Code: Select all

static int Thumbnail()
{
  /* This routine creates a thumbnail, ThumbnailFile from WatchedFile, of size Width x Height  */
  /* I copied the code from ImageMagick's 'core.c' example */

  ExceptionInfo *exception;
  Image *image, *images, *thumbnail_image, *thumbnails;
  ImageInfo    *image_info;

  /* Set up the stuff to use ImageMagick's 'Thumbnail' call  */
  exception=AcquireExceptionInfo();
  image_info=CloneImageInfo((ImageInfo *) NULL);
  (void) strcpy(image_info->filename,WatchedFile);
  images=ReadImage(image_info,exception);
  if (images == (Image *) NULL) fprintf(LogFileHandle, "ReadImage failed, WatchedFile: %s\n", WatchedFile);
  if (exception->severity != UndefinedException)
    CatchException(exception);

  /*    Convert the image to a thumbnail.   */

  thumbnails=NewImageList();
  while ((image=RemoveFirstImageFromList(&images)) != (Image *) NULL)
    {
      thumbnail_image=ThumbnailImage(image,Width,Height,exception);
      if (thumbnail_image == (Image *) NULL)
        {
          fprintf(LogFileHandle, "thumbnail_image failed, WatchedFile: %s\n", WatchedFile);
          MagickError(exception->severity,exception->reason,exception->description);
        }
      (void) AppendImageToList(&thumbnails,thumbnail_image);
      DestroyImage(image);
    }
  /*
    Write the image thumbnail.
  */
  (void) strcpy(thumbnails->filename,ThumbnailFile);
  WriteImage(image_info,thumbnails);
  /*
    Destroy the image thumbnail and exit.
  */
  thumbnails=DestroyImageList(thumbnails);
  image_info=DestroyImageInfo(image_info);
  exception=DestroyExceptionInfo(exception);
  return(0);
}
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: ImageMagick quits when input file absent

Post by snibgo »

RandomTrol wrote:I use inotify to detect new images then create a thumbnail of each new image after it arrives.
I don't know "inotify". Does it tell you when a file has been created, or when it has been created, and written to, and closed, and unlocked, so it is ready to be opened and read? I suspect your problem is that the file exists but is still locked in an exclusive mode. A remedy is for your program to sleep for a second or more, and try again.
snibgo's IM pages: im.snibgo.com
RandomTroll
Posts: 15
Joined: 2017-12-28T11:38:17-07:00
Authentication code: 1152

Re: ImageMagick quits when input file absent

Post by RandomTroll »

Code: Select all

man inotify
will tell you what it does.

It's a good guess that the file is unavailable; that would be new behavior for our webcams. There's already a delay such as you suggest.

I asked on this forum how to keep this bit of ImageMagick code from core.c from making the program exit. Does the MagickError function cause the exit? Can I do without it?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: ImageMagick quits when input file absent

Post by snibgo »

"man inotify" would tell me what it does if I had inotify. But I don't. I expect I could search online to answer your question.

The problem is that IM can't open the file. I can't tell why. Can other programs open that file, eg "cp" or "xcopy" or other file-copy utility? Can "convert" open it? What does "ls" or "dir" say about the file?

If they can't open it, but can after a suitable wait, then that's the problem.

You can insert "printf()" at suitable points to show that your program continues after ReadImage has failed. But it will crash when you attempt to use the result from ReadImage.
snibgo's IM pages: im.snibgo.com
RandomTroll
Posts: 15
Joined: 2017-12-28T11:38:17-07:00
Authentication code: 1152

Re: ImageMagick quits when input file absent

Post by RandomTroll »

inotify monitors files and signals the monitoring program when they're created/deleted/written/read/had-privileges-changed... Because the webcams upload asynchronously I can't just set a timer to make thumbnails at fixed intervals.

The problem isn't that IM can't open the file, it's that IM quits when it can't open the file. The program that monitors for new webcam images runs all the time; I don't run it from the command line and monitor its behavior - I can't. The very instruction before I call Thumbnail relies upon the file's existence.

I have a number of printf in the program to report its behavior. That's how I can pin down the problem to this routine. Note that my code has a printf to report that ReadImage has failed. This error never appears, so ReadImage has always worked.

IM is not merely returning an error, which my program should catch, it's barfing and stopping the whole program, not just failing to make a thumbnail image. What's likely to cause the error I get?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: ImageMagick quits when input file absent

Post by snibgo »

RandomTroll wrote:... This error never appears, so ReadImage has always worked.

IM is not merely returning an error, which my program should catch, it's barfing and stopping the whole program, not just failing to make a thumbnail image. What's likely to cause the error I get?
So, you claim that ReadImage works, so it doesn't return NULL, it returns a valid pointer to an image. But then something stops the whole program. Where in your program does that happen?

You have shown an error message that I suppose comes from CatchException(), which you call after ReadImage. So ReadImage has completed, but probably returned NULL. (Do you check for NULL, and announce the result in a printf?)

But you also say:
RandomTroll wrote:...it's that IM quits when it can't open the file
So, does that mean ReadImage doesn't complete? It never returns to your program? If that's the case, then I suggest you add some code before ReadImage. Try to open the file in read mode, binary. If that fails, report it and exit gracefully. But if it works then close it and go ahead with ReadImage.
snibgo's IM pages: im.snibgo.com
Post Reply