Rotate selected images in a folder using VBA and ImageMagick

China☆狼群 提交于 2020-01-06 08:22:21

问题


I want to check a folder if a rotated version of my images exists, if it doesn't I want to use ImageMagick to automatically do it for me (or another program if it would be a better option). This is my code:

Dim imageDomain As String
Dim imagePath As String
Dim rotatePath As String
Dim rotateBool As Boolean
Dim imageRotator As Integer
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT Afbeelding FROM Products")

imageDomain = CurrentProject.Path & "\folder\"

If Not (rs.EOF And rs.BOF) Then
    rs.MoveFirst
    Do Until rs.EOF = True

        If Not IsNull(rs!Afbeelding) Then
            rotatePath = imageDomain & "rotate\" & rs!Afbeelding
            rotateBool = Len(Dir(rotatePath))

            If Not rotateBool Then
                imagePath = imageDomain & rs!Afbeelding
                imageRotator = Shell("cmd.exe /c convert.exe -rotate ""270"" " & imagePath & " " & rotatePath)
            End If
        End If

        rs.MoveNext
    Loop
Else
    MsgBox "There are no records in the recordset."
End If

rs.Close
Set rs = Nothing

The code gives an overflow error at the shell command. How can I selectively rotate the images I want with ImageMagick using VBA? Using a batch file would rotate all images in the folder?


回答1:


The COM+ object did the trick.

Dim img As Object
Set img = CreateObject("ImageMagickObject.MagickImage.1")

In the loop:

imageRotator = img.Convert(imagePath, "-rotate", "270", rotatePath)


来源:https://stackoverflow.com/questions/48094834/rotate-selected-images-in-a-folder-using-vba-and-imagemagick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!