Lua+GraphicsMagicks 实现图片批量处理(筛选图片,压缩等相关操作)

/ linux / 0 条评论 / 1736浏览

列表项目

GraphicsMagicks安装

直接上官网链接吧

http://www.graphicsmagick.org/INSTALL-unix.html

Lua安装

Building from source Lua is very easy to build and install. Just download it and follow the instructions in the package.

Here is a simple terminal session that downloads the current release of Lua and builds it in a Linux system:

curl -R -O http://www.lua.org/ftp/lua-5.3.5.tar.gz
tar zxf lua-5.3.5.tar.gz
cd lua-5.3.5
make linux test

For Mac OS X, use make macosx test. If you don't have cjiaourl, try wget. If you use Windows and want to build Lua from source, there are detailed instructions in the wiki.


功能说明

if fileSize>400 then

修改上面代码的 400 可以指定大于多少kb的图片会被转换

if getExtension(path)=="jpg" then

修改上面的可以转换指定类型的图片

两条合起来就是 大于400kb的jpg格式的图片会被转换

脚本部分

--gm convert -quality 95

-- os.execute("gm convert -quality 85 +profile "*" test.jpg test-gm.jpg");

require "io"  
require "lfs" 

function getpathes(rootpath, pathes)  
    pathes = pathes or {}
    for entry in lfs.dir(rootpath) do
        if entry ~= '.' and entry ~= '..' then  
            local path = rootpath .. '/' .. entry  
            local attr = lfs.attributes(path)
            assert(type(attr) == 'table')
              if(attr ~= nil) then
            if attr.mode == 'directory' then
                getpathes(path, pathes)
            else  
               if getExtension(path)=="jpg" then
                   tt=lfs.attributes(path,"size")
                   fileSize=tt/1000
                  if fileSize>400 then
                  print("Path:"..path.."-------文件大小:"..fileSize.."KB")
                  table.insert(pathes, path)  
                  end
               end
            end 
            end 
        end  
    end  
    return pathes  
end  

function getExtension(str)  
    return str:match(".+%.(%w+)$")  
end  

names={}
names=getpathes("/home/luke/桌面/test")
print(type(names))
for i,v in ipairs(names) do
   gm="gm convert -quality 80 +profile \"*\" "..v.." "..v
   os.execute(gm);
end