twitter投稿用画像3分割(仮)
#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
(*
1つの画像を16:9の4分割します
サイズによって切り取られる部分があります
com.cocolog-nifty.quicktimer.icefloe
*)
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
#############################
###ダイアログを前面に出す
set strName to (name of current application) as text
if strName is "osascript" then
tell application "Finder" to activate
# tell application id "com.apple.appkit.xpc.openAndSavePanelService" to activate
else
tell current application to activate
end if
set listUTI to {"public.image"}
set strMes to ("ファイルを選んでください") as text
set strPrompt to ("ファイルを選んでください") as text
try
### ファイル選択時
set aliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias
on error
log "エラーしました"
return "エラーしました"
end try
set strFilePath to (POSIX path of aliasFilePath) as text
set ocidFilePath to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath)
#
set ocidFileName to ocidFilePathURL's lastPathComponent()
set strBaseFileName to (ocidFileName's stringByDeletingPathExtension()) as text
#
set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
set aliasSaveDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias
############
set strMes to "保存先フォルダを選んでください" as text
set strPrompt to "保存先フォルダを選択してください" as text
try
set aliasSaveDirPath to (choose folder strMes with prompt strPrompt default location aliasSaveDirPath with invisibles and showing package contents without multiple selections allowed) as alias
on error
log "エラーしました"
return "エラーしました"
end try
set strSaveDirPath to (POSIX path of aliasSaveDirPath) as text
set ocidSaveDirPath to refMe's NSString's stringWithString:(strSaveDirPath)
set ocidSaveDirPathURL to refMe's NSURL's fileURLWithPath:(ocidSaveDirPath)
############
set ocidOption to (refMe's NSDataReadingMappedIfSafe)
set listData to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error|:(reference)
if (item 2 of listData) ≠ (missing value) then
log (item 2 of listData)'s localizedFailureReason() as text
return "データ読み込みでエラーになりました"
else
set ocidData to (item 1 of listData)
end if
############
#イメージに変換
set ocidImage to refMe's NSImage's alloc()'s initWithData:(ocidData)
#BmpRepに変換
set ocidImageRepArray to ocidImage's representations()
set ocidImageRep to ocidImageRepArray's firstObject()
#ptサイズ
set ocidSize to ocidImageRep's |size|()
set numWpt to ocidSize's width
set numHpt to ocidSize's height
#pxサイズ
set numWpx to ocidImageRep's pixelsWide()
set numHpx to ocidImageRep's pixelsHigh()
#解像度
set numResolution to (numWpx / numWpt) as number
#BmpRepを72ppiにしておく
set ocidSetSize to refMe's NSMakeSize(numWpx, numHpx)
ocidImageRep's setSize:(ocidSetSize)
############
#画像1セルサイズを計算
if numWpx ≥ numHpx then
log "横長か正方形画像"
set numAspectH to (numHpx / 18) as number
set numAspectW to (numAspectH * 16) as number
if (numAspectW * 2) ≥ numWpx then
log "上下に余白でCROP"
set numAspectW to (numWpx / 32) as number
set numSelW to (numAspectW * 16) as number
set numSelH to (numAspectW * 9) as number
set numMarginTop to ((numHpx - (numAspectW * 18)) / 2) as number
set numMarginLeft to 0 as integer
log numSelW as integer
log numSelH as integer
log numMarginTop as integer
set ocidMakeSize to refMe's NSMakeSize(numSelW, numSelH)
else
log "左右に余白でCROP"
set numSelW to (numAspectH * 16) as number
set numSelH to (numAspectH * 9) as number
set numMarginLeft to ((numWpx - (numAspectH * 32)) / 2) as number
set numMarginTop to 0 as integer
set ocidMakeSize to refMe's NSMakeSize(numSelW, numSelH)
end if
else if numWpx < numHpx then
log "縦長"
set numAspectW to (numWpx / 32) as number
set numSelW to (numAspectW * 16) as number
set numSelH to (numAspectW * 9) as number
set numMarginTop to ((numHpx - (numAspectW * 18)) / 2) as number
set numMarginLeft to 0 as integer
log numSelW as integer
log numSelH as integer
log numMarginTop as integer
set ocidMakeSize to refMe's NSMakeSize(numSelW, numSelH)
end if
repeat with itemIntNo from 1 to 3 by 1
################左下
##出力用REP
if itemIntNo is 1 then
set ocidAardboardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numSelW) pixelsHigh:(numSelH * 2) bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(refMe's NSCalibratedRGBColorSpace) bitmapFormat:(refMe's NSBitmapFormatAlphaFirst) bytesPerRow:0 bitsPerPixel:32)
else
set ocidAardboardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numSelW) pixelsHigh:(numSelH) bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(refMe's NSCalibratedRGBColorSpace) bitmapFormat:(refMe's NSBitmapFormatAlphaFirst) bytesPerRow:0 bitsPerPixel:32)
end if
##処理開始
set ocidGraphicsContext to refMe's NSGraphicsContext
ocidGraphicsContext's saveGraphicsState()
(ocidGraphicsContext's setCurrentContext:(ocidGraphicsContext's graphicsContextWithBitmapImageRep:(ocidAardboardRep)))
##仕上がり寸法RECT(ペースト位置)
if itemIntNo is 1 then
set ocidPasteRect to {origin:{x:0, y:0}, |size|:{width:(numSelW), height:(numSelH * 2)}}
else
set ocidPasteRect to {origin:{x:0, y:0}, |size|:{width:(numSelW), height:(numSelH)}}
end if
##元データのRECT(コピー位置)
#セル毎のコピー位置
if itemIntNo is 1 then
set ocidCopyRect to {origin:{x:(numMarginLeft), y:(numMarginTop)}, |size|:{width:(numSelW), height:(numSelH * 2)}}
else if itemIntNo is 2 then
set ocidCopyRect to {origin:{x:(numMarginLeft + numSelW), y:(numMarginTop + numSelH)}, |size|:{width:(numSelW), height:(numSelH)}}
else if itemIntNo is 3 then
set ocidCopyRect to {origin:{x:(numMarginLeft + numSelW), y:numMarginTop}, |size|:{width:(numSelW), height:(numSelH)}}
end if
##合成
(ocidImageRep's drawInRect:(ocidPasteRect) fromRect:(ocidCopyRect) operation:(refMe's NSCompositingOperationSourceOver) fraction:1.0 respectFlipped:false hints:(missing value))
##処理終了
ocidGraphicsContext's restoreGraphicsState()
###変換オプション
set ocidPropertiesDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidPropertiesDict's setValue:(false) forKey:(refMe's NSImageInterlaced))
(ocidPropertiesDict's setValue:(2.2) forKey:(refMe's NSImageGamma))
#PNG変換でデータにする
set ocidSaveData to (ocidAardboardRep's representationUsingType:(refMe's NSPNGFileType) |properties|:(ocidPropertiesDict))
##########################
##保存
#ファイル名
if itemIntNo is 1 then
set strSaveFileName to (strBaseFileName & "_1_L") as text
else if itemIntNo is 2 then
set strSaveFileName to (strBaseFileName & "_2_RT") as text
else if itemIntNo is 3 then
set strSaveFileName to (strBaseFileName & "_3_RB") as text
end if
#保存パス
set ocidBaseSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false))
set ocidSaveFilePathURL to (ocidBaseSaveFilePathURL's URLByAppendingPathExtension:("png"))
#保存
set listDone to (ocidSaveData's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference))
if (item 2 of listDone) ≠ (missing value) then
log (item 2 of listDone)'s localizedFailureReason() as text
return "データ読み込みでエラーになりました"
end if
end repeat
###保存先を開く
set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
set ocidConfig to refMe's NSWorkspaceOpenConfiguration's configuration
(ocidConfig's setActivates:(refMe's NSNumber's numberWithBool:true))
ocidConfig's setHides:(refMe's NSNumber's numberWithBool:false)
##
(appSharedWorkspace's openURL:(ocidSaveDirPathURL) configuration:(ocidConfig) completionHandler:(missing value))
| 固定リンク
「Twitter」カテゴリの記事
- twitter用画像生成 9種類の画像の合成(2024.08.06)
- twitter投稿用画像3分割(仮)(2024.04.11)
- twitter投稿用画像4分割(2024.04.07)
- [com.microsoft.edgemac]前面のページのURLをtwitter.comに引用投稿(2024.02.28)
- twitter投稿用(2023.05.01)