Post by (X) on Oct 10, 2023 3:16:20 GMT 1
Have you ever wanted to convert > 200 SVG format images of Nation Flags to PNG format?
I experimented with calling InkScape to accomplish this task. (Obviously, you will need have InkScape installed.)
I was surprised to learn the slowest part was due to the lag of the disk file system posting the existence of the converted file and then it's size!
Maybe there is a way to speed that part up but chatgpt doesn't seem to know.
'#################################################################################
'
' DISCLAIMER: THIS CODE IS OFFERED AS IS FOR EDUCATIONAL PURPOSES.
' YOU ARE FREE TO USE, COPY AND DISTRIBUTE.
' THE USE OF THIS CODE IS AT YOUR OWN RISK.
' I ACCEPT NO LIABILITY NOR MAKE ANY CLAIM OF
' CORRECTNESS OR SUITABILITY IN PART OR IN WHOLE.
'
'#################################################################################
'
' FILENAME Demo SVG to PNG.G32
'
' DESCRIPTION Can we call Inkscape to convert an SVG file to PNG?
' This demo speaks for itself.
'
' AUTHOR (X)
' EMAIL xman.gb32@gmail.com
' WEBSITE https:\\gfabasic32.blogspot.com\
' FORUM https:\\gb32.proboards.com\
'
' STARTED 2023-10-08 Fiddled around for some time with the InkScape.exe
' parameters... Finally; running inkscape in a cmd window with -?
' or help parameter yielded all I needed to know.
' Tried a variety of possiblilities using: Shell, ShellExec, Exec
' until I finally got a successful file conversion using the Exec command.
'
' UPDATED 2023-10-09 Trying to perform a batch conversion of a collection
' of nation flags. I was surprised of the 2-3 sec lag of the disk file system
' in posting the file name and file size.
'
'
'$ManifestOff
$Library "GfaWinX"
$Library "UpdateRT"
UpdateRuntime
P_Main
Proc P_Main
Mode Date "-"
LoadForm frm1
frm1.Caption = App.Name
DoEvents
P_Convert_SVG2PNG_Files
Do
Sleep
Until Me Is Nothing
If !IsNothing(frm1) frm1.Close
EndProc
Proc P_Convert_SVG2PNG_Files
sb1.SimpleText = "Conversion Start:" & DateTime$
Dim SourceDirectory$ = App.Path
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Use the Dir$() function to sequentially go through all the SVG files
' in the specified directory...
'
Dim FileName$ = Dir$(SourceDirectory$ & "\*.svg")
While (FileName$ <> "")
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Add the SVG file name to the listbox.
'
lb1.AddItem FileName$ & FileSize(FileName$)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Get the filename only
'
Dim fn$ = Left$(FileName$, RInStr(FileName$, ".") - 1)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Convert the SVG file by name to the PNG format.
'
P_Convert_SVG2PNG_File(fn$)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Get the next SVG file in the directory.
'
FileName$ = Dir$
Wend
sb1.SimpleText = sb1.SimpleText & " Finish:" & DateTime$
EndProc
Proc P_Convert_SVG2PNG_File(InputFileName$)
Try
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Use InkScape to convert an SVG file to a PNG file.
'
' Set the full paths of the input SVG, output PNG and Inkscape executable.
'
Dim InputFilePath$ = App.Path & "\" & InputFileName$ & ".svg"
Dim OutputFileName$ = InputFileName$ & ".png"
'Dim OutputFilePath$ = TempDir & OutputFileName$
Dim OutputFilePath$ = App.Path & "\" & OutputFileName$
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Delete any previously existing file of the same output name...
'
If Exist(OutputFilePath$)
Kill OutputFilePath$
EndIf
Dim InkScapePath$ = ShortPathName("C:\Program Files (x86)\inkscape\bin\inkscape.exe")
Trace InputFilePath$
Trace OutputFilePath$
Trace InkScapePath$
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Set the command string...
'
Dim command$ = InkScapePath$ : Trace command$
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Set the parameter string...
'
Dim cmdLine$ = _
" -o " & #34 & OutputFilePath$ & #34 & _
" --export-type=png " & _
" -d 10 " & #34 & InputFilePath$ & #34
Trace cmdLine$
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Run the conversions using Exec command...
'
Dim t0#, dt#
t0 = Timer
~Exec(command$, cmdLine$)
dt = (Timer - t0)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Wait for disk file system to update...
'
Do : Sleep 1 : DoEvents : Until Exist(OutputFilePath$)
Do : Sleep 1 : DoEvents : Until FileLen(OutputFilePath$)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Report the output file name and conversion time...
'
lb2.AddItem OutputFileName$ & FileSize(OutputFilePath$)
lb3.AddItem Format(dt, " 0.##0") & "s"
Catch
Debug "Error: "; Err$
EndCatch
EndProc
Function FileSize(fn$) As String
Try
If !Exist(fn$) Return " File does not exist."
Dim fs% = FileLen%(fn$)
'Trace fs
If (fs > (1024 ^ 2))
Return" (" & Format((fs / 2 ^ 20), "###.##0") & "MB)"
ElseIf (fs > 1024)
Return" (" & Format((fs / 2 ^ 10), "###.##0") & "KB)"
Else
Return" (" & Format(fs, "###0") & "B)"
EndIf
Catch
Return "Error: " & Err$
EndCatch
EndFunc