当前位置: 首页 > news >正文

processing像素画教程

前提:各位已经安装了processing

第一步:创建一个简单的网格

我们首先创建一个网格来定义我们作品的像素画布。网格将帮助您在适当的位置绘制每个像素。

int gridSize = 20; // 每个像素的大小
int cols, rows;
void setup() {size(400, 400); // 设置画布大小cols = width / gridSize;rows = height / gridSize;noLoop(); // 不需要在draw中不断重复
}void draw() {for (int i = 0; i < cols; i++) {for (int j = 0; j < rows; j++) {stroke(200); // 网格线颜色noFill();rect(i * gridSize, j * gridSize, gridSize, gridSize);}}
}

这个width是内置的变量,height也是内置变量,一般和你 size(400, 400);就是上面你设置的400,400

cols = width / gridSize; 代表的是多少列个方格,rows = height / gridSize; 代表的是多少行个方格

咱们使用 rect()函数,咱们可以查看官网,官网是这样介绍的:

就是绘制矩形而已

运行展示:

第二步:绘制像素

使用fill()和rect()来填充网格中的特定方块,这就是“绘制”一个像素。

void draw() {for (int i = 0; i < cols; i++) {for (int j = 0; j < rows; j++) {stroke(200);noFill();rect(i * gridSize, j * gridSize, gridSize, gridSize);// 示例:绘制一个红色色块if (i == 5 && j == 5) {fill(255, 0, 0); // 红色rect(i * gridSize, j * gridSize, gridSize, gridSize);}}}
}

fill函数是填充颜色的意思,它在 rect()函数的前面,就意味着填充这个单独的rect方块

运行展示:

完整代码:

int gridSize = 20; // 每个像素的大小
int cols, rows;
void setup() {size(400, 400); // 设置画布大小cols = width / gridSize;rows = height / gridSize;noLoop(); // 不需要在draw中不断重复
}void draw() {for (int i = 0; i < cols; i++) {for (int j = 0; j < rows; j++) {stroke(200);noFill();rect(i * gridSize, j * gridSize, gridSize, gridSize);// 示例:绘制一个红色色块if (i == 5 && j == 5) {fill(255, 0, 0); // 红色rect(i * gridSize, j * gridSize, gridSize, gridSize);}}}
}

第三步:将图片像素化

咱们了解以上基本内容之后,就可以实现将图片像素化的过程

完整演示代码如下:

PImage img;       // 存储加载的图像
int blockSize = 10; // 每个像素块的大小void setup() {size(400, 400);img = loadImage("C:\\Users\\Administrator\\Desktop\\t1.png"); // 替换为您图像的文件名img.resize(width, height); // 调整图像大小以适应窗口
}void draw() {pixelateImage();
}void pixelateImage() {for (int x = 0; x < img.width; x += blockSize) {for (int y = 0; y < img.height; y += blockSize) {color avgColor = averageColor(x, y);fill(avgColor);noStroke();rect(x, y, blockSize, blockSize);}}
}// 计算给定块的平均颜色
color averageColor(int startX, int startY) {int r = 0, g = 0, b = 0;int count = 0;for (int dx = 0; dx < blockSize; dx++) {for (int dy = 0; dy < blockSize; dy++) {int currentX = startX + dx;int currentY = startY + dy;// 检查是否在图像边界内if (currentX < img.width && currentY < img.height) {color currentColor = img.get(currentX, currentY);r += red(currentColor);g += green(currentColor);b += blue(currentColor);count++;}}}// 返回平均颜色return color(r / count, g / count, b / count);
}

效果展示图:

首先,咱们需要一个存储加载的图像,就是上文的PImage img;

第二,设置每个像素块的大小,int blockSize = 10;

第三, 给每个像素块上色,这里上色用的是 给定块的平均颜色 (这里说一下,noStroke();是不设置网格线颜色)

其算法我单独拿下来给各位讲解:

// 计算给定块的平均颜色
color averageColor(int startX, int startY) {int r = 0, g = 0, b = 0;int count = 0;for (int dx = 0; dx < blockSize; dx++) {for (int dy = 0; dy < blockSize; dy++) {int currentX = startX + dx;int currentY = startY + dy;// 检查是否在图像边界内if (currentX < img.width && currentY < img.height) {color currentColor = img.get(currentX, currentY);r += red(currentColor);g += green(currentColor);b += blue(currentColor);count++;}}}// 返回平均颜色return color(r / count, g / count, b / count);
}

众所周知,颜色是由rgb决定的,这个算法很简单img.get是获取图片每一处的颜色的,r/count意味着这个像素块里面的红色除以这个像素块每个像素点,得到的就是像素的平均颜色,同理如下,count++是计算每个像素块的像素点的,按照我说的这个count应该为100.咱们可以测试一下:在返回平均颜色的上面加入打印count

咱们可以看到就是100,因为一个像素块长10像素点,宽10像素点,10*10可不就100.

第四步:修改图片像素化的模糊程度

咱们经过上面的学习,可以明白,如果想要图片像素更加清晰,那么咱们可以直接将像素块调小一点,这样计算平均值填充的颜色将会更加清晰。以下是将像素块调成5之后的效果

当然,咱们调成1,那就是原画了

后续教学我会继续在以下项目的processing版本进行更新nanshaws/LibgdxTutorial: libgdx 教程项目 本项目旨在提供完整的libgdx桌面教程,帮助开发者快速掌握libgdx游戏开发框架的使用。成功的将gdx-ai和ashley的tests从官网剥离出来,并成功运行。libgdx tutorial project This project aims to provide a complete libgdx desktop tutorial to help developers quickly master the use of libgdx game development framework. Successfully separated GDX-AI and Ashley's tests from the official website and ran them (github.com)

http://www.lryc.cn/news/460080.html

相关文章:

  • 【秋招笔试】10.13字节跳动(已改编)秋招-三语言题解
  • 牛客网上最全的Java八股文整理,涵盖Java全栈技术点
  • Skyeye 云智能制造 v3.14.9 发布,ERP 商城 + AI
  • Element-快速入门
  • 利士策分享,从“亮剑精神”汲取财富智慧
  • 【JavaScript】关于使用JS对word文档实现预览的一些思考
  • 安宝特方案 | AR技术在轨交行业的应用优势
  • K8S配置MySQL主从自动水平扩展
  • Excel:将一列拆分成多列
  • 一款强大灵活的流程图引擎,支持React 和 Svelte 框架
  • 基于STM32 ARM+FPGA+AD的电能质量分析仪方案设计(一)硬件设计
  • LLM - 配置 ModelScope SWIFT 测试 Qwen2-VL 视频微调(LoRA) 教程(3)
  • jmeter实现SSL双向验证
  • 数据结构 ——— 单链表oj题:相交链表(链表的共节点)
  • 【WKWebview】WKWebView Cookie 同步
  • vue-router拦截器
  • SpringBoot驱动的人事管理系统:高效办公新选择
  • 大数据干了什么?
  • android studio可用下载地址
  • HTTP 协议详解
  • 【力扣 | SQL题 | 每日四题】力扣534, 574, 2314, 2298
  • Gitxray:一款基于GitHub REST API的网络安全工具
  • Chrome(谷歌)浏览器 数据JSON格式美化 2024显示插件安装和使用
  • 关于相机的一些零碎知识点
  • 看不懂来打我!让性能提升56%的Vue3.5响应式重构
  • Halcon 极坐标变换
  • JavaScript进阶--深入面向对象
  • Python列表专题:list与in
  • 利用Microsoft Entra Application Proxy在无公网IP条件下安全访问内网计算机
  • 【IEEE独立出版 | 厦门大学主办】第四届人工智能、机器人和通信国际会议(ICAIRC 2024)