Create missing thumbnails within a folder tree

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
Skippy Grey
Posts: 5
Joined: 2018-01-11T17:00:25-07:00
Authentication code: 1152

Create missing thumbnails within a folder tree

Post by Skippy Grey » 2018-01-11T18:33:45-07:00

I am trying to create thumbnail images in a subfolder to where the photos are and I would like to specify which photos to create thumbnails for in a text file.

I don’t want to generate thumbnails for all the photos in a folder as in most cases, this has already occurred. Sometimes a new photo is placed into an existing folder, such as a cover image for a video and I only need to create thumbnails for the new images. I already have a way of creating a list of files that need to have thumbnails created. However I can’t get a windows command line string working that reads the file and creates the desired thumbnails. A second issue is that I do not yet know how to take the path of the original image and use this path as a base path for saving the output file.

I have developed an image information system based on ms-access. When photos are imported into a the underlying folder structure, I create thumbnails for the photos and put these in a thumbs subdirectory inside the same folder as the photos. An example is below.

F:\2017\2017-12-19\Card_3034-3534\ is where the photos are; and
F:\2017\2017-12-19\Card_3034-3534\Thumbs\ is where the thumbnails are.

In my tests, the command below works, but the input file is hardwired as is the path for the output.

Code: Select all

C:\ImageMagick-7.0.7-21-portable-Q16-x64\magick convert "C:\Temp\ImageMagickTest\IMG_2547.JPG" 
-resize 640x480 -set filename:f "%t" 
"C:\Temp\ImageMagickTest\Thumbs\%[filename:f].jpg"
If I try to use the @file syntax, things do not work

Code: Select all

C:\ImageMagick\ImageMagick-7.0.7-21-portable-Q16-x64\magick convert @F:\PhotoSessions\PhotoHarvester\Pages\NeedThumbsTest.txt 
-resize 640x480 -set filename:f "%t" 
"C:\Temp\ImageMagickTest\Thumbs\%[filename:f].jpg"
I get the following error message for each file listed in the text file:
convert: UnableToOpenBlob '@F:C:\Temp\ImageMagickTest\IMG_2547.JPG': Invalid argument @ error/blob.c/OpenBlob/3335.

Can you please tell me what this error means. Could you also please tell me how to get the path from each input file and use that path as a base for each output file.

In the text file which specifies the filenames, I am assuming that the files do not have to be in the same directory. It would be great to scan a folder tree, build up a list of missing thumbnails in a text file (in VBA) and then run ImageMagick which reads the text file and then generates all the thumbnails.

Skippy

User avatar
fmw42
Posts: 24156
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Create missing thumbnails within a folder tree

Post by fmw42 » 2018-01-11T18:44:12-07:00

F:C:
You are referencing two volumes. I am not sure that is valid.

I do not know if Imagemagick allows you to get the file from a different volume. I don't know Windows that well. But try adding double quotes.

"@F:\PhotoSessions\PhotoHarvester\Pages\NeedThumbsTest.txt"

I suspect @ expects the images to be in the same volume as the txt file. That may be why it is give the error with F:C:

snibgo
Posts: 10978
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Create missing thumbnails within a folder tree

Post by snibgo » 2018-01-11T18:49:33-07:00

1. You are using v7. I suggest you use "magick" or "magick mogrify", not "magick convert".

2. In the "@filename" construct, IM gets confused if filename starts with a drive such as "F:". A directory is fine, but not a drive. (The image files named within that text file can contain a drive.)

3. Your command would read through the input images and create a single output image. That's not what you want. You could use "magick mogrify". Personally, I would write a shell script with "for" to read the lines of \PhotoSessions\PhotoHarvester\Pages\NeedThumbsTest.txt
and call magick once per line.
snibgo's IM pages: im.snibgo.com

Skippy Grey
Posts: 5
Joined: 2018-01-11T17:00:25-07:00
Authentication code: 1152

Re: Create missing thumbnails within a folder tree

Post by Skippy Grey » 2018-01-11T20:02:09-07:00

Thanks for your replies. I can try putting the text file on my C: Drive for a start. The text file is being read as it echos the file names within the text file.

I am trying to avoid calling Magick once per line as this would incur an overhead of starting the application each time, would it not. I could write such a script in VBA but have to use a shellwait procedure and not the shell command as the shell command would loop and create hundreds of instances of Magick. I am not a great shell script writer either.

Did the XML bases scripting language ever get developed? That sounds like a good potential approach?

Finally, was there a way to use the path of the input image to specify the path of the output image?

Hopefully, I will get over the hump.

Skippy Grey
Posts: 5
Joined: 2018-01-11T17:00:25-07:00
Authentication code: 1152

Re: Create missing thumbnails within a folder tree

Post by Skippy Grey » 2018-01-11T20:29:56-07:00

I can get the cmd to work if I remove the path from @filename construct. The file names inside the file can have paths. That opens the possibility of making a very short shell script that CD's to the current directory (where the list of file names is) and then runs the convert command, which uses the text file of field names.

Skippy Grey
Posts: 5
Joined: 2018-01-11T17:00:25-07:00
Authentication code: 1152

Re: Create missing thumbnails within a folder tree

Post by Skippy Grey » 2018-01-11T23:23:12-07:00

Here is the procedure that I wrote in VBA (ms-access or ms-excel) which regenerates missing thumbnails in folders. I have run it on over 1000 folders so it works in my application. You will need to adjust the code below a little bit to replace some of my user defined functions.

Code: Select all

Public Function MakeMagickThumbsCmd(FileList As String, Folder As String) As String

    On Error GoTo procerr
    
    Dim fso As Object
    Dim w As Object

    Dim args As String
    Dim appExe As String
    Dim destination As String
    Dim fileArgs As String
    Dim fileListPath As String
    Dim outputFileName As String
    
    outputFileName = GetTempFolder() & "NeedThumbsShellCmd.bat"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set w = fso.CreateTextFile(outputFileName, True)
    
    appExe = GetImageMagick()  'This function just returns the path of to ImageMagick - can replace with hardwired filepath
    args = " -resize 640x480 -set filename:f " & Quote & "%%t" & Quote
    fileListPath = Left(FileList, InStrRev(FileList, "\"))
    fileArgs = "@" & Mid(FileList, InStrRev(FileList, "\") + 1)
    destination = Folder & "Thumbs\"
    
    w.writeline "CD " & fileListPath
    w.writeline appExe & "magick convert " & fileArgs & Space(1) & args & Space(1) & destination & "%%[filename:f].jpg "
    Set fso = Nothing
    
    MakeMagickThumbsCmd = outputFileName
    shell(outputFileName)
	
exitproc:
    Exit Function

procerr:
    If Developing() Then
        Stop
        Resume
    Else
        Call NewErrorLog(err.Number, err.Description, "MakeMagickThumbsCmd")
        Resume exitproc
    End If
    
End Function
The code that finds the missing thumbnails is as follows:

Code: Select all

Public Function NeedMagickThumbs(Folder As String) As String

    'Checks whether thumbnails already exist

    On Error GoTo procerr    PushStack ("NeedMagickThumbs")
    Dim fso As Object
    Dim fsoFile As Object
    Dim w As Object
    
    Dim thumbsMissing As Long
    Dim outputFileName As String
    Dim thumbFile As String
    
    outputFileName = GetTempFolder() & "NeedThumbs.txt"  'replace GetTempFolder() with the location of your tempfolder
    Folder = TrailingSlash(Folder)  'user defined function that ensures folder has a trailing slash
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set w = fso.CreateTextFile(outputFileName, True)

    For Each fsoFile In fso.GetFolder(Folder).files
        If fso.GetExtensionName(fsoFile) = "jpg" Then
            thumbFile = Folder & "Thumbs\" & fsoFile.name
            If Not fso.FileExists(thumbFile) Then
                w.writeline Folder & fsoFile.name
                thumbsMissing = thumbsMissing + 1
            End If
        End If
    Next
    
    If thumbsMissing Then
        NeedMagickThumbs = outputFileName  'I check if a filename has been returned to the calling procedure and if yes, then run the above procedure in the top box.
    End If
    
exitproc:
    Set fso = Nothing
    PopStack
    Exit Function

procerr:
    If err.Number = 76 Then 'path not found
        Resume exitproc
    ElseIf Developing() Then
        Stop
        Resume
    Else
        Call NewErrorLog(err.Number, err.Description, "NeedMagickThumbs")
        Resume exitproc
    End If

End Function

Skippy Grey
Posts: 5
Joined: 2018-01-11T17:00:25-07:00
Authentication code: 1152

Re: Create missing thumbnails within a folder tree

Post by Skippy Grey » 2018-01-13T17:55:12-07:00

I am finding that ImageMagick is very fast when there are a few missing thumbnails and very slow/several minutes when there are a lot (50+). I assume this has to do with system resources. I would like to find a way of echoing to screen what IM is doing and also find a way to limit resource use.

User avatar
fmw42
Posts: 24156
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Create missing thumbnails within a folder tree

Post by fmw42 » 2018-01-13T19:13:43-07:00

Probably use -verbose (or -debug all) in your command line to see what is going on. For resources, edit your IM policy.xml file. To see resources use

Code: Select all

convert -list resource
see
https://www.imagemagick.org/Usage/basics/#controls
https://www.imagemagick.org/script/resources.php

Post Reply