« Finder再起動(QuickLookも) | トップページ | [XML] RSS »

[Python]SVGをPNGに変換

ダウンロード - svg2png.zip


#!/usr/bin/env python3
########################
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF, renderPM
import sys
import os

args = sys.argv

strFilePath = args[1]
strSavePath = args[2]

print("strFilePath:" + strFilePath)
print("strSavePath:" + strSavePath)

drawing = svg2rlg(strFilePath)
renderPDF.drawToFile(drawing, strSavePath)



【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "PDFKit"
use framework "Quartz"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

##設定項目
set strPyFileName to "svg2pdf.py"

###パス取得
set aliasPathToMe to path to me as alias
tell application "Finder"
  set aliasPathToMeContainerDir to container of aliasPathToMe as alias
end tell
###パスを生成
set strPathToMeContainerDir to POSIX path of aliasPathToMeContainerDir as text
set strBinPath to strPathToMeContainerDir & "bin/" as text
set strPyPath to strBinPath & strPyFileName as text
####ダイアログ用デスクトップパス
set aliasDefaultLocation to path to desktop folder from user domain as alias
####ダイアログ
set listAliasFilePath to (choose file with prompt "ファイルを選んでください" default location aliasDefaultLocation of type {"public.svg-image"} with multiple selections allowed without showing package contents and invisibles) as list

################################
###カラープロファイルを読み出しておく ファイルはお好みで
set strIccFilePath to "/System/Library/ColorSync/Profiles/sRGB Profile.icc"
set ocidIccFilePathStr to refMe's NSString's stringWithString:(strIccFilePath)
set ocidIccFilePath to ocidIccFilePathStr's stringByStandardizingPath
set ocidIccFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidIccFilePath) isDirectory:(false)
set ocidProfileData to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidIccFilePathURL)

###ディレクトリ
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidTempDirURL to appFileManager's temporaryDirectory()
set ocidUUID to refMe's NSUUID's alloc()'s init()
set ocidUUIDString to ocidUUID's UUIDString
set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true
#
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
# 777-->511 755-->493 700-->448 766-->502
ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)

####ファイルの数だけ繰り返し
repeat with itemAliasFilePath in listAliasFilePath
  ###UNIXパスに変換
  set strFilePath to (POSIX path of itemAliasFilePath) as text
  set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
  set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
  set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
  ##
  set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
  set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension()
  set strBaseFileName to ocidBaseFilePathURL's lastPathComponent() as text
  
  ###PDFパス
  set strPDFFileName to (strBaseFileName & ".pdf") as text
  set ocidPDFFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strPDFFileName) isDirectory:false)
  set strPDFSaveFilePath to (ocidPDFFilePathURL's |path|()) as text
  
  ###スクリプトに渡す
  set strCommandText to "\"" & strPyPath & "\" \"" & strFilePath & "\" \"" & strPDFSaveFilePath & "\"" as text
do shell script strCommandText
  
  
  ###PNGパス
  set strSaveFileName to (strBaseFileName & ".png") as text
  set ocidSaveFilePathURL to (ocidContainerDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:false)
  ###NSDATAに読み込む
  set ocidReadPdfData to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidPDFFilePathURL))
  ####NSPDFImageRepに変換
  set ocidPdfPep to (refMe's NSPDFImageRep's alloc()'s initWithData:(ocidReadPdfData))
  ##1ページ目を読み込み
(ocidPdfPep's setCurrentPage:0)
  ####ページデータをNSIMAGEに格納
  set ocidPageImageData to refMe's NSImage's alloc()'s init()
(ocidPageImageData's addRepresentation:ocidPdfPep)
  ###中間ファイルとしてのTIFFに変換
  set ocidOsDispatchData to ocidPageImageData's TIFFRepresentation()
  ####出力されるBmpImageRepに格納
  set ocidBmpImageRep to (refMe's NSBitmapImageRep's imageRepWithData:ocidOsDispatchData)
  ##PNG書出オプション
  set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidPropertyDict's setObject:true forKey:(refMe's NSImageInterlaced))
  ####ガンマ値が不要な場合はコメントアウト
(ocidPropertyDict's setObject:0.45455 forKey:(refMe's NSImageGamma))
(ocidPropertyDict's setObject:ocidProfileData forKey:(refMe's NSImageColorSyncProfileData))
  set ocidFileType to refMe's NSBitmapImageFileTypePNG
  ##変換
  set ocidNSInlineData to (ocidBmpImageRep's representationUsingType:(ocidFileType) |properties|:(ocidPropertyDict))
  #####保存
  set boolDone to (ocidNSInlineData's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference))
  
end repeat



|

« Finder再起動(QuickLookも) | トップページ | [XML] RSS »

XML SVG」カテゴリの記事