我想制作一张由我附上的个人资料图片组成的图片:
答案 0 :(得分:3)
计算每个配置文件图像的平均RGB值。
在Photoshop(或任何其他图形编辑软件)中,创建包含每种颜色的自定义调色板
加载要渲染的照片,然后将其缩小,使宽度和高度(以像素为单位)与每个尺寸中要包含的个人影像数量相对应。
使用在步骤1中创建的调色板将图像的位深度减少到8位或更少。确保在执行此操作时选择“抖动”选项。
编写脚本以读取下采样图像并创建一个更大的图像,其中下采样图像的每个像素都转换为单个轮廓图像。
可能的增强功能:如果有超过256个配置文件图像,您可能会得到比单个颜色表中可容纳的颜色更多的颜色。将相似的颜色聚类在一起,并在渲染大图像时从这些组中随机选择图像。您甚至可以根据光线和阴影的分布与原始图像的相应部分中的分布的匹配程度来选择图像。
答案 1 :(得分:0)
答案 2 :(得分:0)
我今天在C#中编写了一个例行程序。我看到了一张照片马赛克,我突然想到你会怎么做,所以我把它扔在一起作为一种概念证明。我第二次尝试。有点吹嘘我的想法:
public void BuildMosaic(string srcFolder, string picFileSrc, string mosaicFile, uint mosaicSizeMultiplier, Size numTiles)
{
// The file we're going to create a mosaic of
Image srcPic = Image.FromFile(picFileSrc);
int mosaicWidth = srcPic.Width * (int)mosaicSizeMultiplier;
int mosaicHeight = srcPic.Height * (int)mosaicSizeMultiplier;
int thumbWidth = mosaicWidth / numTiles.Width;
int thumbHeight = mosaicHeight / numTiles.Height;
List<ImageInfo> imageInfos = new List<ImageInfo>();
foreach (string filename in Directory.GetFiles(srcFolder))
{
string ext = Path.GetExtension(filename).ToUpper();
if (ext == ".JPG" || ext == ".PNG" || ext == ".GIF" || ext == ".JPEG")
{
imageInfos.Add(ImageInfo.FromImage(filename, new Size(thumbWidth, thumbHeight)));
}
}
int segmentWidth = srcPic.Width / numTiles.Width;
int segmentHeight = srcPic.Height / numTiles.Height;
Image mosaic = new Bitmap(mosaicWidth, mosaicHeight);
Bitmap segBmp = new Bitmap(segmentWidth, segmentHeight);
for (int tileX = 0; tileX < numTiles.Width; tileX++)
for(int tileY = 0; tileY < numTiles.Height; tileY++)
{
// Create a bitmap from the original image that we'll try to match a tile to.
using (Graphics g = Graphics.FromImage(segBmp))
{
g.DrawImage(srcPic, new Rectangle(0, 0, segmentWidth, segmentHeight), new Rectangle(tileX * segmentWidth, tileY * segmentHeight, segmentWidth, segmentHeight), GraphicsUnit.Pixel);
}
ImageInfo segInfo = ImageInfo.FromImage(segBmp);
// Find the matching tile and paint it onto our mosaic
ImageInfo match = segInfo.FindMatch(imageInfos.ToArray());
using (Graphics g = Graphics.FromImage(mosaic))
{
g.DrawImage(match.Thumbnail, tileX * thumbWidth, tileY * thumbHeight);
}
}
segBmp.Dispose();
mosaic.Save(mosaicFile, ImageFormat.Jpeg);
mosaic.Dispose();
}
和ImageInfo类:
public class ImageInfo
{
private ImageInfo()
{
}
public string Filename { get; private set; }
public int Blue { get; private set; }
public int Green { get; private set; }
public int Red { get; private set; }
public System.Drawing.Image Thumbnail { get; private set; }
// Calculate color distance
private float CalcDistance(ImageInfo otherImage)
{
int blueDiff = Math.Abs(Blue - otherImage.Blue);
int greenDiff = Math.Abs(Green - otherImage.Green);
int redDiff = Math.Abs(Red - otherImage.Red);
return (float) Math.Sqrt(blueDiff * blueDiff + greenDiff * greenDiff + redDiff * redDiff);
}
// Find the image with the closes matching color average
internal ImageInfo FindMatch(ImageInfo[] list)
{
ImageMatch closest = null;
foreach (ImageInfo ii in list)
{
if (closest == null)
{
closest = new ImageMatch()
{
Distance = CalcDistance(ii),
Info = ii
};
continue;
}
float dist = CalcDistance(ii);
if (dist < closest.Distance)
{
closest = new ImageMatch()
{
Distance = CalcDistance(ii),
Info = ii
};
}
}
return closest.Info;
}
internal static ImageInfo FromImage(System.Drawing.Bitmap srcBmp)
{
ImageStatistics stats = new ImageStatistics(srcBmp);
return new ImageInfo()
{
Blue = (int)stats.Blue.Mean,
Green = (int)stats.Green.Mean,
Red = (int)stats.Red.Mean
};
}
internal static ImageInfo FromImage(string filename, System.Drawing.Size thumbSize)
{
using(System.Drawing.Bitmap bmp = System.Drawing.Bitmap.FromFile(filename) as System.Drawing.Bitmap)
{
ImageStatistics stats = new ImageStatistics(bmp);
return new ImageInfo()
{
Filename = filename,
Blue = (int)stats.Blue.Mean,
Green = (int)stats.Green.Mean,
Red = (int)stats.Red.Mean,
Thumbnail = new System.Drawing.Bitmap(bmp, thumbSize)
};
}
}
internal class ImageMatch
{
public float Distance { get; set; }
public ImageInfo Info { get; set; }
}
}
第一个参数是包含所有“tile”图像的目录。它们可以是任何尺寸。 第二个参数是您要变成马赛克的图像 第三个参数是输出镶嵌文件。 第四是乘数。它采用参数2中的图像,并将高度和宽度乘以该值,这就是马赛克文件的大小。 最后一个参数是构成马赛克的X和y图块的数量。
它使用AForge图像处理库。