report.html 7.6 KB

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