Skip to content

Commit

Permalink
Add Centercrop
Browse files Browse the repository at this point in the history
  • Loading branch information
bartbroere authored Mar 29, 2024
1 parent 90811ef commit 405de88
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Net;
using System.Text.Json;
using Math;

Check failure on line 7 in Program.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Math' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 7 in Program.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Math' could not be found (are you missing a using directive or an assembly reference?)
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using SixLabors.ImageSharp;
Expand All @@ -30,6 +31,18 @@ static void Main(string[] args) {
// Load an image specified as a command line argument
var image = Image.Load<Rgba32>(File.ReadAllBytes(args[0]));

// Calculate the shortest side, and use that to extract a square from the center
// Known in other image libraries as Centercrop
// AFAIK Centercrop is not available in Sixlabors.ImageSharp, so we do it manually
var smallestSide = Math.Min(image.Width, image.Height);
image.Mutate(x => x.Crop(
new Rectangle(
(image.Width - smallestSide) / 2,
(image.Height - smallestSide) / 2,
smallestSide,
smallestSide
)));

// Resize to 224 x 224 (bicubic resizing is the default)
image.Mutate(x => x.Resize(224, 224));

Expand Down

0 comments on commit 405de88

Please sign in to comment.