« [Resolution]解像度を求める | トップページ | [ttml] srt をttmlに変換 »

[NSBitmapImageRep]画像のサイズ変更(CROP)

元画像
左用と右用のファイルに複製
順番に開いて変更後のCROPサイズでNSBitmapImageRepを作成
元画像をNSBitmapImageRepに場所指定してNSCompositeSourceOverする
(小さいサイズに大きいサイズを座標で上から置くのではみ出た部分は削除される)
-->この方法が正しいか?は疑問だがQRコード作った時の使い回し
出来上がったCROP画像データをファイルに『上書き』で『書き出す』



#!/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 scripting additions
use framework "UniformTypeIdentifiers"

property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSURL : a reference to objMe's NSURL

property objNSArray : a reference to objMe's NSArray
property objNSMutableArray : a reference to objMe's NSMutableArray
property objUTType : a reference to objMe's UTType

property objNSDictionary : a reference to NSDictionary

property objNSImage : a reference to objMe's NSImage
property objNSBitmapImageRep : a reference to objMe's NSBitmapImageRep
property objNSGraphicsContext : a reference to objMe's NSGraphicsContext

set objFileManager to objMe's NSFileManager's defaultManager()
set ocidNSURLIsRegularFileKey to objMe's NSURLIsRegularFileKey

############################################
###設定項目##左右ページ 右から左ならtrue 左から右ならfalse
############################################
set boolRtoL to true as boolean

############################################
######ログ表示
doLogView()

tell application "Finder"
##set aliasDefaultLocation to container of (path to me) as alias
set aliasDefaultLocation to (path to desktop folder from user domain) as alias
end tell

try
set aliasResponse to (choose folder "画像が入っているフォルダを選んでください" with prompt "フォルダを選択してください" default location aliasDefaultLocation without multiple selections allowed, invisibles and showing package contents)
on error
log "エラーしました"
return
end try


###パス
set strDirPath to POSIX path of aliasResponse as text
###NSStringテキスト
set ocidDirPath to (objNSString's stringWithString:strDirPath)
#####NSURL NSStringURL
set ocidDirUrlPath to (objNSURL's fileURLWithPath:ocidDirPath)
###不可視ファイル以外のファイルを取得
set ocidContentsArray to objFileManager's contentsOfDirectoryAtURL:ocidDirUrlPath includingPropertiesForKeys:{ocidNSURLIsRegularFileKey} options:(objMe's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
####可変リストに変更
set ocidSortedArrayM to objNSMutableArray's arrayWithArray:ocidContentsArray
#####UNIXパス形式
set ocidSortedArrayM to ocidSortedArrayM's valueForKeyPath:"path"
###ローカライズ順でソート
set ocidSortedArrayM to (ocidSortedArrayM's sortedArrayUsingSelector:"localizedCompare:")
###出来上がりファイル番号のカウント
set numFileCntNo to 0 as number
###ファイルの数だけ繰り返し
repeat with objSortedArrayM in ocidSortedArrayM
####拡張子名
set strExtension to objSortedArrayM's pathExtension as text
log
if strExtension is "" then
log "対象ファイル外なのでそのままにする"
else
####拡張子からUTI取得
set ocidUTType to (objUTType's typeWithFilenameExtension:strExtension)
set strUTI to ocidUTType's identifier as text
####UTIからMIME取得
set ocidFileMimeType to ocidUTType's preferredMIMEType()
set strFileMimeType to ocidFileMimeType as text
####ファイル名
set ocidFileName to objSortedArrayM's lastPathComponent
set strFileName to ocidFileName as text
####拡張子を除いたファイル名
set ocidPrefixName to ocidFileName's stringByDeletingPathExtension
#####ファイル名に加える『接頭名』を指定
####ファイル名用番号(2桁ゼロサプレス)
if boolRtoL is true then
set numFileCntNo to (numFileCntNo + 1) as number
set strSplitFileNo to "000" & numFileCntNo as text
set strSplitFileNo to text -3 through -1 of strSplitFileNo as text
set strRpageNameHeader to (strSplitFileNo & "_R_") as text
set strRpageName to (strRpageNameHeader & strFileName) as text
set strRsaveFilePath to (strDirPath & strRpageName) as text
set ocidDistFilePathR to (objNSURL's fileURLWithPath:strRsaveFilePath)

set numFileCntNo to (numFileCntNo + 1) as number
set strSplitFileNo to "000" & numFileCntNo as text
set strSplitFileNo to text -3 through -1 of strSplitFileNo as text
set strLpageNameHeader to (strSplitFileNo & "_L_") as text
set strLpageName to (strLpageNameHeader & strFileName) as text
set strLsaveFilePath to (strDirPath & strLpageName) as text
set ocidDistFilePathL to (objNSURL's fileURLWithPath:strLsaveFilePath)
else
set numFileCntNo to (numFileCntNo + 1) as number
set strSplitFileNo to "000" & numFileCntNo as text
set strSplitFileNo to text -3 through -1 of strSplitFileNo as text
set strLpageNameHeader to (strSplitFileNo & "_L_") as text
set strLpageName to (strLpageNameHeader & strFileName) as text
set strLsaveFilePath to (strDirPath & strLpageName) as text
set ocidDistFilePathL to (objNSURL's fileURLWithPath:strLsaveFilePath)


set numFileCntNo to (numFileCntNo + 1) as number
set strSplitFileNo to "000" & numFileCntNo as text
set strSplitFileNo to text -3 through -1 of strSplitFileNo as text
set strRpageNameHeader to (strSplitFileNo & "_R_") as text
set strRpageName to (strRpageNameHeader & strFileName) as text
set strRsaveFilePath to (strDirPath & strRpageName) as text
set ocidDistFilePathR to (objNSURL's fileURLWithPath:strRsaveFilePath)

end if

####MIMEの照合
if strFileMimeType is "image/jpeg" then
log "リストに項目がある"
set boolFileCopyDoneR to (objFileManager's copyItemAtPath:objSortedArrayM toPath:ocidDistFilePathR |error|:(boolean))
set boolFileCopyDoneL to (objFileManager's copyItemAtPath:objSortedArrayM toPath:ocidDistFilePathL |error|:(boolean))

if boolFileCopyDoneR is true then
log "Rファイルはコピーされました"
if boolFileCopyDoneL is true then
log "Lファイルはコピーされました"
#####NSURL NSStringURL
set ocidOrignalFileUrlPath to (objNSURL's fileURLWithPath:objSortedArrayM)
log "対象ファイル外なのでゴミ箱へ"
set boolGoToTrash to (objFileManager's trashItemAtURL:ocidOrignalFileUrlPath resultingItemURL:(missing value) |error|:(""))
if boolGoToTrash is true then
log "オリジナルファイルはゴミ箱へ入れました"
else
log "ゴミ箱へ入れるのを失敗しました"
end if
else if boolFileCopyDoneL is false then
log "Lファイルコピーは失敗しました"
end if
else if boolFileCopyDoneR is false then
log "Rファイルコピーは失敗しました"
end if

else if strFileMimeType is "image/png" then
log "リストに項目がある"
set boolFileCopyDoneR to (objFileManager's copyItemAtPath:objSortedArrayM toPath:ocidDistFilePathR |error|:(boolean))
set boolFileCopyDoneL to (objFileManager's copyItemAtPath:objSortedArrayM toPath:ocidDistFilePathL |error|:(boolean))
###左と右の分岐
if boolFileCopyDoneR is true then
log "Rファイルはコピーされました"
if boolFileCopyDoneL is true then
log "Lファイルはコピーされました"
#####NSURL NSStringURL
set ocidOrignalFileUrlPath to (objNSURL's fileURLWithPath:objSortedArrayM)
log "対象ファイル外なのでゴミ箱へ"
set boolGoToTrash to (objFileManager's trashItemAtURL:ocidOrignalFileUrlPath resultingItemURL:(missing value) |error|:(""))
if boolGoToTrash is true then
log "オリジナルファイルはゴミ箱へ入れました"
else
log "ゴミ箱へ入れるのを失敗しました"
end if
else if boolFileCopyDoneL is false then
log "Lファイルコピーは失敗しました"
end if
else if boolFileCopyDoneR is false then
log "Rファイルコピーは失敗しました"
end if

else
set numFileCntNo to (numFileCntNo - 2) as number
#####NSURL NSStringURL
set ocidOrignalFileUrlPath to (objNSURL's fileURLWithPath:objSortedArrayM)
log "対象ファイル外なのでゴミ箱へ"
set boolGoToTrash to (objFileManager's trashItemAtURL:ocidOrignalFileUrlPath resultingItemURL:(missing value) |error|:(""))
if boolGoToTrash is true then
log "オリジナルファイルはゴミ箱へ入れました"
else
log "ゴミ箱へ入れるのを失敗しました"
end if
end if

end if
end repeat

#############################################
(*
ここで、ファイルの複製(左用と右用)が出来たので
再度ファイルリストを取り直す
*)

###不可視ファイル以外のファイルを取得
set ocidContentsArrayNEW to objFileManager's contentsOfDirectoryAtURL:ocidDirUrlPath includingPropertiesForKeys:{ocidNSURLIsRegularFileKey} options:(objMe's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
####可変リストに変更
set ocidContentsArrayNewM to objNSMutableArray's arrayWithArray:ocidContentsArrayNEW
#####UNIXパス形式
set ocidContentsArrayNewM to ocidContentsArrayNewM's valueForKeyPath:"path"
###ローカライズ順でソート
set ocidSortedArrayNewM to (ocidContentsArrayNewM's sortedArrayUsingSelector:"localizedCompare:")

###ファイルの数だけ繰り返し
repeat with objSortedArrayNewM in ocidSortedArrayNewM
###パスで来るのでテキストに
set strSortedArrayNewMObj to objSortedArrayNewM as text
###パスをエイリアスに
set aliasContentsFilePath to POSIX file strSortedArrayNewMObj as alias
###########################
tell application "System Events"
set objKind to kind of aliasContentsFilePath
if objKind is "PNGイメージ" then
set boolKindChk to true as boolean
####PNG用の圧縮プロパティ
else if objKind is "JPEGイメージ" then
set boolKindChk to true as boolean
####JPEG用の圧縮プロパティ
else
set boolKindChk to false as boolean
end if
end tell

####NSStリングに
set ocidSortedFilePath to (objNSString's stringWithString:strSortedArrayNewMObj)
####ファイル名
set ocidFileName to ocidSortedFilePath's lastPathComponent
set strFileName to ocidFileName as text
####拡張子を除いたパス
set ocidSortedFileNamePath to ocidSortedFilePath's stringByDeletingPathExtension()
###拡張子
set strFileExtension to ocidSortedFilePath's pathExtension() as text
###保存パスここでは上書きなので同じにする
set ocidSaveFilePath to ocidSortedFilePath
####ファイルを読み込み NSBitmapImageRep
set ocidBitmapImageRepObj to (objNSBitmapImageRep's imageRepWithContentsOfFile:objSortedArrayNewM)
set numImageRepHight to ocidBitmapImageRepObj's pixelsHigh() as integer
set numImageRepWidth to ocidBitmapImageRepObj's pixelsWide() as integer
set numImageRepBytePerPlane to (numImageRepHight * numImageRepWidth) as integer
log numImageRepHight
log numImageRepWidth
log numImageRepBytePerPlane
####ページの半分の幅
set numImageRepWidthHarf to numImageRepWidth / 2 as integer
log numImageRepWidthHarf
#######ファイルを読み込み NSImage
set ocidNSImageFileObj to (objNSImage's alloc()'s initWithContentsOfFile:objSortedArrayNewM)
set recordSize to (ocidNSImageFileObj's |size|()) as record
set numNSImageHeight to height of recordSize as integer
set numNSImageWidth to width of recordSize as integer
set numNSImageBytePerPlane to (numNSImageHeight * numNSImageWidth) as integer
log numNSImageHeight
log numNSImageWidth
log numNSImageBytePerPlane
####ページの半分の幅
set numNSImageWidthHarf to numNSImageWidth / 2 as integer
log numNSImageWidthHarf
#### 解像度を求める
set numResolution to (((numImageRepBytePerPlane / numNSImageBytePerPlane) ^ 0.5) * 72) as integer
####NSImageのサイズで『半分の』サイズのNSBitmapImageRepを作成する
set ocidNewImageRep to (objNSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:numNSImageWidthHarf pixelsHigh:numNSImageHeight bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(objMe's NSCalibratedRGBColorSpace) bitmapFormat:(objMe's NSAlphaFirstBitmapFormat) bytesPerRow:0 bitsPerPixel:32)
####処理開始
objNSGraphicsContext's saveGraphicsState()
#####半分サイズのイメージをgraphicsContextに変換
(objNSGraphicsContext's setCurrentContext:(objNSGraphicsContext's graphicsContextWithBitmapImageRep:ocidNewImageRep))
objMe's NSColor's grayColor()'s |set|()
#### 座標と幅高さを指定
objMe's NSRectFill({origin:{x:0, y:0}, |size|:{width:numNSImageWidthHarf, height:numNSImageHeight}})

if boolRtoL is true then
#####右から左へのパターン
if strFileName contains "_R_" then
###新規画像の座標00に開いた画像の座標00を描画する
(ocidBitmapImageRepObj's drawInRect:{origin:{x:0, y:0}, |size|:{width:numNSImageWidthHarf, height:numNSImageHeight}} fromRect:{origin:{x:numNSImageWidthHarf, y:0}, |size|:{width:numNSImageWidthHarf, height:numNSImageHeight}} operation:(objMe's NSCompositeSourceOver) fraction:1.0 respectFlipped:true hints:(missing value))
else if strFileName contains "_L_" then
#####新規画像の座標00に開いた画像の座標幅を半分にした部分でを描画する
(ocidBitmapImageRepObj's drawInRect:{origin:{x:0, y:0}, |size|:{width:numImageRepWidthHarf, height:numImageRepHight}} fromRect:{origin:{x:0, y:0}, |size|:{width:numImageRepWidthHarf, height:numImageRepHight}} operation:(objMe's NSCompositeSourceOver) fraction:1.0 respectFlipped:true hints:(missing value))
else
return false
end if
else if boolRtoL is false then
#####右から左へのパターン
if strFileName contains "_L_" then
###新規画像の座標00に開いた画像の座標00を描画する
(ocidBitmapImageRepObj's drawInRect:{origin:{x:0, y:0}, |size|:{width:numImageRepWidthHarf, height:numImageRepHight}} fromRect:{origin:{x:0, y:0}, |size|:{width:numImageRepWidthHarf, height:numImageRepHight}} operation:(objMe's NSCompositeSourceOver) fraction:1.0 respectFlipped:true hints:(missing value))
else if strFileName contains "_R_" then
#####新規画像の座標00に開いた画像の座標幅を半分にした部分でを描画する
(ocidBitmapImageRepObj's drawInRect:{origin:{x:0, y:0}, |size|:{width:numNSImageWidthHarf, height:numNSImageHeight}} fromRect:{origin:{x:numNSImageWidthHarf, y:0}, |size|:{width:numNSImageWidthHarf, height:numNSImageHeight}} operation:(objMe's NSCompositeSourceOver) fraction:1.0 respectFlipped:true hints:(missing value))
else
return false
end if
end if
####処理終了
objNSGraphicsContext's restoreGraphicsState()

###########################
tell application "System Events"
set objKind to kind of aliasContentsFilePath
if objKind is "PNGイメージ" then
set boolKindChk to true as boolean
####PNG用の圧縮プロパティで出力用フォーマットに変換
set ocidInlineImageData to (ocidNewImageRep's representationUsingType:(objMe's NSPNGFileType) |properties|:{NSImageInterlaced:true})
else if objKind is "JPEGイメージ" then
set boolKindChk to true as boolean
####JPEG用の圧縮プロパティで出力用フォーマットに変換
set ocidInlineImageData to (ocidNewImageRep's representationUsingType:(objMe's NSJPEGFileType) |properties|:{NSImageCompressionFactor:0.9, NSImageProgressive:false})
else
set boolKindChk to false as boolean
return false
end if
end tell
(ocidInlineImageData's writeToFile:ocidSaveFilePath atomically:true)

set ocidInlineImageData to ""
set ocidBitmapImageRepObj to ""
end repeat



#########################ログ表示
to doLogView()

tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat

end doLogView
#########################

|

« [Resolution]解像度を求める | トップページ | [ttml] srt をttmlに変換 »

AppleScript Image Events」カテゴリの記事

AppleScript NSImage」カテゴリの記事

AppleScript NSBitmapImageRep」カテゴリの記事