-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImg2WebP.ps1
199 lines (162 loc) · 13.8 KB
/
Img2WebP.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
$WebPEncoderPath = "C:\Codecs\LibWebPV1.3.0\bin\cwebp.exe"
$GIF2WebPEncoderPath = "C:\Codecs\LibWebPV1.3.0\bin\gif2webp.exe"
# Download CWEBP.EXE and GIF2WEBP.EXE at: https://developers.google.com/speed/webp/docs/precompiled
$FFMPEGPath = "C:\Codecs\LibFFMPEGV6.0\bin\ffmpeg.exe"
# Download FFMPEG.EXE at: https://www.gyan.dev/ffmpeg/builds/
$JPEGXRDecoderPath = "C:\Codecs\LibJXRv2019.10.9\LibJXRv2019.10.9_Release_x64\JXRDecApp.exe"
# Download JXRDECAPP.EXE at: https://github.com/Knewest/precompiled-jxrlib-binaries/releases/tag/Release-v2019.10.9
$JPEGXLDecoderPath = "C:\Codecs\LibJXLv0.8.1\djxl.exe"
$JPEGXLEncoderPath = "C:\Codecs\LibJXLv0.8.1\cjxl.exe"
# Download DJXL.EXE and CJXL.EXE at: https://jpeg.org/jpegxl/software.html
$IdentifyPath = "C:\Codecs\LibImageMagick\ImageMagick-7.1.1-Q16-HDRI\identify.exe"
$MagickPath = "C:\Codecs\LibImageMagick\ImageMagick-7.1.1-Q16-HDRI\magick.exe"
# Download MAGICK.EXE and IDENTIFY.EXE at: https://imagemagick.org/script/download.php#windows
#> $NvidiaDecoderPath = "C:\Codecs\NVIDIA Texture Tools\nvcompress.exe"
# Download NVCOMPRESS.EXE at: https://developer.nvidia.com/gpu-accelerated-texture-compression <#
# Load assembly so messages can actually appear on your screen.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# This will search for PNG, APNG, JPG, JPEG, JPE, JIF, JFIF, JIF, BMP, DIB, RLE, AVIF, TIF, TIFF, JXR, HDP, WDP, WMP, JXL, JP2, J2C, JPG2, JPF, JPX, J2K, TGA, DDS, HEIC, HEIF and GIF files in the current directory and its subdirectories.
$images = Get-ChildItem -Path (Get-Location) -Include *.png, *.apng, *.jpg,*.jpeg,*.jpe,*.jif,*.jfif,*.jfi,*.bmp,*.dib,*.rle,*.avif,*.tif,*.tiff,*.jxr,*.hdp,*.wdp,*.wmp,*.jxl,*.jp2,*.j2c,*.jpg2,*.jpf,*.jpx,*.j2k,*.tga,*.dds,*.heic,*.heif,*.gif -Recurse
# Ask the user if they want to create a folder to put the converted files in.
$createWebPFolder = [System.Windows.Forms.MessageBox]::Show('Would you like to create a folder to put the converted files in?
This will copy all the directories the source images are in and place them in a "WebP_Output" folder.
Otherwise, it will just output the results to the same folder as the source image.', " ", [System.Windows.Forms.MessageBoxButtons]::YesNo)
# Set the path where the converted files will be stored.
if ($createWebPFolder -eq "Yes") {
$outputPath = (Get-Location).Path + "\WebP_Output"
New-Item -ItemType Directory -Path $outputPath -Force
} else {
$outputPath = (Get-Location).Path
}
foreach ($image in $images) {
$currentPath = Get-Location
$relativePath = ""
if($image.DirectoryName.Length -ge ($currentPath.Path.Length + 1)) {
$relativePath = $image.DirectoryName.Substring(($currentPath.Path.Length + 1))
}
$outputDir = Join-Path $outputPath $relativePath
if (!(Test-Path $outputDir)) {
New-Item -ItemType Directory -Path $outputDir -Force
}
$fileName = Join-Path $outputDir $image.BaseName
if ($image.Extension -eq ".png" -or $image.Extension -eq ".apng") {
# Check if IDENTIFY.EXE encoder/decoder exists, otherwise prompt user to download.
if (-not(Test-Path $IdentifyPath)) {
[System.Windows.Forms.MessageBox]::Show("The ImageMagick resolver (IDENTIFY.EXE) was not found.`n_________________________________________________________________________`n`nPlease download it at the link below:`n`nhttps://imagemagick.org/script/download.php#windows`n_________________________________________________________________________`n`nIf you already have them installed, make sure the directories in the code is set correctly.`nThe code can be opened using Notepad.")
break
}
# Use IDENTIFY.EXE to determine if the image is an animated PNG or not.
$identifyOutput = & $IdentifyPath $image.FullName | Select-String -Pattern "PNG|APNG"
# If the image is an animated PNG, convert it to an animated WebP using FFMPEG.EXE.
if ($identifyOutput) {
# Check if FFMPEG.EXE encoder/decoder exists, otherwise prompt user to download.
if (-not(Test-Path $FFMPEGPath)) {
[System.Windows.Forms.MessageBox]::Show("The FFMPEG encoder/decoder (FFMPEG.EXE) was not found.`n_________________________________________________________________________`n`nPlease download it at the link below:`n`nhttps://www.gyan.dev/ffmpeg/builds/`n_________________________________________________________________________`n`nIf you already have it installed, make sure the directories in the code is set correctly.`nThe code can be opened using Notepad.")
break
}
& $FFMPEGPath -i $image.FullName -loop 0 -c:v libwebp_anim -compression_level 4 -lossless 1 -an -sn "$fileName.webp"
}
# If the image is not an animated PNG, convert it to a static WebP using FFMPEG.EXE too.
}
elseif ($image.Extension -eq ".jxr" -or $image.Extension -eq ".wdp" -or $image.Extension -eq ".hdp") {
# Check if JPEGXRDecoderPath and WebPEncoderPath exist, otherwise prompt user to download.
if (-not(Test-Path $JPEGXRDecoderPath) -or -not(Test-Path $WebPEncoderPath)) {
[System.Windows.Forms.MessageBox]::Show("The JPEG XR decoder (JXRDECAPP.EXE) and/or the WebP encoder (CWEBP.EXE) were not found.`n_________________________________________________________________________`n`nPlease download them at the links below:`n`nhttps://github.com/Knewest/precompiled-jxrlib-binaries/releases/tag/Release-v2019.10.9`nhttps://developers.google.com/speed/webp/docs/precompiled`n_________________________________________________________________________`n`nIf you already have them installed, make sure the directories in the code is set correctly.`nThe code can be opened using Notepad.")
break
}
# This will losslessly convert JXR to TIF using JXRDECAPP.EXE.
$tempTIFPath = Join-Path $outputDir ($image.BaseName + ".tif")
& $JPEGXRDecoderPath -i $image.FullName -o $tempTIFPath
# This will losslessly convert the temporary PNG to WebP using CWEBP.EXE.
& $WebPEncoderPath -lossless -z 6 $tempTIFPath -o "$fileName.webp"
# Delete the temporary TIF file.
Remove-Item $tempTIFPath
}
elseif ($image.Extension -eq ".jpg" -or $image.Extension -eq ".jpeg" -or $image.Extension -eq ".jpe" -or $image.Extension -eq ".jfif" -or $image.Extension -eq ".jif" -or $image.Extension -eq ".jfi") {
if (-not(Test-Path $JPEGXLEncoderPath)) {
[System.Windows.Forms.MessageBox]::Show("The JPEG XL encoder (CJXL.EXE), JPEG XL decoder (DJXL.EXE) and/or the WebP encoder (CWEBP.EXE) was not found.`n_________________________________________________________________________`n`nPlease download them at the links below:`n`nhttps://jpeg.org/jpegxl/software.html`nhttps://jpeg.org/jpegxl/software.html`nhttps://developers.google.com/speed/webp/docs/precompiled`n_________________________________________________________________________`n`nIf you already have them installed, make sure the directories in the code is set correctly.`nThe code can be opened using Notepad.")
break
}
# Convert the image to JXL format using CJXL.EXE.
$tempJXLPath = Join-Path $outputDir ($image.BaseName + ".jxl")
& $JPEGXLEncoderPath --lossless_jpeg=0 --jpeg_reconstruction_cfl=0 -q 100 -e 8 --brotli_effort 9 --faster_decoding=4 $image.FullName $tempJXLPath
# Convert the JXL file to PNG format using DJXL.EXE.
$tempPNGPath = Join-Path $outputDir ($image.BaseName + ".png")
& $JPEGXLDecoderPath -q 100 $tempJXLPath $tempPNGPath
# Convert the temporary PNG file to a lossless WebP format using CWEBP.EXE.
$WebPPath = Join-Path $outputDir ($image.BaseName + ".webp")
& $WebPEncoderPath $tempPNGPath -lossless -z 6 -o $WebPPath
# Delete the temporary files.
Remove-Item $tempJXLPath
Remove-Item $tempPNGPath
}
<# elseif ($image.Extension -eq ".hh" -or $image.Extension -eq ".hh") {
# Check if ImageMagick is installed, otherwise prompt user to download it.
if(-not(Test-Path $NvidiaDecoderPath)) {
[System.Windows.Forms.MessageBox]::Show("The Nvidia decoder (NVCOMPRESS.EXE) was not found.`n_________________________________________________________________________`n`nPlease download it at the link below:`n`nhttps://developer.nvidia.com/gpu-accelerated-texture-compression`n_________________________________________________________________________`n`nIf you already have it installed, make sure the directories in the code are set correctly.`nThe code can be opened using Notepad.")
break
}
# This will losslessly convert DDS and TGA to WebP using NVCOMPRESS.EXE.
$tempPNGPath = Join-Path $outputDir ($image.BaseName + ".png")
& $NvidiaDecoderPath -highest -alpha -profile -nomips -bc7 $image.FullName $tempPNGPath
# This will losslessly convert the temporary PNG to WebP using CWEBP.EXE.
& $WebPEncoderPath -lossless -z 6 $tempPNGPath -o "$fileName.webp"
# Delete the temporary PNG file.
Remove-Item $tempPNGPath
} #>
elseif ($image.Extension -eq ".heic" -or $image.Extension -eq ".heif" -or $image.Extension -eq ".avif" -or $image.Extension -eq ".dds" -or $image.Extension -eq ".tga") {
# Check if ImageMagick is installed, otherwise prompt user to download it.
if(-not(Test-Path $MagickPath)) {
[System.Windows.Forms.MessageBox]::Show("The ImageMagick encoder/decoder (MAGICK.EXE) was not found.`n_________________________________________________________________________`n`nPlease download it at the link below:`n`nhttps://imagemagick.org/script/download.php#windows`n_________________________________________________________________________`n`nIf you already have it installed, make sure the directories in the code are set correctly.`nThe code can be opened using Notepad.")
break
}
# This will losslessly convert DDS, TGA, HEIC, HEIF and AVIF to WebP using ImageMagick.
& $MagickPath -define webp:lossless=true -define webp:method=4 $image.FullName "$fileName.webp"
}
elseif ($image.Extension -eq ".jp2" -or $image.Extension -eq ".j2c" -or $image.Extension -eq ".jpg2" -or $image.Extension -eq ".jpf" -or $image.Extension -eq ".jpx" -or $image.Extension -eq ".j2k") {
# Check if FFMPEG exists, otherwise prompt user to download it.
if (-not(Test-Path $FFMPEGPath)) {
[System.Windows.Forms.MessageBox]::Show("The FFMPEG encoder/decoder (FFMPEG.EXE) was not found.`n_________________________________________________________________________`n`nPlease download it at the link below:`n`nhttps://www.gyan.dev/ffmpeg/builds/`n_________________________________________________________________________`n`nIf you already have it installed, make sure the directories in the code is set correctly.`nThe code can be opened using Notepad.")
break
}
# This will losslessly covert JPEG 2000 to WebP.
& $FFMPEGPath -i $image.FullName -c:v libwebp -compression_level 4 -lossless 1 -an -sn "$fileName.webp"
}
elseif ($image.Extension -eq ".jxl") {
# Check if JPEGXLDecoderPath and WebPEncoderPath exist, otherwise prompt user to download.
if (-not(Test-Path $JPEGXLDecoderPath) -or -not(Test-Path $WebPEncoderPath)) {
[System.Windows.Forms.MessageBox]::Show("The JPEG XL decoder (DJXL.EXE) and/or the WebP encoder (CWEBP.EXE) were not found.`n_________________________________________________________________________`n`nPlease download them at the links below:`n`nhttps://jpeg.org/jpegxl/software.html`nhttps://developers.google.com/speed/webp/docs/precompiled`n_________________________________________________________________________`n`nIf you already have them installed, make sure the directories in the code is set correctly.`nThe code can be opened using Notepad.")
break
}
# This will losslessly convert JXL to PNG using DJXL.EXE.
$tempPNGPath = Join-Path $outputDir ($image.BaseName + ".png")
& $JPEGXLDecoderPath -q 100 $image.FullName $tempPNGPath
# This will losslessly convert the temporary PNG to WebP using CWEBP.EXE.
& $WebPEncoderPath -lossless -z 6 $tempPNGPath -o "$fileName.webp"
# Delete the temporary PNG file.
Remove-Item $tempPNGPath
}
elseif ($image.Extension -eq ".gif") {
# Check if GIF2WEBP.EXE encoder exists, otherwise prompt user to download.
if (-not(Test-Path $GIF2WebPEncoderPath)) {
[System.Windows.Forms.MessageBox]::Show("The WebP encoders (CWEBP.EXE and/or GIF2WEBP.EXE) were not found.`n_________________________________________________________________________`n`nPlease download both of them at the link below:`n`nhttps://developers.google.com/speed/webp/docs/precompiled`n_________________________________________________________________________`n`nIf you already have them installed, make sure the directories in the code is set correctly.`nThe code can be opened using Notepad.")
break
}
# This will losslessly convert animated GIF to animated WebP using GIF2WEBP.EXE.
& $GIF2WebPEncoderPath -m 4 $image.FullName -o "$fileName.webp"
} else {
# This will losslessly convert all other image formats to WebP using CWEBP.EXE.
& $WebPEncoderPath -lossless -z 6 $image.FullName -o "$fileName.webp"
}
}
# This will show a popup message once all files are converted to WebP.
$msgBoxTitle = "Image conversion complete:"
$msgBoxText = "The image conversion process has finished.`nEnjoy your WebP files!`n_______________________________________________________________`n`n`© Knew (2023-2023)`nThis program is licensed under Boost Software License 1.0.`n_______________________________________________________________`n`nSource: https://github.com/Knewest"
$msgBoxButton = "OK"
$msgBoxIcon = "Information"
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.MessageBox]::Show($msgBoxText, $msgBoxTitle, $msgBoxButton, $msgBoxIcon)
# Version 1.3.0 of Img2WebP
# Copyright (Boost Software License 1.0) 2023-2023 Knew