index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. const fs = require('fs');
  2. const path = require('path');
  3. const { createCanvas, loadImage } = require('canvas');
  4. // 配置
  5. const CONFIG = {
  6. pixelSize: 100, // 马赛克块大小
  7. inputImage: path.join(__dirname, '参考图.png'),
  8. outputMosaic: path.join(__dirname, 'mosaic.png'),
  9. outputReport: path.join(__dirname, 'report.html')
  10. };
  11. // 主函数
  12. async function main() {
  13. try {
  14. console.log('开始处理图片...');
  15. // 读取原始图片
  16. const image = await loadImage(CONFIG.inputImage);
  17. const width = image.width;
  18. const height = image.height;
  19. // 创建马赛克图片
  20. console.log('创建马赛克图片...');
  21. const { mosaicCanvas, colorData } = createMosaic(image, width, height, CONFIG.pixelSize);
  22. // 保存马赛克图片
  23. const mosaicBuffer = mosaicCanvas.toBuffer('image/png');
  24. fs.writeFileSync(CONFIG.outputMosaic, mosaicBuffer);
  25. // 生成报告
  26. console.log('生成HTML报告...');
  27. generateReport(colorData);
  28. console.log('处理完成! 报告已生成: ' + CONFIG.outputReport);
  29. } catch (error) {
  30. console.error('处理过程中出错:', error);
  31. }
  32. }
  33. // 创建马赛克图片并提取颜色
  34. function createMosaic(image, width, height, pixelSize) {
  35. // 创建画布
  36. const canvas = createCanvas(width, height);
  37. const ctx = canvas.getContext('2d');
  38. // 绘制原始图片
  39. ctx.drawImage(image, 0, 0, width, height);
  40. // 颜色映射: 颜色值 -> 出现次数
  41. const colorMap = new Map();
  42. // 遍历图像,按块处理
  43. for (let y = 0; y < height; y += pixelSize) {
  44. for (let x = 0; x < width; x += pixelSize) {
  45. // 计算当前块的大小
  46. const blockWidth = Math.min(pixelSize, width - x);
  47. const blockHeight = Math.min(pixelSize, height - y);
  48. // 获取块的像素数据
  49. const imageData = ctx.getImageData(x, y, blockWidth, blockHeight);
  50. const data = imageData.data;
  51. // 计算平均颜色
  52. let r = 0, g = 0, b = 0;
  53. const pixelCount = blockWidth * blockHeight;
  54. for (let i = 0; i < data.length; i += 4) {
  55. r += data[i];
  56. g += data[i + 1];
  57. b += data[i + 2];
  58. }
  59. r = Math.floor(r / pixelCount);
  60. g = Math.floor(g / pixelCount);
  61. b = Math.floor(b / pixelCount);
  62. // 创建颜色键
  63. const colorKey = `rgb(${r},${g},${b})`;
  64. // 更新颜色映射
  65. if (colorMap.has(colorKey)) {
  66. colorMap.set(colorKey, colorMap.get(colorKey) + 1);
  67. } else {
  68. colorMap.set(colorKey, 1);
  69. }
  70. // 填充块
  71. ctx.fillStyle = colorKey;
  72. ctx.fillRect(x, y, blockWidth, blockHeight);
  73. }
  74. }
  75. // 转换为数组并排序
  76. const sortedColors = Array.from(colorMap.entries())
  77. .map(([color, count]) => ({ color, count }))
  78. .sort((a, b) => b.count - a.count);
  79. return { mosaicCanvas: canvas, colorData: sortedColors };
  80. }
  81. // 生成HTML报告
  82. function generateReport(colorData) {
  83. // 创建HTML内容
  84. const htmlContent = `
  85. <!DOCTYPE html>
  86. <html lang="zh-CN">
  87. <head>
  88. <meta charset="UTF-8">
  89. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  90. <title>图片颜色分析报告</title>
  91. <style>
  92. body {
  93. font-family: 'Arial', sans-serif;
  94. line-height: 1.6;
  95. color: #333;
  96. max-width: 1200px;
  97. margin: 0 auto;
  98. padding: 20px;
  99. }
  100. h1, h2 {
  101. color: #2c3e50;
  102. }
  103. .image-container {
  104. display: flex;
  105. justify-content: space-between;
  106. margin-bottom: 30px;
  107. flex-wrap: wrap;
  108. }
  109. .image-box {
  110. width: 48%;
  111. margin-bottom: 20px;
  112. }
  113. .image-box img {
  114. max-width: 100%;
  115. height: auto;
  116. border: 1px solid #ddd;
  117. }
  118. .color-cards {
  119. display: flex;
  120. flex-wrap: wrap;
  121. gap: 10px;
  122. margin-top: 20px;
  123. }
  124. .color-card {
  125. width: 100px;
  126. height: 100px;
  127. display: flex;
  128. flex-direction: column;
  129. align-items: center;
  130. justify-content: center;
  131. border-radius: 5px;
  132. color: white;
  133. text-shadow: 0 0 2px #000;
  134. font-size: 12px;
  135. position: relative;
  136. }
  137. .color-info {
  138. background: rgba(0,0,0,0.7);
  139. padding: 3px 6px;
  140. border-radius: 3px;
  141. }
  142. .color-distribution {
  143. margin-top: 30px;
  144. }
  145. .color-bar {
  146. height: 30px;
  147. margin: 5px 0;
  148. display: flex;
  149. }
  150. .color-bar-segment {
  151. height: 100%;
  152. display: flex;
  153. align-items: center;
  154. justify-content: center;
  155. color: white;
  156. text-shadow: 0 0 2px #000;
  157. font-size: 12px;
  158. }
  159. </style>
  160. </head>
  161. <body>
  162. <h1>图片颜色分析报告</h1>
  163. <div class="image-container">
  164. <div class="image-box">
  165. <h2>原始图片</h2>
  166. <img src="参考图.png" alt="原始图片">
  167. </div>
  168. <div class="image-box">
  169. <h2>马赛克图片</h2>
  170. <img src="mosaic.png" alt="马赛克图片">
  171. </div>
  172. </div>
  173. <h2>颜色分布</h2>
  174. <p>从马赛克图中提取的主要颜色(按出现频率排序):</p>
  175. <div class="color-cards">
  176. ${colorData.slice(0, 20).map((item, index) => `
  177. <div class="color-card" style="background-color: ${item.color}">
  178. <div class="color-info">
  179. ${item.color}<br>
  180. 频率: ${((item.count / colorData.reduce((sum, c) => sum + c.count, 0)) * 100).toFixed(1)}%
  181. </div>
  182. </div>
  183. `).join('')}
  184. </div>
  185. <div class="color-distribution">
  186. <h2>颜色频率分布图</h2>
  187. <div class="color-bar">
  188. ${colorData.slice(0, 10).map(item => {
  189. const percentage = (item.count / colorData.reduce((sum, c) => sum + c.count, 0)) * 100;
  190. return `<div class="color-bar-segment" style="background-color: ${item.color}; width: ${percentage}%;">${percentage.toFixed(1)}%</div>`;
  191. }).join('')}
  192. </div>
  193. </div>
  194. </body>
  195. </html>
  196. `;
  197. // 写入HTML文件
  198. fs.writeFileSync(CONFIG.outputReport, htmlContent);
  199. }
  200. // 运行主函数
  201. main();