Coding Coder Art

January 22nd, 2010


closeThis post was published 7 months 13 days ago and as such, probably does not reflect my current opinion or ability.

Starting work on a 3D project as of yet lacking in assets, I was considering cracking open Paint.Net to create some default textures. However, being a programmer rather than an artist, I figured I’d better investigate some alternatives. I reduced my work, and made even more hideous coder art in the process!

I ended up trying two options: .Net Graphics support (which uses GDI) and everyone’s favourite image manipulation tool ImageMagick. My aim was to create bright an horrible images, to ensure they don’t remain in the project by mistake, with text on them to show what object my many cubes and spheres represent.

First up, my C# script. It created the fantastic texture you see above, and can be run from a windows command line as
> DefaultTexGen.exe Text_To_Write FilenameToCreate.bmp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
 
//creates a default texture with text on it
namespace DefaultTexGen
{
    class Program
    {
        static void Main(string[] args)
        {
            //check whether arguments were passed
            if (args.Length < 2)
            {
                Console.WriteLine("Please run with arguments for image text and output file name.");
                return;
            }
 
            //get name and output file from arguments
            string name = args[0];
            string file = args[1];
 
            //set width and height (square), could get these from args too
            int dimension = 256;
 
            //create the bitmap
            Bitmap genmap = new Bitmap(dimension, dimension);
 
            //apply the colour and text
            FillTexture(genmap);
            WriteText(name, genmap);
 
            //output bitmap
            genmap.Save(file);
            Console.WriteLine("Image " + file + " created.");
        }
    }
}

This relies on two further functions. The first of these colours the image with a simple gradient, but could be replaced with any colouring function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
        //fill a bitmap with a random gradient
        //could replace with other colouring function
        static void FillTexture(Bitmap _target)
        {
 
            //set random r,g,b values between 0 and 1
            Random rand = new Random();
            float rCol = (float)rand.NextDouble();
            float gCol = (float)rand.NextDouble();
            float bCol = (float)rand.NextDouble();
 
            //fill each pixel
            for (int x = 0; x < _target.Width; x++)
            {
                for (int y = 0; y < _target.Height; y++)
                {
                    Color pixColour = Color.FromArgb((int)(255 * rCol), Math.Min(255, (int)(x * gCol)), Math.Min(255, (int)(y * bCol)));
                    _target.SetPixel(x, y, pixColour);
                }
            }
 
        }

The second actually writes the text to the image. It calculates a font size to fit the given string onto the texture.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
        //Fit and write text to a bitmap
        static void WriteText(string _text, Bitmap _target)
        {
            //calculate the font size to fit the text to the image
            //pixel width in monospaced Courier New = 0.833 point size
            const float fontReduce = 0.833f;
            const float edgePadding = 20;
            float size = ((_target.Width - edgePadding) / _text.Length) / fontReduce;
 
            //set the max font size to 25
            size = Math.Min(size, 35);
 
            //set up font and graphics
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            Font font = new Font("Courier New", size);
            Graphics graphics = Graphics.FromImage(_target);
 
            graphics.DrawString(_text, font, Brushes.Black, _target.Width / 2, _target.Height / 3, format);
 
        }

I’ll be honest, the second solution (with Imagemagick) is simpler, more flexible, and creates better looking textures.
As an example:
convert -size 256x256 plasma:tomato-steelblue -font Arial -pointsize 20 -draw "text 10,20 Default_Texture" TextureToCreate.jpg
Produces the following image:

Creating a batch/shell script to calculate the correct font size and run this command should be trivial, leading to some disgusting, but useful, textures.



Related Posts


Comments

  1. Karin says:

    That is indeed garishly hideous, or maybe hideously garish. I think I had a skirt like that in the 70s.

  2. Hazel says:

    Thanks, that’s exactly what I was going for.

Leave a Reply