最近一直在優化我的部落格網站,有常來逛的人應該都可以明顯感受出效能提升,我的其中一個優化項目就是開始使用最新潮的 WebP 與 AVIF 圖檔格式。這篇文章我就來分享如何透過強大的 ImageMagick 工具來批次轉換各種圖片格式。

安裝 ImageMagick 工具
ImageMagick 是一套相當複雜且多功能的工具,且支援各種作業系統平台,無論 Windows, Linux, macOS 通通都有支援,不過安裝的時候確實有些地雷!🔥
我從 ImageMagick 的 Download 頁面的 Linux Binary Release 發現下載的 magick 執行檔並沒有把 heic delegate 編譯進去,所以無法轉換圖片為 AVIF 格式的圖片! 😒
這是我今天下載最新版的時候看到的版本資訊:
$ magick -version
Version: ImageMagick 7.1.0-57 Q16-HDRI x86_64 d68553b17:20221230 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP(3.1)
Delegates (built-in): bzlib djvu fontconfig freetype jbig jng jpeg lcms lqr lzma openexr png raqm tiff webp x xml zlib
Compiler: gcc (4.2)
注意: 我在 Ubuntu 22.04.1 LTS (最新穩定版) 透過 apt install imagemagick -y 安裝 ImageMagick 已經有包含 heic 模組了!
不過,你可以改用 imagemagick-docker 專案放在 Docker Hub 上面的 Image (容器映像),直接透過容器來執行,完全免安裝,這應該是最簡單的使用方法了!👍
除此之外,我在 Windows Binary Release 下載的版本,就真的把所有可能的 Delegates 都加入了,所以直接下載安裝即可,安裝好就可以開始使用了!👍
- 
先到 Windows Binary Release 下載最新版安裝檔

以下是自動安裝的命令:
curl -sLO https://imagemagick.org/archive/binaries/ImageMagick-7.1.0-57-Q16-HDRI-x64-dll.exe
.\ImageMagick-7.1.0-57-Q16-HDRI-x64-dll.exe /silent
 
- 
安裝好之後,開啟一個新的命令提示字元(Command Prompt)視窗或 PowerShell 視窗
以下是查詢版本的方式:
magick -version
Version: ImageMagick 7.1.0-57 Q16-HDRI x64 eadf378:20221230 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenCL OpenMP(2.0)
Delegates (built-in): bzlib cairo flif freetype gslib heic jng jp2 jpeg jxl lcms lqr lzma openexr pangocairo png ps raqm raw rsvg tiff webp xml zip zlib
Compiler: Visual Studio 2022 (193431937)
如果要看 ImageMagick 所有支援的圖檔格式,可以用以下命令查詢:
magick -list format
 點擊看結果 🖱
  Format  Module    Mode  Description
-------------------------------------------------------------------------------
      3FR  DNG       r--   Hasselblad CFV/H3D39II (0.21.0-Beta1)
      3G2  VIDEO     r--   Media Container
      3GP  VIDEO     r--   Media Container
      AAI* AAI       rw+   AAI Dune image
      AI  PDF       rw-   Adobe Illustrator CS2
    APNG  VIDEO     rw+   Animated Portable Network Graphics
      ART* ART       rw-   PFS: 1st Publisher Clip Art
      ARW  DNG       r--   Sony Alpha Raw Image Format (0.21.0-Beta1)
  ASHLAR* ASHLAR    -w+   Image sequence laid out in continuous irregular courses
      AVI  VIDEO     r--   Microsoft Audio/Visual Interleaved
    AVIF  HEIC      rw+   AV1 Image File Format (1.14.0)
      AVS* AVS       rw+   AVS X image
    BAYER* BAYER     rw+   Raw mosaiced samples
  BAYERA* BAYER     rw+   Raw mosaiced and alpha samples
      BGR* BGR       rw+   Raw blue, green, and red samples
    BGRA* BGR       rw+   Raw blue, green, red, and alpha samples
    BGRO* BGR       rw+   Raw blue, green, red, and opacity samples
      BIE* JBIG      ---   Joint Bi-level Image experts Group interchange format
      BMP* BMP       rw-   Microsoft Windows bitmap image
    BMP2* BMP       rw-   Microsoft Windows bitmap image (V2)
    BMP3* BMP       rw-   Microsoft Windows bitmap image (V3)
      BRF* BRAILLE   -w-   BRF ASCII Braille format
      CAL* CALS      rw-   Continuous Acquisition and Life-cycle Support Type 1
          Specified in MIL-R-28002 and MIL-PRF-28002
    CALS* CALS      rw-   Continuous Acquisition and Life-cycle Support Type 1
          Specified in MIL-R-28002 and MIL-PRF-28002
  CANVAS* XC        r--   Constant image uniform color
  CAPTION* CAPTION   r--   Caption
      CIN* CIN       rw-   Cineon Image File
      CIP* CIP       -w-   Cisco IP phone image format
    CLIP* CLIP      rw+   Image Clip Mask
CLIPBOARD* CLIPBOARD rw-   The system clipboard
    CMYK* CMYK      rw+   Raw cyan, magenta, yellow, and black samples
    CMYKA* CMYK      rw+   Raw cyan, magenta, yellow, black, and alpha samples
      CR2  DNG       r--   Canon Digital Camera Raw Image Format (0.21.0-Beta1)
      CR3  DNG       r--   Canon Digital Camera Raw Image Format (0.21.0-Beta1)
      CRW  DNG       r--   Canon Digital Camera Raw Image Format (0.21.0-Beta1)
    CUBE* CUBE      r--   Cube LUT
      CUR* ICON      rw-   Microsoft icon
      CUT* CUT       r--   DR Halo
    DATA* INLINE    rw+   Base64-encoded inline images
      DCM* DCM       r--   Digital Imaging and Communications in Medicine image
          DICOM is used by the medical community for images like X-rays.  The
          specification, "Digital Imaging and Communications in Medicine
          (DICOM)", is available at http://medical.nema.org/.  In particular,
          see part 5 which describes the image encoding (RLE, JPEG, JPEG-LS),
          and supplement 61 which adds JPEG-2000 encoding.
      DCR  DNG       r--   Kodak Digital Camera Raw Image File (0.21.0-Beta1)
    DCRAW  DNG       r--   Raw Photo Decoder (dcraw) (0.21.0-Beta1)
      DCX* PCX       rw+   ZSoft IBM PC multi-page Paintbrush
      DDS* DDS       rw+   Microsoft DirectDraw Surface
    DFONT* TTF       r--   Multi-face font package (Freetype 2.12.1)
    DJVU* DJVU      ---   Deja vu
          See http://www.djvuzone.org/ for details about the DJVU format.  The
          DJVU 1.2 specification is available there and at
          ftp://swrinde.nde.swri.edu/pub/djvu/documents/.
      DNG  DNG       r--   Digital Negative (0.21.0-Beta1)
      DOT  DOT       ---   Graphviz
      DPS  DPS       ---   Display Postscript Interpreter
      DPX* DPX       rw-   SMPTE 268M-2003 (DPX 2.0)
          Digital Moving Picture Exchange Bitmap, Version 2.0.
          See SMPTE 268M-2003 specification at http://www.smtpe.org
    DXT1* DDS       rw+   Microsoft DirectDraw Surface
    DXT5* DDS       rw+   Microsoft DirectDraw Surface
      EMF  EMF       r--   Windows Enhanced Meta File
    EPDF  PDF       rw-   Encapsulated Portable Document Format
      EPI  PS        rw-   Encapsulated PostScript Interchange format
      EPS  PS        rw-   Encapsulated PostScript
    EPS2  PS2       -w-   Level II Encapsulated PostScript
    EPS3  PS3       -w+   Level III Encapsulated PostScript
    EPSF  PS        rw-   Encapsulated PostScript
    EPSI  PS        rw-   Encapsulated PostScript Interchange format
      EPT  EPT       rw-   Encapsulated PostScript with TIFF preview
    EPT2  EPT       rw-   Encapsulated PostScript Level II with TIFF preview
    EPT3  EPT       rw+   Encapsulated PostScript Level III with TIFF preview
      ERF  DNG       r--   Epson RAW Format (0.21.0-Beta1)
      EXR  EXR       rw-   High Dynamic-range (HDR) (OpenEXR 3.1.3)
FARBFELD* FARBFELD  rw-   Farbfeld
      FAX* FAX       rw+   Group 3 FAX
          FAX machines use non-square pixels which are 1.5 times wider than
          they are tall but computer displays use square pixels, therefore
          FAX images may appear to be narrow unless they are explicitly
          resized using a geometry of "150x100%".
      FF* FARBFELD  rw-   Farbfeld
    FILE* URL       r--   Uniform Resource Locator (file://)
    FITS* FITS      rw+   Flexible Image Transport System
    FL32* FL32      rw-   FilmLight
    FLIF* FLIF      rw+   Free Lossless Image Format (libflif 0.0.2 [0000])
      FLV  VIDEO     rw+   Flash Video Stream
      FPX  FPX       ---   FlashPix Format
  FRACTAL* PLASMA    r--   Plasma fractal image
      FTP* URL       r--   Uniform Resource Locator (ftp://)
      FTS* FITS      rw+   Flexible Image Transport System
    FTXT* FTXT      rw-   Formatted text image
      G3* FAX       rw-   Group 3 FAX
      G4* FAX       rw-   Group 4 FAX
      GIF* GIF       rw+   CompuServe graphics interchange format
    GIF87* GIF       rw-   CompuServe graphics interchange format (version 87a)
GRADIENT* GRADIENT  r--   Gradual linear passing from one shade to another
    GRAY* GRAY      rw+   Raw gray samples
    GRAYA* GRAY      rw+   Raw gray and alpha samples
  GROUP4* TIFF      rw-   Raw CCITT Group4
      GV  DOT       ---   Graphviz
    HALD* HALD      r--   Identity Hald color lookup table image
      HDR* HDR       rw+   Radiance RGBE image format
    HEIC  HEIC      r--   High Efficiency Image Format (1.14.0)
    HEIF  HEIC      r--   High Efficiency Image Format (1.14.0)
HISTOGRAM* HISTOGRAM -w-   Histogram of the image
      HRZ* HRZ       rw-   Slow Scan TeleVision
      HTM* HTML      -w-   Hypertext Markup Language and a client-side image map
    HTML* HTML      -w-   Hypertext Markup Language and a client-side image map
    HTTP* URL       r--   Uniform Resource Locator (http://)
    HTTPS* URL       r--   Uniform Resource Locator (https://)
      ICB* TGA       rw-   Truevision Targa image
      ICO* ICON      rw+   Microsoft icon
    ICON* ICON      rw-   Microsoft icon
      IIQ  DNG       r--   Phase One Raw Image Format (0.21.0-Beta1)
    INFO  INFO      -w+   The image format and characteristics
  INLINE* INLINE    rw+   Base64-encoded inline images
      IPL* IPL       rw+   IPL Image Sequence
  ISOBRL* BRAILLE   -w-   ISO/TR 11548-1 format
  ISOBRL6* BRAILLE   -w-   ISO/TR 11548-1 format 6dot
      J2C* JP2       rw-   JPEG-2000 Code Stream Syntax (2.5.0)
      J2K* JP2       rw-   JPEG-2000 Code Stream Syntax (2.5.0)
      JBG* JBIG      ---   Joint Bi-level Image experts Group interchange format
    JBIG* JBIG      ---   Joint Bi-level Image experts Group interchange format
      JNG* PNG       rw-   JPEG Network Graphics
          See http://www.libpng.org/pub/mng/ for details about the JNG
          format.
      JNX* JNX       r--   Garmin tile format
      JP2* JP2       rw-   JPEG-2000 File Format Syntax (2.5.0)
      JPC* JP2       rw-   JPEG-2000 Code Stream Syntax (2.5.0)
      JPE* JPEG      rw-   Joint Photographic Experts Group JFIF format (libjpeg-turbo 2.1.1)
    JPEG* JPEG      rw-   Joint Photographic Experts Group JFIF format (libjpeg-turbo 2.1.1)
      JPG* JPEG      rw-   Joint Photographic Experts Group JFIF format (libjpeg-turbo 2.1.1)
      JPM* JP2       rw-   JPEG-2000 File Format Syntax (2.5.0)
      JPS* JPEG      rw-   Joint Photographic Experts Group JFIF format (libjpeg-turbo 2.1.1)
      JPT* JP2       rw-   JPEG-2000 File Format Syntax (2.5.0)
    JSON  JSON      -w+   The image format and characteristics
      JXL* JXL       rw+   JPEG XL (ISO/IEC 18181) (libjxl 0.7.0)
      K25  DNG       r--   Kodak Digital Camera Raw Image Format (0.21.0-Beta1)
      KDC  DNG       r--   Kodak Digital Camera Raw Image Format (0.21.0-Beta1)
  KERNEL* KERNEL    -w-   Morphology Kernel
    LABEL* LABEL     r--   Image label
      M2V  VIDEO     rw+   MPEG Video Stream
      M4V  VIDEO     rw+   Raw VIDEO-4 Video
      MAC* MAC       r--   MAC Paint
      MAP* MAP       rw-   Colormap intensities and indices
    MASK* MASK      rw+   Image Clip Mask
      MAT  MAT       rw+   MATLAB level 5 image format
    MATTE* MATTE     -w+   MATTE format
      MEF  DNG       r--   Mamiya Raw Image File (0.21.0-Beta1)
    MIFF* MIFF      rw+   Magick Image File Format
      MKV  VIDEO     rw+   Multimedia Container
      MNG* PNG       rw+   Multiple-image Network Graphics (libpng 1.6.38)
          See http://www.libpng.org/pub/mng/ for details about the MNG
          format.
    MONO* MONO      rw-   Raw bi-level bitmap
      MOV  VIDEO     rw+   MPEG Video Stream
      MP4  VIDEO     rw+   VIDEO-4 Video Stream
      MPC* MPC       rw+   Magick Pixel Cache image format
    MPEG  VIDEO     rw+   MPEG Video Stream
      MPG  VIDEO     rw+   MPEG Video Stream
      MRW  DNG       r--   Sony (Minolta) Raw Image File (0.21.0-Beta1)
      MSL* MSL       rw+   Magick Scripting Language
    MSVG* SVG       rw+   ImageMagick's own SVG internal renderer
      MTV* MTV       rw+   MTV Raytracing image format
      MVG* MVG       rw-   Magick Vector Graphics
      NEF  DNG       r--   Nikon Digital SLR Camera Raw Image File (0.21.0-Beta1)
      NRW  DNG       r--   Nikon Digital SLR Camera Raw Image File (0.21.0-Beta1)
    NULL* NULL      rw-   Constant image of uniform color
      ORA  ORA       r--   OpenRaster format
      ORF  DNG       r--   Olympus Digital Camera Raw Image File (0.21.0-Beta1)
      OTB* OTB       rw-   On-the-air bitmap
      OTF* TTF       r--   Open Type font (Freetype 2.12.1)
      PAL* UYVY      rw-   16bit/pixel interleaved YUV
    PALM* PALM      rw+   Palm pixmap
      PAM* PNM       rw+   Common 2-dimensional bitmap format
    PANGO* PANGO     r--   Pango Markup Language (Pangocairo 1.45.2)
  PATTERN* PATTERN   r--   Predefined pattern
      PBM* PNM       rw+   Portable bitmap format (black and white)
      PCD* PCD       rw-   Photo CD
    PCDS* PCD       rw-   Photo CD
      PCL  PCL       rw+   Printer Control Language
      PCT* PICT      rw-   Apple Macintosh QuickDraw/PICT
      PCX* PCX       rw-   ZSoft IBM PC Paintbrush
      PDB* PDB       rw+   Palm Database ImageViewer Format
      PDF  PDF       rw+   Portable Document Format
    PDFA  PDF       rw+   Portable Document Archive Format
      PEF  DNG       r--   Pentax Electronic File (0.21.0-Beta1)
      PES* PES       r--   Embrid Embroidery Format
      PFA* TTF       r--   Postscript Type 1 font (ASCII) (Freetype 2.12.1)
      PFB* TTF       r--   Postscript Type 1 font (binary) (Freetype 2.12.1)
      PFM* PNM       rw+   Portable float format
      PGM* PNM       rw+   Portable graymap format (gray scale)
      PGX* PGX       rw-   JPEG 2000 uncompressed format
      PHM* PNM       rw+   Portable half float format
    PICON* XPM       rw-   Personal Icon
    PICT* PICT      rw-   Apple Macintosh QuickDraw/PICT
      PIX* PIX       r--   Alias/Wavefront RLE image format
    PJPEG* JPEG      rw-   Joint Photographic Experts Group JFIF format (libjpeg-turbo 2.1.1)
  PLASMA* PLASMA    r--   Plasma fractal image
      PNG* PNG       rw-   Portable Network Graphics (libpng 1.6.38)
          See http://www.libpng.org/ for details about the PNG format.
    PNG00* PNG       rw-   PNG inheriting bit-depth, color-type from original, if possible
    PNG24* PNG       rw-   opaque or binary transparent 24-bit RGB (zlib 1.2.12)
    PNG32* PNG       rw-   opaque or transparent 32-bit RGBA
    PNG48* PNG       rw-   opaque or binary transparent 48-bit RGB
    PNG64* PNG       rw-   opaque or transparent 64-bit RGBA
    PNG8* PNG       rw-   8-bit indexed with optional binary transparency
      PNM* PNM       rw+   Portable anymap
POCKETMOD  PDF       rw+   Pocketmod Personal Organizer
      PPM* PNM       rw+   Portable pixmap format (color)
      PS  PS        rw+   PostScript
      PS2  PS2       -w+   Level II PostScript
      PS3  PS3       -w+   Level III PostScript
      PSB* PSD       rw+   Adobe Large Document Format
      PSD* PSD       rw+   Adobe Photoshop bitmap
    PTIF* TIFF      rw+   Pyramid encoded TIFF
      PWP* PWP       r--   Seattle Film Works
      QOI* QOI       rw-   Quite OK image format
RADIAL-GRADIENT* GRADIENT  r--   Gradual radial passing from one shade to another
      RAF  DNG       r--   Fuji CCD-RAW Graphic File (0.21.0-Beta1)
      RAS* SUN       rw+   SUN Rasterfile
      RAW  DNG       r--   Raw (0.21.0-Beta1)
      RGB* RGB       rw+   Raw red, green, and blue samples
  RGB565* RGB       r--   Raw red, green, blue samples in 565 format
    RGBA* RGB       rw+   Raw red, green, blue, and alpha samples
    RGBO* RGB       rw+   Raw red, green, blue, and opacity samples
      RGF* RGF       rw-   LEGO Mindstorms EV3 Robot Graphic Format (black and white)
      RLA* RLA       r--   Alias/Wavefront image
      RLE* RLE       r--   Utah Run length encoded image
      RMF  DNG       r--   Raw Media Format (0.21.0-Beta1)
    RSVG* SVG       rw+   Librsvg SVG renderer (RSVG 2.40.20)
      RW2  DNG       r--   Panasonic Lumix Raw Image (0.21.0-Beta1)
      SCR* SCR       r--   ZX-Spectrum SCREEN$
SCREENSHOT* SCREENSHO r--   Screen shot
      SCT* SCT       r--   Scitex HandShake
      SFW* SFW       r--   Seattle Film Works
      SGI* SGI       rw+   Irix RGB image
    SHTML* HTML      -w-   Hypertext Markup Language and a client-side image map
      SIX* SIXEL     rw-   DEC SIXEL Graphics Format
    SIXEL* SIXEL     rw-   DEC SIXEL Graphics Format
SPARSE-COLOR* TXT       -w+   Sparse Color
      SR2  DNG       r--   Sony Raw Format 2 (0.21.0-Beta1)
      SRF  DNG       r--   Sony Raw Format (0.21.0-Beta1)
  STEGANO* STEGANO   r--   Steganographic image
  STRIMG* STRIMG    rw-   String to image and back
      SUN* SUN       rw+   SUN Rasterfile
      SVG* SVG       rw+   Scalable Vector Graphics (RSVG 2.40.20)
    SVGZ* SVG       rw+   Compressed Scalable Vector Graphics (RSVG 2.40.20)
    TEXT* TXT       r--   Text
      TGA* TGA       rw-   Truevision Targa image
THUMBNAIL* THUMBNAIL -w+   EXIF Profile Thumbnail
    TIFF* TIFF      rw+   Tagged Image File Format (LIBTIFF, Version 4.3.0)
  TIFF64* TIFF      rw+   Tagged Image File Format (64-bit) (LIBTIFF, Version 4.3.0)
    TILE* TILE      r--   Tile image with a texture
      TIM* TIM       r--   PSX TIM
      TM2* TIM2      r--   PS2 TIM2
      TTC* TTF       r--   TrueType font collection (Freetype 2.12.1)
      TTF* TTF       r--   TrueType font (Freetype 2.12.1)
      TXT* TXT       rw+   Text
    UBRL* BRAILLE   -w-   Unicode Text format
    UBRL6* BRAILLE   -w-   Unicode Text format 6dot
      UIL* UIL       -w-   X-Motif UIL table
    UYVY* UYVY      rw-   16bit/pixel interleaved YUV
      VDA* TGA       rw-   Truevision Targa image
    VICAR* VICAR     rw-   Video Image Communication And Retrieval
      VID* VID       rw+   Visual Image Directory
    VIFF* VIFF      rw+   Khoros Visualization image
    VIPS* VIPS      rw+   VIPS image
      VST* TGA       rw-   Truevision Targa image
    WBMP* WBMP      rw-   Wireless Bitmap (level 0) image
    WEBM  VIDEO     rw+   Open Web Media
    WEBP* WEBP      rw+   WebP Image Format (libwebp 1.2.2 [020F])
      WMF  EMF       r--   Windows Meta File
      WMV  VIDEO     rw+   Windows Media Video
      WPG* WPG       r--   Word Perfect Graphics
      X3F  DNG       r--   Sigma Camera RAW Picture File (0.21.0-Beta1)
      XBM* XBM       rw-   X Windows system bitmap (black and white)
      XC* XC        r--   Constant image uniform color
      XCF* XCF       r--   GIMP image
      XPM* XPM       rw-   X Windows system pixmap (color)
      XPS  XPS       r--   Microsoft XML Paper Specification
      XV* VIFF      rw+   Khoros Visualization image
    YAML  YAML      -w+   The image format and characteristics
    YCbCr* YCbCr     rw+   Raw Y, Cb, and Cr samples
  YCbCrA* YCbCr     rw+   Raw Y, Cb, Cr, and alpha samples
      YUV* YUV       rw-   CCIR 601 4:1:1 or 4:2:2
* native blob support
r read support
w write support
+ support for multiple images
  
透過 CLI 命令轉換圖檔
在使用 ImageMagick 命令列工具時,在 Linux 與 macOS 作業系統下,其主程式預設為 convert,但是在 Windows 作業系統下已經有內建一個 C:\Windows\System32\convert.exe 執行檔,所以在 Windows 安裝好 ImageMagick 之後的主程式為 magick.exe!
我們先來嘗試轉換單張圖片:
- 
預設轉換設定
將 demo.png 轉換為 demo1.avif 格式,使用預設品質設定
magick demo.png demo1.avif
圖片大小從 602 KB (617,214 bytes) 降為 107 KB (110,241 bytes)!
 
- 
轉換設定 80% 影像品質
將 demo.png 轉換為 demo2.avif 格式,使用 80% 品質進行轉換,這種轉換會產生一個「有損壓縮」的圖片
magick -quality "80" demo.png demo2.avif
圖片大小從 602 KB (617,214 bytes) 降為 178 KB (182,523 bytes)!
 
- 
使用進階的選項設定,提供更好的壓縮效果,但轉換時間會拉長很多
將 demo.png 轉換為 demo3.avif 格式,並使用 heic 外掛(delegate)提供的 speed 參數,這裡的 heic:speed=2 是指轉換速度,其值從 1 (最慢/品質與壓縮比最高) ~ 10 (最快/品質與壓縮比最差)。
magick -define "heic:speed=2" demo.png demo3.avif
圖片大小從 602 KB (617,214 bytes) 降為 82.7 KB (84,759 bytes)!
 
透過 PowerShell 批次轉換圖檔
當你決定好轉換的參數,就可以透過 PowerShell 批次轉換圖檔了!
- 
將當前目錄下所有 *.png 圖檔轉換為 *.avif 與 *.webp 格式
$sourceFolder = "${PWD.Path}\*"
$filePatterns = '*.png','*.jpg','*.gif'
Get-ChildItem -Path $sourceFolder -Include $filePatterns -File | ForEach-Object {
    $name = $_.FullName
    $webp = [System.IO.Path]::ChangeExtension($name,".webp")
    $avif = [System.IO.Path]::ChangeExtension($name,".avif")
    echo "Orig: $name"
    echo "WebP: $webp"
    magick -quality 75 "$name" "$webp"
    echo "AVIF: $avif"
    magick -quality 75 "$name" "$avif"
    echo ""
}
 
- 
將當前目錄包含其子目錄下所有 *.png 圖檔轉換為 *.avif 與 *.webp 格式
$sourceFolder = "${PWD.Path}\*"
$filePatterns = '*.png','*.jpg','*.gif'
Get-ChildItem -Path $sourceFolder -Include $filePatterns -File -Recurse | ForEach-Object {
    $name = $_.FullName
    $webp = [System.IO.Path]::ChangeExtension($name,".webp")
    $avif = [System.IO.Path]::ChangeExtension($name,".avif")
    echo "Orig: $name"
    echo "WebP: $webp"
    magick -quality 75 "$name" "$webp"
    echo "AVIF: $avif"
    magick -quality 75 "$name" "$avif"
    echo ""
}
 
透過 Bash 與 Docker 批次轉換圖檔
當你決定好轉換的參數,也可以透過 Docker 批次轉換圖檔!
- 
將當前目錄下所有 *.png 圖檔轉換為 *.avif 與 *.webp 格式
for file in $(find $PWD -maxdepth 1 -name "*.png" -o -name "*.jpg" -o -name "*.gif"); do
    echo $file
    file=${file/$PWD/\/imgs}
    docker run --rm -w '/imgs' -v "$PWD:/imgs" "dpokidov/imagemagick" ${file} ${file/.png/.avif}
    docker run --rm -w '/imgs' -v "$PWD:/imgs" "dpokidov/imagemagick" ${file} ${file/.png/.webp}
done
 
- 
將當前目錄包含其子目錄下所有 *.png 圖檔轉換為 *.avif 與 *.webp 格式
for file in $(find $PWD -name "*.png" -o -name "*.jpg" -o -name "*.gif"); do
    echo $file
    file=${file/$PWD/\/imgs}
    docker run --rm -w '/imgs' -v "$PWD:/imgs" "dpokidov/imagemagick" ${file} ${file/.png/.avif}
    docker run --rm -w '/imgs' -v "$PWD:/imgs" "dpokidov/imagemagick" ${file} ${file/.png/.webp}
done
 
相關連結