以下是答案的要素:
-
pdfimages
将提取图像,以便可以发现点的数量。
-
identify
将给出图像的大小,单位为PostScript点(每英寸72个)。
- 由于一些扫描仪可能将单个页面拆分为多个大小和形状不同的图像,因此关键在于添加所有图像的面积。将平方点除以平方英寸并取平方根即可得出答案。
以下是解决问题的Lua脚本。我可能可以使用纯shell,但捕获宽度和高度会更加麻烦。
#!/usr/bin/env lua
require 'osutil'
require 'posixutil'
require 'mathutil'
local function runf(...) return os.execute(string.format(...)) end
assert(arg[1], "no file on command line")
local function dimens(filename)
local cmd = [[identify -format "return %w, %h\n" $file | sed 1q]]
cmd = cmd:gsub('$file', os.quote(filename))
local w, h = assert(loadstring(os.capture(cmd)))()
assert(w and h)
return w, h
end
assert(#arg == 1, "dpi of just one file")
for _, pdf in ipairs(arg) do
local w, h = dimens(pdf)
local insquared = w * h / (72.00 * 72.00)
local imagedir = os.capture 'mktemp -d'
assert(posix.isdir(imagedir))
runf('pdfimages -f 1 -l 1 %s %s 1>&2', os.quote(pdf),
os.quote(imagedir .. '/img'))
local dotsquared = 0
for file in posix.glob(imagedir .. '/img*') do
local w, h = dimens(file)
dotsquared = dotsquared + w * h
end
os.execute('rm -rf ' .. os.quote(imagedir))
local dpi = math.sqrt(dotsquared / insquared)
if true then
io.stderr:write(insquared, " square inches\n")
io.stderr:write(dotsquared, " square dots\n")
io.stderr:write(dpi, " exact dpi\n")
io.stderr:write(math.round(dpi, 10), " rounded dpi\n")
end
print(math.round(dpi, 10))
end
width
和height
变量的单位是什么? - Norman Ramsey