Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Some algos to draw things from things. For all of these, Install-Package SixLabors.ImageSharp.Drawing -Version 1.0.0-beta11
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
var destWidth = 1920;
var destHeight = 1080;
using var image = new Image<Rgba32>(destWidth, destHeight);
var rand = new Random();
image.Mutate(ic =>
{
ic.Fill(Color.White);
var rotation = GeometryUtilities.DegreeToRadian(45);
for(var row = 1; row < 27; row++)
{
for(var col = 1; col < 48; col++)
{
var r = (byte)rand.Next(0, 255);
var g = (byte)rand.Next(0, 255);
var b = (byte)rand.Next(0, 255);
var squareColor = new Color(new Rgba32(r, g, b, 255));
var polygon = new RegularPolygon(40 * col, 40 * row, 4, 20, rotation);
ic.Fill(squareColor, polygon);
}
}
});
using var outputStream = File.OpenWrite("generated.png");
image.SaveAsPng(outputStream);
Console.WriteLine("Done!");
//Note, there are a few methods for playing around
class Program
{
static void Main(string[] args)
{
SortedRowsAndColumns();
Console.WriteLine("Hello World!");
}
private static void SimpleSort()
{
using var source = Image.Load<Rgb24>("source4.jpg");
var sourceWidth = source.Size().Width;
var sourceHeight = source.Size().Height;
using var dest = new Image<Rgb24>(sourceWidth, sourceHeight);
for (var row = 0; row < sourceHeight; row++)
{
var pixels = source.GetPixelRowSpan(row).ToArray();
var orderedPixels = pixels.OrderBy(p => p.R + p.G + p.B).ToArray();
for (var col = 0; col < orderedPixels.Length; col++)
{
dest[col, row] = orderedPixels[col];
}
}
using var outputStream = File.OpenWrite("out4.jpg");
dest.SaveAsJpeg(outputStream);
}
private static void SortedRowsAndColumns()
{
using var source = Image.Load<Rgb24>("source2.jpg");
var sourceWidth = source.Size().Width;
var sourceHeight = source.Size().Height;
using var dest = new Image<Rgb24>(sourceWidth, sourceHeight);
var rows = new List<(long rowWeight, Rgb24[] pixels)>();
for (var rowIdx = 0; rowIdx < sourceHeight; rowIdx++)
{
var pixels = source.GetPixelRowSpan(rowIdx).ToArray();
var orderedPixels = pixels.OrderBy(p => p.R + p.G + p.B).ToArray();
rows.Add((orderedPixels.Sum(p => p.R + p.G + p.B), orderedPixels));
}
var row = 0;
foreach(var pixelRow in rows.OrderByDescending(r => r.rowWeight))
{
for (var col = 0; col < sourceWidth; col++)
{
dest[col, row] = pixelRow.pixels[col];
}
row++;
}
using var outputStream = File.OpenWrite("outsorted.jpg");
dest.SaveAsJpeg(outputStream);
}
private static void TwoTogether()
{
using var sourceLeft = Image.Load<Rgb24>("source2.jpg");
using var sourceRight = Image.Load<Rgb24>("source6.png");
var sourceWidth = Math.Min(sourceLeft.Size().Width, sourceRight.Size().Width);
var sourceHeight = Math.Min(sourceLeft.Size().Height, sourceRight.Size().Height);
var destWidth = sourceWidth * 2;
var destHeight = sourceHeight;
using var dest = new Image<Rgb24>(destWidth, destHeight);
for (var row = 0; row < sourceHeight; row++)
{
var leftPixels = sourceLeft.GetPixelRowSpan(row).ToArray().OrderBy(p => p.R + p.G + p.B).ToArray();
var rightPixels = sourceRight.GetPixelRowSpan(row).ToArray().OrderByDescending(p => p.R + p.G + p.B).ToArray();
for (var col = 0; col < sourceWidth; col++)
{
dest[col, row] = leftPixels[col];
dest[col + sourceWidth, row] = rightPixels[col];
}
}
using var outputStream = File.OpenWrite("outcomb.jpg");
dest.SaveAsJpeg(outputStream);
}
}
static void Main(string[] args)
{
var rand = new Random();
var iterations = 5000;
var destWidth = 4800;
var destHeight = 1345;
var shapeReductionFactor = 0.002f;
var startingAlphaTransparency = 0.1f;
var alphaTransparancyIncreaseStep = 0.06f;
var polyMinSides = 4;
var polyMaxSides = 10;
var initialShapeSize = 0.75 * destHeight;
var maxSizeToGetBorder = 0.05 * initialShapeSize;
using var source = Image.Load<Rgba32>("source4.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 <= maxSizeToGetBorder * 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