-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
50 lines (40 loc) · 1.62 KB
/
Copy pathProgram.cs
File metadata and controls
50 lines (40 loc) · 1.62 KB
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
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Webp;
using System.IO;
class Program
{
static void Main(string[] args)
{
// Use the folder where you run "dotnet run" (your project folder)
string basePath = Directory.GetCurrentDirectory();
string inputFolder = Path.Combine(basePath, "_input");
string outputFolder = Path.Combine(basePath, "_output");
Directory.CreateDirectory(inputFolder);
Directory.CreateDirectory(outputFolder);
Console.WriteLine($"Input folder: {inputFolder}");
Console.WriteLine($"Output folder: {outputFolder}");
var encoder = new WebpEncoder { Quality = 80 };
var files = Directory.GetFiles(inputFolder)
.Where(f =>
f.EndsWith(".png", StringComparison.OrdinalIgnoreCase) ||
f.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) ||
f.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase))
.ToList();
if (files.Count == 0)
{
Console.WriteLine("No PNG/JPG files found in the input folder.");
return;
}
foreach (var file in files)
{
using var image = Image.Load(file);
string outPath = Path.Combine(
outputFolder,
Path.GetFileNameWithoutExtension(file) + ".webp"
);
image.Save(outPath, encoder);
Console.WriteLine($"Converted: {Path.GetFileName(file)}");
}
Console.WriteLine("Done!");
}
}