Created
October 21, 2024 17:53
-
-
Save javiercampos/43b836efbb1c0b77ff827eb3a71ed09c to your computer and use it in GitHub Desktop.
Instagram batch resize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using SixLabors.ImageSharp; | |
| using SixLabors.ImageSharp.Formats.Jpeg; | |
| using SixLabors.ImageSharp.PixelFormats; | |
| using SixLabors.ImageSharp.Processing; | |
| var inputFolder = "G:\\Input"; // Your input folder | |
| var outputFolder = "G:\\Resized"; // Your output folder | |
| Directory.CreateDirectory(outputFolder); | |
| foreach (var inputFile in Directory.GetFiles(inputFolder, "*.jpg", SearchOption.AllDirectories)) | |
| { | |
| var outputFile = Path.Combine(outputFolder, Path.GetRelativePath(inputFolder, inputFile)); | |
| var dirName = Path.GetDirectoryName(outputFile); | |
| if (dirName is null) | |
| { | |
| Console.WriteLine($"Invalid folder for {outputFile}"); | |
| return 1; | |
| } | |
| Directory.CreateDirectory(dirName); | |
| if (File.Exists(outputFile)) | |
| { | |
| continue; | |
| } | |
| using var image = Image.Load<Rgba32>(inputFile); | |
| int targetWidth, targetHeight; | |
| if (image.Width > image.Height) | |
| { | |
| // Landscape | |
| targetWidth = 1080; | |
| targetHeight = 566; | |
| } | |
| else | |
| { | |
| // Portrait | |
| targetWidth = 1080; | |
| targetHeight = 1350; | |
| } | |
| Console.WriteLine($"{outputFile} {image.Width}x{image.Height} -> {targetWidth}x{targetHeight}"); | |
| image.Mutate(x => x.Resize(new ResizeOptions | |
| { | |
| Size = new Size(targetWidth, targetHeight), | |
| PadColor = Color.Black, | |
| Mode = ResizeMode.Pad, | |
| Sampler = KnownResamplers.Lanczos3 | |
| })); | |
| var encoder = new JpegEncoder { Quality = 90 }; | |
| image.Save(outputFile, encoder); | |
| } | |
| return 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment