go 语言图片像素点处理
将一张图片色彩反转,就是将 rgb 值,分别被 255 减
package main import (
"bytes"
"fmt"
"image"
"image/color"
"image/gif"
"image/jpeg"
"image/png"
"io/ioutil"
"os"
"strings"
) func main(){
source := "./image/b.png" //输入图片
target := "./image/result.png" //输出图片 ff, _ := ioutil.ReadFile(source) //读取文件
bbb := bytes.NewBuffer(ff)
m, _, _ := image.Decode(bbb)
bounds := m.Bounds()
dx := bounds.Dx()
dy := bounds.Dy() newRgba := image.NewRGBA(bounds) //new 一个新的图片
for i := 0; i < dx; i++ {
for j := 0; j < dy; j++ {
colorRgb := m.At(i, j)
r, g, b, a := colorRgb.RGBA()
r_uint8 := uint8(r >> 8) //转换为 255 值
g_uint8 := uint8(g >> 8)
b_uint8 := uint8(b >> 8)
a_uint8 := uint8(a >> 8) r_uint8 = 255 - r_uint8
g_uint8 = 255 - g_uint8
b_uint8 = 255 - b_uint8
newRgba.SetRGBA(i, j, color.RGBA{r_uint8, g_uint8, b_uint8, a_uint8}) //设置像素点
}
} f, _ := os.Create(target)
defer f.Close()
encode(source, f, newRgba) } //图片编码 写入
func encode(inputName string, file *os.File, rgba *image.RGBA) {
if strings.HasSuffix(inputName, "jpg") || strings.HasSuffix(inputName, "jpeg") {
jpeg.Encode(file, rgba, nil)
} else if strings.HasSuffix(inputName, "png") {
png.Encode(file, rgba)
} else if strings.HasSuffix(inputName, "gif") {
gif.Encode(file, rgba, nil)
} else {
fmt.Errorf("不支持的图片格式")
}
}
效果:


其他的一些常见算法如下:
//图片灰化处理
func hdImage(m image.Image) *image.RGBA {
bounds := m.Bounds()
dx := bounds.Dx()
dy := bounds.Dy()
newRgba := image.NewRGBA(bounds)
for i := 0; i < dx; i++ {
for j := 0; j < dy; j++ {
colorRgb := m.At(i, j)
_, g, _, a := colorRgb.RGBA()
g_uint8 := uint8(g >> 8)
a_uint8 := uint8(a >> 8)
newRgba.SetRGBA(i, j, color.RGBA{g_uint8, g_uint8, g_uint8, a_uint8})
}
}
return newRgba
} //图片缩放, add at 2018-9-12
func rectImage(m image.Image, newdx int) *image.RGBA {
bounds := m.Bounds()
dx := bounds.Dx()
dy := bounds.Dy()
newRgba := image.NewRGBA(image.Rect(0, 0, newdx, newdx*dy/dx))
graphics.Scale(newRgba, m)
return newRgba
} //图片转为字符画(简易版)
func ascllimage(m image.Image, target string) {
if m.Bounds().Dx() > 300 {
m = rectImage(m, 300)
}
bounds := m.Bounds()
dx := bounds.Dx()
dy := bounds.Dy()
arr := []string{"M", "N", "H", "Q", "$", "O", "C", "?", "7", ">", "!", ":", "–", ";", "."} fileName := target
dstFile, err := os.Create(fileName)
if err != nil {
fmt.Println(err.Error())
return
}
defer dstFile.Close()
for i := 0; i < dy; i++ {
for j := 0; j < dx; j++ {
colorRgb := m.At(j, i)
_, g, _, _ := colorRgb.RGBA()
avg := uint8(g >> 8)
num := avg / 18
dstFile.WriteString(arr[num])
if j == dx-1 {
dstFile.WriteString("\n")
}
}
}
}
go 语言图片像素点处理的更多相关文章
- Go 语言图片处理简明教程
虽然 Go 语言主要用于 Web 后端以及各类中间件和基础设施开发,也难免遇到一些图像处理的需求.Go 语言提供的 image 标准库提供了基本的图片加载.裁剪.绘制等能力,可以帮助我们实现一些绘图需 ...
- 在c#中用指针操作图片像素点
在Bitmap类中有两个函数SetPixel,GetPixel,分别用来设置或读取图片中指定点的颜色(这里发现了VS的一个错误,SetPixel的文档说明写的是“获取颜色”??). 当要对一幅图进行相 ...
- python 使用 PIL 和 matplotlib 获取图片像素点处理之后再写入
python 版本 3.x 首先安装 PIL 由于PIL仅支持到Python 2.7,加上年久失修,于是一群志愿者在PIL的基础上创建了兼容的版本,名字叫Pillow,支持最新Python 3.x,又 ...
- Atitit.java图片图像处理attilax总结 BufferedImage extends java.awt.Image获取图像像素点image.getRGB(i, lineIndex); 图片剪辑/AtiPlatf_cms/src/com/attilax/img/imgx.javacutImage图片处理titit 判断判断一张图片是否包含另一张小图片 atitit 图片去噪算法的原理与
Atitit.java图片图像处理attilax总结 BufferedImage extends java.awt.Image 获取图像像素点 image.getRGB(i, lineIndex); ...
- Atitit.java图片图像处理attilax总结
Atitit.java图片图像处理attilax总结 BufferedImage extends java.awt.Image 获取图像像素点 image.getRGB(i, lineIndex); ...
- IOS APP 国际化 程序内切换语言实现 不重新启动系统(支持项目中stroyboard 、xib 混用。完美解决方案)
上篇 IOS APP 国际化(实现不跟随系统语言,不用重启应用,代码切换stroyboard ,xib ,图片,其他资源 介绍了纯代码刷新 实现程序内切换语言. 但效率底下,也存在一些问题.暂放弃. ...
- Android--ColorMatrix改变图片颜色
前言 本篇博客讲解如何通过改变图片像素点RGB的值的方式,在Android中改变图片的颜色.在最后将以一个简单的Demo来作为演示. 本篇博客的主要内容: ColorMatrix 使用ColorMat ...
- 小tip: 使用CSS将图片转换成模糊(毛玻璃)效果
去年盛夏之时,曾写过“小tip: 使用CSS将图片转换成黑白”一文,本文的模式以及内容其实走得是类似路线.CSS3 → SVG → IE filter → canvas. 前段时间,iOS7不是瓜未熟 ...
- Android应用开发多语言drawable目录
Android程序多语言的支持是就该Value目录不同国家的区分,前面文档有介绍相相应的国家名称Value怎样写 例如以下说下多语言图片的替换也是就该图片目录目录drawable目录例如以下: Bac ...
随机推荐
- python 基于机器学习识别验证码
1.背景 验证码自动识别在模拟登陆上使用的较为广泛,一直有耳闻好多人在使用机器学习来识别验证码,最近因为刚好接触这方面的知识,所以特定研究了一番.发现网上已有很多基于machine learni ...
- C++中STL常用容器的优点和缺点
我们常用到的STL容器有vector.list.deque.map.multimap.set和multiset,它们究竟有何区别,各自的优缺点是什么,为了更好的扬长避短,提高程序性能,在使用之前需要我 ...
- String Formatting in C#
原文地址 http://blog.stevex.net/string-formatting-in-csharp/ When I started working with the .NET framew ...
- maya cmds pymel undoInfo chunk 撤销束
maya cmds pymel undoInfo chunk 撤销束 cmds.undoInfo(openChunk = 1) # your code cmds.undoInfo(closeChunk ...
- Python学习之MySQLdb模块
摘要: MySQLdb模块用于操作mysql数据库.1.安装MySQLdb模块 yum install MySQL-python -y2.操作流程①.导入模块: import MySQLdb②.连接数 ...
- 关于H5页面在iPhoneX适配
1. iPhoneX的介绍 屏幕尺寸 我们熟知的iPhone系列开发尺寸概要如下: △ iPhone各机型的开发尺寸 转化成我们熟知的像素尺寸: △ 每个机型的多维度尺寸 倍图其实就是像素尺寸和开 ...
- [nodemon] clean exit - waiting for changes before restart
出现上述日志信息,程序就不能往下运行了. 原因:node程序在初始化的时候就报错了,仔细debug吧...
- __x__(32)0908第五天__Photoshop的基本操作
1. 设置 Photoshop 的单位为 像素px 2. 标尺 显示与隐藏 Ctrl + r 3. 放大与缩小 Ctrl + 1 放大到100% Ctrl + 0 适应屏幕 Alt + ...
- [LeetCode] Minimum Distance Between BST Nodes 二叉搜索树中结点的最小距离
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
- ES6 promise学习
Promise 是异步编程的一种解决方案,比传统的解决方案——回调函数和事件——更合理和更强大. 1.promise是一构造函数,既然是构造函数,那么我们就可以用 new Promise()得到一个p ...