佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 956|回复: 8

图片转换去字符图案(C#)

[复制链接]
发表于 8-3-2021 11:23 PM | 显示全部楼层 |阅读模式
不知道有谁想试一试?

cmp.PNG

  1. using System;
  2. using System.Drawing;
  3. using System.IO;

  4. namespace BMP2ASCII
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             char[] ascii = new char[] { '#', '#', '*', '*', '+', '+', '+', ';', ';', ':', ':', ',', ',', '.', '.', ' ', ' ' };
  11.             Bitmap bmp = new Bitmap(@"C:\Users\ASUS\Pictures\horse.jpg");
  12.             string txt = null;

  13.             for (int y = 0; y < bmp.Height; y+=6)
  14.             {
  15.                 for (int x = 0; x < bmp.Width; x+=3)
  16.                 {
  17.                     uint rgb = ((uint)bmp.GetPixel(x, y).ToArgb() & 0xFFFFFF) / 1048576;
  18.                     //Console.Write(ascii[rgb]);
  19.                     txt += ascii[rgb];
  20.                 }
  21.                 //Console.WriteLine();
  22.                 txt += "\r\n";
  23.             }

  24.             File.WriteAllText(@"C:\Users\ASUS\Desktop\horse.txt", txt);
  25.             Console.ReadLine();
  26.         }
  27.     }
  28. }
复制代码


回复

使用道具 举报


ADVERTISEMENT

发表于 9-3-2021 12:14 PM | 显示全部楼层
測試了。有趣的想法。

嘗試解說其中的做法:

  1. uint rgb = ((uint)bmp.GetPixel(x, y).ToArgb() & 0xFFFFFF) / 1048576;

  2. 1. 從圖畫上拿一個點,然後轉換去 argb。
  3. 2. 通過 & 0xFFFFFF 把 "Alpha" 去除。
  4. 3. 之後 / 1048576 (或者 right shift 20 bit),得到 0 ~ 15
  5. 4. 在從 char[] ascii 取出對應的字符。

  6. * char[] ascii 有 16 個 (0 ~ 15)多了一個,最後一個 char 是使用不到的,可以拿走。

  7.             char[] ascii = new char[] {
  8.                 '#', '#', '*', '*', '+', '+', '+', ';',
  9.                 ';', ':', ':', ',', ',', '.', '.', ' ' };

复制代码


這個做法會把點上的 紅色強度 -> 轉換成不同的字符。
可以嘗試把顏色轉換成 灰度 Grayscale 再轉換成字符。



回复

使用道具 举报

 楼主| 发表于 9-3-2021 03:25 PM 来自手机 | 显示全部楼层
flashang 发表于 9-3-2021 12:14 PM
測試了。有趣的想法。

嘗試解說其中的做法:


這個做法會把點上的 紅色強度 -> 轉換成不同的字符。
可以嘗試把顏色轉換成 灰度 Grayscale 再轉換成字符。

你果然是高手喔,我自己也不知道原来是取红色强度。
回复

使用道具 举报

发表于 9-3-2021 09:16 PM | 显示全部楼层
褐眼睛 发表于 9-3-2021 03:25 PM
你果然是高手喔,我自己也不知道原来是取红色强度。





是使用了這樣的圖,然後發現了紅色不正常。。。






RGB.PNG

点评

原来如此!  发表于 9-3-2021 09:59 PM
回复

使用道具 举报

 楼主| 发表于 10-3-2021 01:22 AM | 显示全部楼层

兄弟,再试一试这个,有16色的图案:

cmp1.PNG

cmp2.PNG

  1. using System;
  2. using System.Drawing;

  3. namespace BMP2ANSI
  4. {
  5.     class Program
  6.     {
  7.         static int SimplifyColorComponent(int Value)
  8.         {
  9.             if (Value >= 52)
  10.                 return 63;
  11.             else if (Value >= 32)
  12.                 return 42;
  13.             else if (Value >= 12)
  14.                 return 21;

  15.             return 0;
  16.         }

  17.         static byte DecreaseColor256(byte Red, byte Green, byte Blue)
  18.         {
  19.             byte[,] Palette16 = new byte[16, 3] { { 0, 0, 0 }, { 0, 0, 42 }, { 0, 42, 0 }, { 0, 42, 42 }, { 42, 0, 0 }, { 42, 0, 42 }, { 42, 42, 0 }, { 42, 42, 42 }, { 0, 0, 21 }, { 0, 0, 63 }, { 0, 42, 21 }, { 0, 42, 63 }, { 42, 0, 21 }, { 42, 0, 63 }, { 42, 42, 21 }, { 42, 42, 63 } };

  20.             byte Color;
  21.             byte Component;
  22.             byte Value = 0;
  23.             byte NewRed = 0;
  24.             byte NewGreen = 0;
  25.             byte NewBlue = 0;

  26.             for (Component = 0; Component <= 2; Component++)
  27.             {
  28.                 if (Component == 0)
  29.                     Value = (byte)SimplifyColorComponent(Red / 4);
  30.                 else if (Component == 1)
  31.                     Value = (byte)SimplifyColorComponent(Green / 4);
  32.                 else if (Component == 2)
  33.                     Value = (byte)SimplifyColorComponent(Blue / 4);

  34.                 Color = 0;

  35.                 while (Value != Palette16[Color, Component])
  36.                 {
  37.                     Color++;

  38.                     if (Color > 15)
  39.                     {
  40.                         Value -= 21;
  41.                         Color = 0;
  42.                     }
  43.                 }

  44.                 if (Component == 0)
  45.                     NewRed = Value;
  46.                 else if (Component == 1)
  47.                     NewGreen = Value;
  48.                 else if (Component == 2)
  49.                     NewBlue = Value;

  50.             }

  51.             for (Color = 0; Color <= 15; Color++)
  52.                 if ((Palette16[Color, 0] == NewRed) && (Palette16[Color, 1] == NewGreen) && (Palette16[Color, 2] == NewBlue))
  53.                     return Color;

  54.             return 0;
  55.         }
  56.         static void Main(string[] args)
  57.         {
  58.             Bitmap bmp = new Bitmap(@"C:\Users\ASUS\Pictures\flower.png");

  59.             for (int y = 0; y < bmp.Height; y += 28)
  60.             {
  61.                 for (int x = 0; x < bmp.Width; x += 14)
  62.                 {
  63.                     Color rgb = bmp.GetPixel(x, y);
  64.                     Console.BackgroundColor = (ConsoleColor)DecreaseColor256(rgb.R, rgb.G, rgb.B);
  65.                     Console.Write(' ');
  66.                 }
  67.                 Console.BackgroundColor = ConsoleColor.Black;
  68.                 Console.WriteLine();
  69.             }
  70.             Console.ReadLine();
  71.         }
  72.     }
  73. }
复制代码
回复

使用道具 举报

 楼主| 发表于 10-3-2021 01:24 AM | 显示全部楼层
DecreaseColor256() 是22年前写的,当时还没有C#,用的是Pascal.

去年将它换成C#,方便我处理图像转换。
回复

使用道具 举报

Follow Us
发表于 10-3-2021 01:42 PM | 显示全部楼层
褐眼睛 发表于 10-3-2021 01:22 AM
兄弟,再试一试这个,有16色的图案:

測試了,把顏色轉去 text mode 16 colour palette 比較多限制,只能選那些比較 “明顯” 的顏色。


把顏色轉換是為了方便在其他機器上執行。
現在新的器材 (手機,遊戲機) 都沒有顏色的限制了。
如果是做一些 arduino project 這個就會使用到。


參考:

https://wiki.osdev.org/Text_UI

https://www.davideaversa.it/blog/how-choose-fantasy-console/






回复

使用道具 举报

 楼主| 发表于 10-3-2021 10:10 PM | 显示全部楼层
flashang 发表于 10-3-2021 01:42 PM
測試了,把顏色轉去 text mode 16 colour palette 比較多限制,只能選那些比較 “明顯” 的顏色。


把顏色轉換是為了方便在其他機器上執行。
現在新的器材 (手機,遊戲機) 都沒有顏色的限制了。
如果是做 ...

怎么都给你说中了。
回复

使用道具 举报


ADVERTISEMENT

发表于 11-3-2021 09:13 AM | 显示全部楼层
本帖最后由 flashang 于 11-3-2021 09:51 AM 编辑
褐眼睛 发表于 10-3-2021 10:10 PM
怎么都给你说中了。

以前有玩遊戲機,也有玩 emulator,也有做過一些小遊戲來玩。






回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 20-4-2024 10:10 PM , Processed in 0.065564 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表