Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// This tool comes handy when buying beads, like Hama, Nabbi, Ikea Pyssla and so on.
- /// It takes a pixel art image as argument and it outputs how many colors do you need
- /// and how many beads do you need per color.
- /// It does not remove alpha channel or colored background from the image, so if your
- /// background color matches some part of the image subject, you may have to repaint
- /// the background or you have to count them manually.
- ///
- /// Made by Andrea Giorgio "Muu?" Cerioli
- /// http://www.lanoiadimuu.it/
- /// http://www.lanoiadimuu.it/2014/02/counting-plastic-beads/
- ///
- /// Licensed under MIT License
- /// http://opensource.org/licenses/MIT
- /// </summary>
- using System;
- using System.IO;
- using System.Drawing;
- using System.Collections.Generic;
- namespace beadscount
- {
- class MainClass
- {
- public static void Main (string[] args)
- {
- // Exit if no arguments are given
- if (args.Length == 0)
- {
- Console.WriteLine("Usage: \nhamacount.exe file");
- Console.ReadLine();
- return;
- }
- foreach(string file in args)
- {
- try
- {
- Stream bitmapFile = File.Open(file, FileMode.Open);
- Bitmap bitmap = new Bitmap(bitmapFile);
- WriteColors(CountHamas(bitmap), file);
- bitmapFile.Close();
- Console.Clear();
- Console.WriteLine("Done! See " + file + ".txt for colors. \nPress enter to exit.");
- Console.ReadLine();
- }
- catch(Exception ex)
- {
- Console.WriteLine("Unable to read file " + file + "\n" + ex);
- }
- }
- }
- // Parse the img to get pixel info
- public static Dictionary<Color, int> CountHamas(Bitmap bitmap)
- {
- try
- {
- Console.WriteLine("Parsing file, may take a while");
- // Get image basic infos
- int size = bitmap.Size.Width * bitmap.Size.Height;
- int width = bitmap.Size.Width;
- int height = bitmap.Size.Height;
- // Get all the pixels as a list
- List<Color> colorList = new List<Color>();
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- colorList.Add(bitmap.GetPixel(j,i));
- }
- }
- // For each color, write color down and remove them from list
- Dictionary<Color, int> colorCount = new Dictionary<Color, int>();
- int parsedPixel = 0;
- while(colorList.Count > 0)
- {
- Color colorToSeek = colorList[0];
- int count = 0;
- for(int i = colorList.Count-1; i >=0; i--)
- {
- Color colorToCompare = colorList[i];
- if(colorToSeek == colorToCompare)
- {
- colorList.Remove(colorToCompare);
- count++;
- // Debugs progress to console
- parsedPixel++;
- Console.Clear();
- Console.WriteLine("Parsing file, may take a while");
- Console.WriteLine(parsedPixel + " / " + size + " ( " + (parsedPixel * 100 / size) + "% ) " );
- }
- }
- // Store colours and number of occurrences
- colorCount.Add(colorToSeek, count);
- }
- return colorCount;
- }
- catch
- {
- throw new Exception("Parsing error");
- };
- }
- // Prints the output of CountHamas
- public static void WriteColors(Dictionary<Color, int> colors, string fileName)
- {
- try
- {
- StreamWriter file = new StreamWriter(fileName + ".txt", false);
- foreach(KeyValuePair<Color, int> keyValue in colors)
- {
- string line = keyValue.Key + ": \t\t" + keyValue.Value;
- file.WriteLine(line);
- }
- file.Close();
- }
- catch
- {
- Console.WriteLine("Unable to print pixel outputs");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement