图片转字符画

一个有趣的程序。

主要技术包含图片拖拽、粘贴、双击上传,然后通过canvas获得图片每一像素的rgb颜色值,然后按颜色值总体大小使用相应的字符代替形成字符画。

原图:

 

 

 转换后:

 

 

代码:

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>image2char</title>
    <style>
    .imageUploader {}
    .imageUploader .eventContainerDiv {width:100%;height:100px;min-width:300px;min-height:100px;border:3px dashed silver;padding:4px;cursor:default;}
    .imageUploader .eventContainerDiv img {display:none;overflow-x:auto;}
    .imageUploader .imageContainerDiv .imageBox {width:200px;height:150px;float:left;margin:4px;border:1px solid #eee;}
    .imageUploader .imageContainerDiv .imageBox img {max-width:100%;max-height:100%;object-fit: cover;}
    .imageUploader .imageContainerDiv .imageBox button {height:28px;margin-top:-28px;position: absolute;}
    .pixel-container {margin:0;padding:0;}
    .pixel-row {display:flex;}
    .pixel {width:12px;height:12px;margin:0;padding:0;font-size:12px;font-family:宋体;}
    </style>
</head>
<body>
 
<input type="hidden" id="article_link_image" name="article_link_image" value=""/>
<div id="imageUploader" class="imageUploader" upload="" maxWidth="400" maxHeight="600" limitCount="1"></div>
<div id="imageShow"></div>
</body>
 
<script>
 
 
    // 拖拽上传,兼容IE、FireFox、Chrome
    function dragEventProcess(eventContainerDiv,upload){
        document.addEventListener("dragenter", function(e){
            eventContainerDiv.style.borderColor = 'gray';
        }, false);
        document.addEventListener("dragleave", function(e){
            eventContainerDiv.style.borderColor = 'silver';
        }, false);
        eventContainerDiv.addEventListener("dragenter", function(e){
            eventContainerDiv.style.borderColor = 'gray';
            eventContainerDiv.style.backgroundColor = 'white';
        }, false);
        eventContainerDiv.addEventListener("dragleave", function(e){
            eventContainerDiv.style.backgroundColor = 'transparent';
        }, false);
        eventContainerDiv.addEventListener("dragenter", function(e){
            e.stopPropagation();
            e.preventDefault();
        }, false);
        eventContainerDiv.addEventListener("dragover", function(e){
            e.stopPropagation();
            e.preventDefault();
        }, false);
        eventContainerDiv.addEventListener("drop", function(e){
            e.stopPropagation();
            e.preventDefault();
            var imageFile = e.dataTransfer.files[0];
            loadImage(imageFile);
            //submit.disabled = false;
        }, false);
    }
 
 
    // 粘贴上传,兼容IE、FireFox、Chrome
    function pasteEventProcess(eventContainerDiv,upload){
        eventContainerDiv.addEventListener("paste", function(event){
            // console.log(event);
            // console.log(clipboardData);
            // chrome、edge、firefox,不管粘贴截图还是网络图片都能从clipboardData中得到图片二进制数据。
            if ( event.clipboardData || event.originalEvent ) {
                var clipboardData = (event.clipboardData || event.originalEvent.clipboardData);
                if ( clipboardData.items ) {
                    var  items = clipboardData.items;
                    for (var i = 0; i < items.length; i++) {
                        // console.log(items[i].type);
                        if (items[i].type.indexOf("image") !== -1) {
                            var imageFile = items[i].getAsFile();
                            loadImage(imageFile);
                        }
                    }
 
                }
            }else {
                alert('the browser is not support.');
            }
        }, false);
    }
 
 
    // 双击上传
    function dblclickEventProcess(eventContainerDiv,upload){
        eventContainerDiv.addEventListener("dblclick", function(event){
            // 触发上一个元素(input file)的点击事件
            eventContainerDiv.previousElementSibling.click();
        });
    }
 
 
    function clearEventContainerDivImages(eventContainerDiv){
        eventContainerDiv.setAttribute("count", "0");
        var images = eventContainerDiv.querySelectorAll("img");
        for(var i=0;i<images.length;i++){
            eventContainerDiv.removeChild(images[i]);
        }
    }
 
    // 初始化函数
    function initialize(){
        var imageUploaders = document.querySelectorAll(".imageUploader");
        if(imageUploaders && imageUploaders.length > 0){
 
            for(var i=0;i<imageUploaders.length;i++){
                var uploader = imageUploaders[i];
                // 如果已经初始化过则跳过。
                if(uploader.getAttribute("initialized") == "true") continue;
                 
                var upload = uploader.getAttribute("upload");
                var eventContainerDiv = document.createElement("div");
                // 往div里创建一个input file标签,用于双击选择文件上传
                var input = document.createElement("input");
                input.type = "file";
                input.style.display = "none";
                input.onchange = function(event){
                    var evt = window.event || event;
                    var evtsrc = evt.srcElement || evt.target;
                    //console.log(evtsrc.parentNode.id);
                    var imageFile = evtsrc.files[0];
                    loadImage(imageFile);
                };
                uploader.appendChild(input);
 
                // 往div里创建两个div,分别作为拖放粘贴双击容器和图片上传后的呈现容器
                eventContainerDiv.className = "eventContainerDiv";
                eventContainerDiv.contentEditable = true;
                eventContainerDiv.innerHTML = "请将图片粘贴或拖拽到此区域,或双击此区域选择图片进行上传.";
 
 
                // 绑定拖拽事件及处理
                dragEventProcess(eventContainerDiv,upload);
                // 绑定粘贴事件及处理
                pasteEventProcess(eventContainerDiv,upload);
                // 绑定双击事件及处理
                dblclickEventProcess(eventContainerDiv,upload);
                // 添加到容器里
                uploader.appendChild(eventContainerDiv);
                var imageContainerDiv = document.createElement("div");
                imageContainerDiv.className = "imageContainerDiv";
 
                // 添加到容器里
                uploader.appendChild(imageContainerDiv);
 
 
 
                uploader.setAttribute("initialized", "true");
 
            }
        }
    }
 
 
    window.addEventListener("load",function(){
        // 在window下设置一个初始化函数,用于可动态地初始化组件(用js动态创建的组件,需要动态初始化)。
        window.powerfulImageUploadInitialize = initialize;
        powerfulImageUploadInitialize();
    },false);
 
 
  function group(array, subGroupLength) {
      let index = 0;
      let newArray = [];
      while(index < array.length) {
          newArray.push(array.slice(index, index += subGroupLength));
      }
      return newArray;
  }
   
    function loadImage(imageFile) {
        var canvas = document.createElement('canvas');
        var context = canvas.getContext('2d');
        var reader = new FileReader();
        reader.readAsDataURL(imageFile);
        reader.onload = function(e){ // reader onload start
          var image = new Image();
          image.src = e.target.result;
          image.onload = function(){ // image onload start
            var img_width = this.width;
            var img_height = this.height;
            // 设置画布尺寸
            canvas.width = img_width;
            canvas.height = img_height;
            // 将图片按像素写入画布
            context.drawImage(this, 0, 0, img_width, img_height);
            // 读取图片像素信息
            var imageData = context.getImageData(0, 0, img_width, img_height);
            var colorRows = group(imageData.data, img_width * 4); // 每4个元素表示一个像素的rgba。
            imageRGB2Char(colorRows);
          }
          
        }
    }
     
    function imageRGB2Char(colorRows) {
        var html = '';
        console.log('colorRows.length:', colorRows.length);
        for (var x=0; x<colorRows.length; x++) {
            html += '<div class="pixel-row">';
            var cells = colorRows[x];
            console.log('cells.length', cells.length);
            for (var y=0; y<cells.length; y++) {
                if(y%4 === 0){ // 每四个元素为一个像素数据 r,g,b,alpha
                    // 这里只取rgb,不取a
                    var rgb = cells[y] + ',' + cells[y+1] + ',' + cells[y+2];
                    // html += '<div class="pixel" style="background-color: rgb('+ rgb2char(rgb) +');"></div>';
                    html += '<div class="pixel" style="color: rgb('+ rgb +');">' + rgb2char(rgb) + '</div>';
                }
            }
            html += '</div>';
        }
        document.getElementById('imageShow').innerHTML = html;
    }
     
    function rgb2char(rgb) {
        rgb = eval(rgb.replaceAll(',', '+'));
        var charr = '';
        if (rgb >= 722) {
            charr = "  ";
        } else if (rgb >= 684 && rgb < 722) {
            charr = ";;";
        } else if (rgb >= 646 && rgb < 684) {
            charr = "\'\'";
        } else if (rgb >= 608 && rgb < 646) {
            charr = "..";
        } else if (rgb >= 570 && rgb < 608) {
            charr = ",,";
        } else if (rgb >= 532 && rgb < 570) {
            charr = "::";
        } else if (rgb >= 494 && rgb < 532) {
            charr = "tt";
        } else if (rgb >= 456 && rgb < 494) {
            charr = "rr";
        } else if (rgb >= 418 && rgb < 456) {
            charr = "ff";
        } else if (rgb >= 380 && rgb < 418) {
            charr = "kk";
        } else if (rgb >= 342 && rgb < 380) {
            charr = "ZZ";
        } else if (rgb >= 304 && rgb < 342) {
            charr = "FF";
        } else if (rgb >= 266 && rgb < 304) {
            charr = "XX";
        } else if (rgb >= 228 && rgb < 266) {
            charr = "##";
        } else if (rgb >= 190 && rgb < 228) {
            charr = "BB";
        } else if (rgb >= 152 && rgb < 190) {
            charr = "HH";
        } else if (rgb >= 114 && rgb < 152) {
            charr = "WW";
        } else if (rgb >= 76 && rgb < 114) {
            charr = "NN";
        } else if (rgb >= 38 && rgb < 76) {
            charr = "@@";
        } else if (rgb >= 0 && rgb < 38) {
            charr = "MM";
        }
        return charr;
    }
     
</script>
</html>

  

 

posted @   jsper  阅读(671)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示