Identify and Convert using VB Script

ImageMagickObject is a Windows COM+ interface to ImageMagick. COM+, Visual Basic, and Delphi users should post to this discussion group.
Post Reply
trinityr
Posts: 5
Joined: 2016-08-04T07:58:41-07:00
Authentication code: 1151

Identify and Convert using VB Script

Post by trinityr » 2016-08-04T08:14:55-07:00

I am not sure if Imagemagick can do this or not. I have been unsuccessful so far.

I would like to be able to do the following:

1. Search for files with the file extension of .tif and file size is greater than 0. - This step is done and working
2. If found I need to identify the actual MIME type of the file. We have an export program that is exporting as TIF files when the actual file type is JPEG.
3. If the file MIME type is JPEG, rename the file to .jpg
4. Move onto the next file

I would like to see the output of the identify so the rest can be done with vbscript since an extension rename is all that is needed. The MIME type is already JPEG but the file has and extension of TIF

Currently, I cannot return the results of the identify when I call it thru a command line. When I use a direct call to identify as shown below I get an error.
identity: 400: Perform: Unsupported argument type.

Thoughts, options? Any help is appreciated.

Code: Select all

Option Explicit
	Dim objFSO, objRegEx, objShell, imageMagick
		Set objFSO = CreateObject("Scripting.FileSystemObject")
		Set objRegEx = CreateObject("Vbscript.RegExp")
		Set objShell = CreateObject("Wscript.Shell")
		Set imageMagick = CreateObject("ImageMagickObject.MagickImage.1")
		
	Dim strPost
		strPost = "ExportBug"
	
	Dim objFolder, colFiles, objFile
	Dim Result

Set objFolder = objFSO.GetFolder(strPost)
		Set colFiles = objFolder.Files
			For Each objFile in colFiles
				If LCase(objFSO.GetExtensionName(objFile.Name)) = "tif" and objFile.size > 0 Then
					MsgBox objFile.name
					Result = imageMagick.identify(objFile)
					MsgBox Result
				End If
			Next
		Set objFolder = Nothing
		Set colFiles = Nothing

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

Re: Identify and Convert using VB Script

Post by snibgo » 2016-08-04T08:21:13-07:00

I'm probably wrong, but perhaps "imageMagick.identify(objFile.Name))".
snibgo's IM pages: im.snibgo.com

trinityr
Posts: 5
Joined: 2016-08-04T07:58:41-07:00
Authentication code: 1151

Re: Identify and Convert using VB Script

Post by trinityr » 2016-08-04T08:40:33-07:00

That did resolve the error, but empty result window.

trinityr
Posts: 5
Joined: 2016-08-04T07:58:41-07:00
Authentication code: 1151

Re: Identify and Convert using VB Script

Post by trinityr » 2016-08-04T09:19:03-07:00

Updated the script to use convert instead of identify.

Code: Select all

Option Explicit
	Dim objFSO, objRegEx, objShell, img
		Set objFSO = CreateObject("Scripting.FileSystemObject")
		Set objRegEx = CreateObject("Vbscript.RegExp")
		Set objShell = CreateObject("Wscript.Shell")
		Set img = CreateObject("ImageMagickObject.MagickImage.1")
		
	Dim strPost
		strPost = "ExportBug"
	
	Dim objFolder, colFiles, objFile
	Dim Result

Set objFolder = objFSO.GetFolder(strPost)
		Set colFiles = objFolder.Files
			For Each objFile in colFiles
				If LCase(objFSO.GetExtensionName(objFile.Name)) = "tif" and objFile.size > 0 Then
					MsgBox objFile.name
					Result = img.convert(objFile.name, "-format","%m","info:")
					MsgBox Result
				End If
			Next
		Set objFolder = Nothing
		Set colFiles = Nothing

spieler
Posts: 47
Joined: 2004-10-08T09:03:16-07:00
Location: Iowa

Re: Identify and Convert using VB Script

Post by spieler » 2016-08-04T09:30:45-07:00

That's a great workaround. I might have to give that a try!

That sounded vaguely familiar, so I found my previous post on it. Feel free to reply to it, but don't hold your breath for a response.
viewtopic.php?f=3&t=8550

trinityr
Posts: 5
Joined: 2016-08-04T07:58:41-07:00
Authentication code: 1151

Re: Identify and Convert using VB Script

Post by trinityr » 2016-08-04T19:58:04-07:00

I'm not sure if this is a bug or working as intended, but I am only able to use the COM object from current directory and must use objFile.name. When trying to use objFile or search subfolders of a given directory I receive:
error 435 unable to open image 'test image (1).tif': No such file or directory @ error/blob.c/OpenBlob/2695: convert: 410: no images defined `info:' @ error/convert.c/ConvertImageCommand/3253: Code: 80041771 Source ImageMagickObject.MagickImage.1

Code: Select all

Option Explicit

Const ERROR_SUCCESS = 0

	Dim objFSO, objRegEx, objShell, img, objArgs, objExplorer
		Set objFSO = CreateObject("Scripting.FileSystemObject")
		Set objRegEx = CreateObject("Vbscript.RegExp")
		Set objShell = CreateObject("Wscript.Shell")	
	Dim strPost
		'strPost = objShell.CurrentDirectory
		strPost = "\\server\path\ExportBug\Test\Batch_1"
		
		Dim objFolder, colFiles, objFile, objStdOut, Subfolder
		Dim Result, NewName, StartTime, EndTime
	
		Dim strConversionLog
			strConversionLog = "_TIFF_to_JPG.log"

		Set objStdOut = objFSO.OpenTextFile(strConversionLog,2,True)
			StartTime = Timer()
	
Set objFolder = objFSO.GetFolder(strPost)
		Set colFiles = objFolder.Files
			For Each objFile in colFiles
				If LCase(objFSO.GetExtensionName(objFile.Name)) = "tif" and objFile.size > 0 Then
					If Err.Number <> ERROR_SUCCESS Then ShowError: WScript.Quit
					Result = img.convert(objFile.name, "-format","%m","info:")
						If Result = "JPEG" Then
							If Err.Number <> ERROR_SUCCESS Then ShowError: WScript.Quit
								NewName = objFSO.GetBaseName(objFile.name) & ".jpg"
									objStdOut.Write objFile.name & vbCrLf
									objFSO.MoveFile objFile, NewName
						End If
				End If
			Next
			
		Set objFolder = Nothing
		Set colFiles = Nothing
	EndTime = Timer()
	objStdOut.Write(FormatNumber(EndTime-StartTime,2))
	objStdOut.Close()
	
Sub ShowError
  sMsgs = ""
  If BasicError(Err.Number) > 5000 Then
    sMsgs = " ImageMagickObject" & vbCrLf
  End If
  WScript.Echo Err.Number & ": " & Err.Description & vbCrLf & sMsgs
End Sub

Function BasicError(e)
  BasicError = e And &HFFFF&
End Function	
			
		

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

Re: Identify and Convert using VB Script

Post by snibgo » 2016-08-04T20:33:21-07:00

Code: Select all

unable to open image 'test image (1).tif'
So, you are passing only the filename, without the drive and directory, to "convert". I expect VB documentation will show you how to append the drive and directory.
snibgo's IM pages: im.snibgo.com

trinityr
Posts: 5
Joined: 2016-08-04T07:58:41-07:00
Authentication code: 1151

Re: Identify and Convert using VB Script

Post by trinityr » 2016-08-05T13:18:53-07:00

If you notice the code attached is using a full UNC path to search thru. The attached script fails with that error. However, if I set strPost to current directory it works like a charm. Taking that out of the equation and hardsetting the drive and path with objfile.name returns the same error as well.

Code: Select all

img.convert("C:\test\"&objFile.name,"-format", "%m","info:")

spieler
Posts: 47
Joined: 2004-10-08T09:03:16-07:00
Location: Iowa

Re: Identify and Convert using VB Script

Post by spieler » 2016-08-08T10:17:12-07:00

I cannot explain why you are getting the error on your last example unless that file truly does not exist. I ran a script from one directory passing the path to a file in another directory and it worked fine.

Instead of using the Name property, you can use the Path property and then you shouldn't have to worry about the current directory.

Code: Select all

Result = img.convert(objFile.path, "-format","%m","info:")

Post Reply