# imagemagick 圖片壓縮, pdf轉jpg ###### tags: `程式設計` `portable` ## 說明 convert.exe下載點 (免安裝,輕量化,官網的比較大功能也比較多) https://mega.nz/#!gQZRDSja!zx7lBGoYKyzeHPeEde2d7CzC39leiDnlaIwEtZx46fI 先放程式碼 ### jpg壓縮 ``` convert *.jpg -resize 50% -set filename:base "%[basename]" "t-%[filename:base].jpg" ``` ### ghostscript pdf轉jpg ``` gswin32c.exe -dNOPAUSE -sDEVICE=jpeg -r200 -dJPEGQ=60 -sOutputFile=page_%03d.jpg -dFirstPage=1 -dLastPage=2 oldpdf.pdf -dBATCH ``` ### ghostscript jpg轉pdf 註:^是windows cmd的換列符號,如果是linux要使用\ ``` gswin32c.exe -dNOSAFER -sDEVICE=pdfwrite -o foo.pdf ..\lib\viewjpeg.ps -c ^ "(1.jpg) viewJPEG showpage ^ (2.jpg) viewJPEG showpage ^ (3.jpg) viewJPEG showpage ^ (4.jpg) viewJPEG showpage" ``` ## 說明 convert.exe是關鍵的轉檔程式 *.jpg是指副檔名是.jpg的就要轉檔,如果需要轉多種副檔名,就多寫幾行,把jpg改掉 ### -rezise 50%是縮放比例,也可以用最大寬度或高度的數字來設定(會等比例縮小) 寬度範例 ``` convert *.jpg -resize 512x512 -set filename:base "%[basename]" "t-%[filename:base].jpg" ``` ### -strip 這是移除exif,網頁用的話這樣圖可以更小 如果要放在windows的.bat,需要把%改成%%,如下 ``` convert *.jpg -resize 50%% -set filename:base "%%[basename]" "t-%%[filename:base].jpg" ``` 兩個%%符號是因為bat的保留字所以要輸入兩個 ## 官方文件 ### convert用法 https://imagemagick.org/script/convert.php ### 存檔更改檔名的技巧 http://www.imagemagick.org/Usage/files/#save_escapes ### %[basename]可以放哪些別的變數 http://imagemagick.org/script/escape.php ## 補充 imagemagicK原始碼版,要試看看 * 從NUGET向自己的程式中加入相應的dll檔案 輸入Install-Package Magick.NET-Q16-AnyCPU -Version 7.5.0 就得到以下的dll檔案了 * 如何在程式中呼叫 ``` MagickImage image = new MagickImage(path);//獲得一個圖片物件 image.Quality = 100;//進行無失真壓縮 image.Resize(1000,1000); //整體進行尺寸調整 string SaveToPath = @rootPath “\\” reName “resize.jpg”;//對儲存路徑進行編輯 image.Write(SaveToPath);//以流的方式寫入目標路徑 image.Dispose();//物件進行釋放 ``` * 如何得到原始碼 github地址:https://github.com/dlemstra/Magick.NET 即可 ### 教學來源 https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/477703/ ## google pagespeed insight 圖片最佳化 Google建議可以使用ImageMagick處理 **GIF, PNG, JPEG**等圖片,會自動移除exif或是其他資訊讓圖片更小 ImageMagick可以從這裡看語法和下載檔案 https://www.imagemagick.org/script/convert.php 這邊提供單純只有convert的迷你免安裝版 https://www.dropbox.com/s/2og70vkbxi9mijs/miniImageMagicK.zip?dl=0 ## 指令列語法 ``` convert.exe SourceBigImage.jpg -sampling-factor 4:2:0 -strip -quality 85 -interlace JPEG -colorspace sRGB TargetSmallImage.jpg ``` 如果要覆蓋掉原始檔,就把上面的Source和Target兩個圖檔輸入相同的路徑就可以了 ## C#程式呼叫的方法 ``` //呼叫的方式 UUEIProgram.ImageOptimizer("D:\\Folder\\puzzle.jpg"); //function內容_ public static string ImageOptimizer(string filename) { var result = "success"; try { //宣告執行路徑 System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo("convert.exe"); startInfo.WorkingDirectory = @"D:\\miniImageMagicK\\"; //宣告參數 var cParams = filename + " -sampling-factor 4:2:0 -strip -quality 85 -interlace JPEG -colorspace sRGB " + filename; startInfo.Arguments = cParams; //隱藏視窗 startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //startInfo.RedirectStandardOutput = true; 尚未測試 //startInfo.CreateNoWindow = true; 無法真的穩藏視窗 //startInfo.UseShellExecute = false; 隱藏了但程式根本沒執行 System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo = startInfo; p.Start(); } catch (Exception ex) { result = ex.ToString(); } return result; } ``` ## 參考來源: ### Optimize Images https://developers.google.com/speed/docs/insights/OptimizeImages?hl=zh-tw ### c# 與ImageMagick的初次相遇 https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/477703/ ### 圖片最佳化(改版了,上面英文版訊息比較新) https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/image-optimization?hl=zh-tw
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.