« [bash]lsregisterに不具合が発生した場合のリセット | トップページ | [Acrobat]用紙サイズ(media box)を変更する(仮) »

[NSImageRep] 画像の解像度(ピクセル密度)を取得する


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#com.cocolog-nifty.quicktimer.icefloe
004#
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use framework "Foundation"
008use framework "AppKit"
009use scripting additions
010
011property refMe : a reference to current application
012
013
014set strFilePath to "/Library/User Pictures/Animals/Eagle.heic" as text
015set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
016set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
017set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
018
019# doGetImageSize(ファイルパスのURL)
020# {true, {{width:幅mm height:mm}, {width:幅px, height:縦px},解像度}
021set listResponse to doGetImageSize(ocidFilePathURL)
022if (item 1 of listResponse) = (true) then
023  log "正常処理 : "
024  set listImageSize to (item 2 of listResponse)
025  set numWmm to (width of (item 1 of listImageSize)) as real
026  set numHmm to (height of (item 1 of listImageSize)) as real
027  set numWpx to (width of (item 2 of listImageSize)) as real
028  set numHpx to (height of (item 2 of listImageSize)) as real
029  set numResolution to (item 3 of listImageSize) as real
030  log listResponse
031else if (item 1 of listResponse) = (false) then
032  log (item 2 of listResponse) as text
033  return "エラーしました"
034end if
035
036##############################
037# 画像のサイズと解像度
038#戻り値は
039# {true, {{width:幅mm height:mm}, {width:幅px, height:縦px},解像度}
040##############################
041to doGetImageSize(argFilePathURL)
042  # NSData
043  set ocidOption to (current application's NSDataReadingMappedIfSafe)
044  set listResponse to current application's NSData's dataWithContentsOfURL:(argFilePathURL) options:(ocidOption) |error| :(reference)
045  if (item 2 of listResponse) is (missing value) then
046    set ocidReadData to (item 1 of listResponse)
047    log "dataWithContentsOfURL 正常終了"
048  else
049    log (item 2 of listResponse)'s code() as text
050    set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text
051    set listReturnValue to {false, strErrorMes} as list
052    return listReturnValue
053  end if
054  # NSImage
055  set ocidImageData to current application's NSImage's alloc()'s initWithData:(ocidReadData)
056  if ocidImageData = (missing value) then
057    set listReturnValue to {false, "NSImageの内容が空です"} as list
058    return listReturnValue
059  end if
060  set ocidImageSizePt to ocidImageData's |size|()
061  #ポイントサイズ取得
062  set numWpt to ocidImageSizePt's width()
063  set numHpt to ocidImageSizePt's height()
064  #DecimalNumberに
065  set ocidDecWpt to current application's NSDecimalNumber's alloc()'s initWithString:(numWpt as text)
066  set ocidDecHpt to current application's NSDecimalNumber's alloc()'s initWithString:(numHpt as text)
067  #計算に必要な値
068  set ocidDecPt to current application's NSDecimalNumber's alloc()'s initWithString:("72")
069  set ocidDecIn to current application's NSDecimalNumber's alloc()'s initWithString:("25.4")
070  #幅pt÷72
071  set ocidFloatWpt to (ocidDecWpt's decimalNumberByDividingBy:(ocidDecPt))
072  #縦pt÷72
073  set ocidFloatHpt to (ocidDecHpt's decimalNumberByDividingBy:(ocidDecPt))
074  #(幅pt÷72)x25.4=幅MM
075  set ocidMMWpt to (ocidFloatWpt's decimalNumberByMultiplyingBy:(ocidDecIn))
076  #(縦pt÷72)x25.4=縦MM
077  set ocidMMHpt to (ocidFloatHpt's decimalNumberByMultiplyingBy:(ocidDecIn))
078  #小数点以下2位で四捨五入
079  set appFormatter to current application's NSNumberFormatter's alloc()'s init()
080  appFormatter's setRoundingMode:(current application's NSNumberFormatterRoundUp)
081  appFormatter's setNumberStyle:(current application's NSNumberFormatterDecimalStyle)
082  appFormatter's setUsesGroupingSeparator:(false)
083  appFormatter's setMaximumFractionDigits:(2)
084  #幅MM
085  set ocidWmm to appFormatter's stringFromNumber:(ocidMMWpt)
086  #縦MM
087  set ocidHmm to appFormatter's stringFromNumber:(ocidMMHpt)
088  # NSImageRepに変換
089  set ocidImageRepArray to ocidImageData's representations()
090  #ImageRepの数を数えて
091  set numCntImageRep to ocidImageRepArray's |count|()
092  if numCntImageRep = 0 then
093    set listReturnValue to {false, "NSImageRepの内容が空です"} as list
094    return listReturnValue
095  else if numCntImageRep > 1 then
096    log "マルチページデータです:最初のページのサイズを取得します"
097    set ocidItemImageData to ocidImageRepArray's firstObject()
098  else if numCntImageRep = 1 then
099    set ocidItemImageData to ocidImageRepArray's firstObject()
100    #フレームがあるか?を確認します
101    set ocidFrameCnt to (ocidItemImageData's valueForProperty:(refMe's NSImageFrameCount))
102    if ocidFrameCnt = (missing value) then
103      log "通常の画像データです"
104    else if (ocidFrameCnt's integerValue()) > 0 then
105      log "GIF画像データです"
106    end if
107  end if
108  #ピクセルサイズ取得
109  set numWpx to ocidItemImageData's pixelsWide()
110  set numHpx to ocidItemImageData's pixelsHigh()
111  #解像度(ピクセル密度)
112  set ocidDecWpx to current application's NSDecimalNumber's alloc()'s initWithString:(numWpx as text)
113  set ocidResolutioW to (ocidDecWpx's decimalNumberByDividingBy:(ocidDecWpt))
114  set ocidResolutin to (ocidResolutioW's decimalNumberByMultiplyingBy:(ocidDecPt))
115  #解像度は桁揃えしておく
116  set ocidResolution to appFormatter's stringFromNumber:(ocidResolutin)
117  set ocidResolution to appFormatter's stringFromNumber:(ocidResolutin)
118  #戻り値用のリスト
119  # {true, {{width:幅mm height:mm}, {width:幅px, height:縦px},解像度}
120  set listReturnValue to {true, {{width:(ocidWmm as real), height:(ocidHmm as real)}, {width:numWpx, height:numHpx}, (ocidResolution as real)}} as list
121  #戻す
122  return listReturnValue
123  
124end doGetImageSize
AppleScriptで生成しました

|

« [bash]lsregisterに不具合が発生した場合のリセット | トップページ | [Acrobat]用紙サイズ(media box)を変更する(仮) »

NSImage」カテゴリの記事