-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
48 lines (39 loc) · 1.68 KB
/
Program.cs
File metadata and controls
48 lines (39 loc) · 1.68 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
using System.Drawing;
using System.Runtime.Versioning;
using ImageColorQuantizer.Services;
using ImageColorQuantizer.Utils;
class Program
{
[SupportedOSPlatform("windows")]
static void Main(string[] args)
{
// ensures there is a valid image path
if (args.Length == 0 || !File.Exists(args[0]))
{
Console.WriteLine("Usage: ImageColorQuantizer <image_path> [color count]");
return;
}
string path = args[0];
int colorCount = args.Length > 1 && int.TryParse(args[1], out int count) ? count : 4;
// loads the image and processes it
Bitmap bitmap = new Bitmap(path);
//gets the pixels from the image
var pixels = ImageLoader.ExtractPixels(bitmap);
// converts the pixels to RGB tuples
var rgbTuples = ColorUtils.ConvertToRGBTuples(pixels);
// removes duplicates
rgbTuples = rgbTuples.Distinct().ToList();
// quantizes the image using K-means algorithm and order the palette by brightness
var palette = QuantizationService.KmeansQuantizer(rgbTuples, colorCount);
palette = palette
.OrderBy(c => c.R + c.G + c.B) // Brightness sort
.ToList();
// converts the image to the quantized palette
var quantizedImage = QuantizationService.RecolorImage(bitmap, palette);
// saves the quantized image to the output directory
Directory.CreateDirectory("ImageOutput");
string outputPath = Path.Combine("ImageOutput", "quantized.png");
quantizedImage.Save(outputPath, System.Drawing.Imaging.ImageFormat.Png);
Console.WriteLine($"Saved quantized image to: {outputPath}");
}
}