Created
March 5, 2021 20:01
-
-
Save hyrmn/b7a38771946913ec0fe06ae191fa4563 to your computer and use it in GitHub Desktop.
File manipulation with ImageSharp
This file contains 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
import the following nuget package: | |
> Install-Package SixLabors.ImageSharp.Drawing -Version 1.0.0-beta11 |
This file contains 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.Drawing; | |
using SixLabors.ImageSharp.Drawing.Processing; | |
using SixLabors.ImageSharp.PixelFormats; | |
using SixLabors.ImageSharp.Processing; | |
static void Main(string[] args) | |
{ | |
var rand = new Random(); | |
var iterations = 5000; | |
var destWidth = 4096; | |
var destHeight = 4096; | |
var shapeReductionFactor = 0.002f; | |
var startingAlphaTransparency = 0.1f; | |
var alphaTransparancyIncreaseStep = 0.06f; | |
var StrokeInversionThreshold = 0.05; | |
var polyMinSides = 4; | |
var polyMaxSides = 10; | |
var initialShapeSize = 0.75 * destHeight; | |
using var source = Image.Load<Rgba32>("source3.jpg"); | |
var sourceWidth = source.Size().Width; | |
var sourceHeight = source.Size().Height; | |
using var image = new Image<Rgba32>(destWidth, destHeight); | |
image.Mutate(ic => | |
{ | |
ic.Fill(Color.Black); | |
var alpha = startingAlphaTransparency; | |
var strokeSize = initialShapeSize; | |
for(var i = 0; i < iterations; i++) | |
{ | |
var srcX = rand.Next(0, sourceWidth); | |
var srcY = rand.Next(0, sourceHeight); | |
var srcRgb = source[srcX, srcY].Rgb; | |
var destRgba = new Rgba32(srcRgb.R, srcRgb.G, srcRgb.B, (byte)alpha); | |
var edges = rand.Next(polyMinSides, polyMaxSides); | |
var x = rand.NextDouble() * destWidth; | |
var y = rand.NextDouble() * destHeight; | |
var rotation = (float)(90 * rand.NextDouble()); | |
var polygon = new RegularPolygon((float)x, (float)y, edges, (float)strokeSize, rotation); | |
var color = destRgba.ToHex(); | |
Console.WriteLine($"{i}: {srcX} {srcY} {srcRgb} {destRgba}"); | |
ic.Fill(destRgba, polygon); | |
if(false && strokeSize <= StrokeInversionThreshold * initialShapeSize) | |
{ | |
var outliner = Outliner.GenerateOutline(polygon, 1); | |
if ((srcRgb.R + srcRgb.G + srcRgb.B) / 3 < 128) | |
{ | |
ic.Fill(new Rgba32(255, 255, 255, alpha), outliner); | |
} | |
else | |
{ | |
ic.Fill(new Rgba32(0, 0, 0, alpha), outliner); | |
} | |
} | |
strokeSize -= (shapeReductionFactor * strokeSize); | |
alpha += alphaTransparancyIncreaseStep; | |
} | |
}); | |
using var outputStream = File.OpenWrite("out.png"); | |
image.SaveAsPng(outputStream); | |
Console.WriteLine("Done!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment