代码改变世界

go 语言图片像素点处理

2019-04-25 13:38  muamaker  阅读(3438)  评论(0编辑  收藏  举报

将一张图片色彩反转,就是将  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")
            }
        }
    }
}