Geo*_*ton 1 shell image imagemagick julia
我正在编写一个程序来处理Julia中的一系列图像,比如ImageMagick,但有一些-evaluate-sequence
不能做的事情.在花时间加载图像之前,我想快速检查以确保它们的大小,颜色深度和颜色空间都相同.我可以从ImageMagic获取这样的信息,如下所示:
identify -ping -format "%[G] %[depth] %[colorspace]" IMAGENAME.PNG
Run Code Online (Sandbox Code Playgroud)
(或.JPG或IM将阅读的任何其他内容).
计划A是使用ImageMagick.jl库,但在浏览源之后,看起来MagickWand总是首先加载图像.
有没有办法使用ImageMagick.jl(或其他一些Julia库)来获取信息而不加载文件?如果不 ...
B计划是自己发起一系列调用identify
并处理结果.我可以验证IM是否存在并加载,因为这有效:
readstring(`identify --version`)
Run Code Online (Sandbox Code Playgroud)
但:
cmd = "identify -ping -format '%[colorspace] %[depth] %[G]' MYIMAGE.JPG"
Run Code Online (Sandbox Code Playgroud)
返回一个字符串,复制并粘贴到命令行,工作得很好.但是当我尝试使用REPL时:
cmd = "identify -ping -format '%[colorspace] %[depth] %[G]' MYIMAGE.JPG"
readstring(`$cmd`)
Run Code Online (Sandbox Code Playgroud)
这是发生的事情:
ERROR: could not spawn `"identify -ping -format '%[colorspace] %[depth] %[G]' IMG_1382.JPG"`: no such file or directory (ENOENT)
in _jl_spawn(::String, ::Array{String,1}, ::Ptr{Void}, ::Base.Process, ::Base.DevNullStream, ::Base.PipeEndpoint, ::Base.TTY) at ./process.jl:321
in #424 at ./process.jl:478 [inlined]
in setup_stdio(::Base.##424#425{Cmd,Ptr{Void},Base.Process}, ::Tuple{Base.DevNullStream,Pipe,Base.TTY}) at ./process.jl:466
in #spawn#423(::Nullable{Base.ProcessChain}, ::Function, ::Cmd, ::Tuple{Base.DevNullStream,Pipe,Base.TTY}, ::Bool, ::Bool) at ./process.jl:477
in (::Base.#kw##spawn)(::Array{Any,1}, ::Base.#spawn, ::Cmd, ::Tuple{Base.DevNullStream,Pipe,Base.TTY}, ::Bool, ::Bool) at ./<missing>:0
in open(::Cmd, ::String, ::Base.DevNullStream) at ./process.jl:539
in read(::Cmd, ::Base.DevNullStream) at ./process.jl:574
in readstring at ./process.jl:581 [inlined] (repeats 2 times)
in readstring(::Cmd) at /Applications/JuliaPro-0.5.1.1.app/Contents/Resources/julia/Contents/Resources/julia/lib/julia/sys.dylib:?
Run Code Online (Sandbox Code Playgroud)
是什么导致了这个问题?
计划C将自己对文件进行低级读取,但我真的,真的不想这样做.
你绝对可以直接使用ImageMagick库; 你只需要wand
自己使用并调用未导出的pingimage
方法:
julia> wand = MagickWand()
ImageMagick.MagickWand(Ptr{Void} @0x00007f92e48fac00)
julia> ImageMagick.pingimage(wand, filename)
julia> getimagecolorspace(wand), getimagedepth(wand), size(wand)
("sRGB",8,(1873,1630))
Run Code Online (Sandbox Code Playgroud)
这是失败的,因为命令插值是特殊的.您拼接到`cmd`
对象中的每个字符串都是命令的原子部分.这需要一些习惯,但不用担心引用是绝对美妙的.因此,不要将其构建为字符串,而是直接将其构建为命令.这样做意味着命令将始终"正常工作",即使文件名中有空格和特殊shell字符.
julia> readstring(`identify -ping -format '%[colorspace] %[depth] %[G]' $filename`)
"sRGB 8 1873x1630"
Run Code Online (Sandbox Code Playgroud)
当然,直接使用库更好,因为您不需要进行字符串处理.