[NSBitmapImageRep]画像の解像度変更(resolution)
pngファイル144ppi -->スクリーンキャプチャーの解像度
を
pngファイル72ppiに変更します
解像度の変更のみですので、画像のpixelサイズは元画像のままです
#!/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
######ログ表示
doLogView()
property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSMutableString : a reference to refMe's NSMutableString
property refNSURL : a reference to refMe's NSURL
property refNSImage : a reference to refMe's NSImage
####ダイアログで使うデフォルトロケーション
tell application "Finder"
set aliasDefaultLocation to (path to desktop folder from user domain) as alias
end tell
####UTIリスト PDFのみ
set listUTI to {"public.png"}
####ダイアログを出す
set aliasFilePath to (choose file with prompt "PNGイメージファイルを選んでください" default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias
set strFilePath to POSIX path of aliasFilePath
##Make NSString
set ocidNSString to refNSString's stringWithString:strFilePath
log ocidNSString as text
log ocidNSString's className() as text
####NSimageに格納
set ocidImageData to refNSImage's alloc()'s initWithContentsOfFile:strFilePath
log ocidImageData
log className() of ocidImageData as text
###サイズ取得PT
set ocidImageSize to ocidImageData's |size|()
log ocidImageSize
log class of ocidImageSize as text
log "#####"
###representations取得
set ocidImageRepArray to ocidImageData's representations()
log ocidImageRepArray
log className() of ocidImageRepArray as text
###NSBitmapImageRep取得
set ocidImageRep to (ocidImageRepArray's objectAtIndex:0)
log ocidImageRep
log className() of ocidImageRep as text
####ピクセル単位での幅
set numpixelsWidth to ocidImageRep's pixelsWide()
log numpixelsWidth
log class of numpixelsWidth as text
####ピクセル単位での高さ
set numpixelsHeight to ocidImageRep's pixelsHigh()
log numpixelsHeight
log class of numpixelsHeight as text
####ポイント単位でのサイズ
set ocidImageRepSize to ocidImageRep's |size|()
log ocidImageRepSize
log class of ocidImageRepSize as text
####ポイント単位での幅と高さ
set numPointWidth to width of ocidImageRepSize
set numPointHeight to height of ocidImageRepSize
log numPointWidth
log numPointHeight
####イメージの解像度
set numCurrentDPI to ((72 * numpixelsWidth) / numPointWidth) as integer
log numCurrentDPI
####変更後のポイントサイズを求める
set numNewImageWidth to ((72.0 * numpixelsWidth) / 72)
set numNewImageHeight to ((72.0 * numpixelsHeight) / 72)
set recordNewImageSize to {width:numNewImageWidth, height:numNewImageHeight} as record
log recordNewImageSize
####ImageRepをポイントサイズに変更
ocidImageRep's setSize:recordNewImageSize
###確認用
set ocidImageRepSize to ocidImageRep's |size|()
log ocidImageRepSize
log class of ocidImageRepSize as text
###PNGで保存する
set ocidSaveImageType to refMe's NSBitmapImageFileTypePNG
###インターレースとアルファは保持
set ocidNewImageData to ocidImageRep's representationUsingType:ocidSaveImageType |properties|:{NSImageInterlaced:true, NSImageDitherTransparency:true}
###上書き保存
ocidNewImageData's writeToFile:strFilePath atomically:false
set ocidImageRep to ""
set ocidNewImageData to ""
#########################ログ表示
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
tell application "Script Editor"
tell application "System Events"
tell process "Script Editor"
tell window 1
tell splitter group 1
tell splitter group 1
tell group 1
tell checkbox "返された値"
set boolValue to value as boolean
end tell
if boolValue is false then
click checkbox "返された値"
end if
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end if
end repeat
end doLogView
#########################
| 固定リンク
「NSBitmapImageRep」カテゴリの記事
- 画像のCROP w指定 (少し修正)(2024.05.07)
- 解像度変更 修正(2024.04.07)
- [2in1]画像合成2in1 2up 右→左順(2024.03.21)
- Gifを全コマ別PNGに書き出し (フレーム数0の場合を考慮した修正)(2024.02.01)
- [NSBitmapImageRep] 処理一覧(2024.02.29)