• <noscript id="e0iig"><kbd id="e0iig"></kbd></noscript>
  • <td id="e0iig"></td>
  • <option id="e0iig"></option>
  • <noscript id="e0iig"><source id="e0iig"></source></noscript>
  • Go語言學習之image、image/color、image/png、image/jpeg包(the way to go)

    標簽: go語言  image  png  jpeg

    生命不止,繼續 go go go !!!

    golang同樣為我們提供了很多關于圖片處理的標準庫,這里與大家一起學習分享。

    image package

    作用:
    Package image implements a basic 2-D image library.
    image包實現了一個基本的2D圖像庫.

    方法:
    func NewNRGBA

    func NewNRGBA(r Rectangle) *NRGBA

    NewNRGBA returns a new NRGBA image with the given bounds.

    什么是NRGBA?
    NRGBA is an in-memory image whose At method returns color.NRGBA values.

    type NRGBA struct {
            Pix []uint8
            Stride int
            Rect Rectangle
    }

    func (*NRGBA) Set

    func (p *NRGBA) Set(x, y int, c color.Color)

    設定指定位置的color,image/color稍后會介紹。

    func Rect

    func Rect(x0, y0, x1, y1 int) Rectangle

    Rect is shorthand for Rectangle{Pt(x0, y0), Pt(x1, y1)}.

    image/color package

    作用:
    Package color implements a basic color library.

    func (NRGBA) RGBA

    func (c NRGBA) RGBA() (r, g, b, a uint32)

    func DecodeConfig

    func DecodeConfig(r io.Reader) (Config, string, error)

    DecodeConfig decodes the color model and dimensions of an image that has been encoded in a registered format.
    更通俗說就是獲得image的信息。

    image/png package

    作用:
    Package png implements a PNG image decoder and encoder。

    func (*Encoder) Encode

    func (enc *Encoder) Encode(w io.Writer, m image.Image) error

    編碼png文件。

    image/jpeg package

    作用:
    Package jpeg implements a JPEG image decoder and encoder.

    func Encode

    func Encode(w io.Writer, m image.Image, o *Options) error

    編碼JPG文件。

    應用

    生成圖片

    package main
    
    import "image"
    import "image/color"
    import "image/png"
    import "os"
    
    func main() {
        // Create an 100 x 50 image
        img := image.NewRGBA(image.Rect(0, 0, 100, 50))
    
        // Draw a red dot at (2, 3)
        img.Set(2, 3, color.RGBA{255, 0, 0, 255})
    
        // Save to out.png
        f, _ := os.OpenFile("out.png", os.O_WRONLY|os.O_CREATE, 0600)
        defer f.Close()
        png.Encode(f, img)
    }
    

    運行結果,就是生成了一張png文件。

    生成復雜色彩的圖片

    package main
    
    import (
        "fmt"
        "image"
        "image/color"
        "image/png"
        "math"
        "os"
    )
    
    type Circle struct {
        X, Y, R float64
    }
    
    func (c *Circle) Brightness(x, y float64) uint8 {
        var dx, dy float64 = c.X - x, c.Y - y
        d := math.Sqrt(dx*dx+dy*dy) / c.R
        if d > 1 {
            return 0
        } else {
            return 255
        }
    }
    
    func main() {
        var w, h int = 280, 240
        var hw, hh float64 = float64(w / 2), float64(h / 2)
        r := 40.0
        θ := 2 * math.Pi / 3
        cr := &Circle{hw - r*math.Sin(0), hh - r*math.Cos(0), 60}
        cg := &Circle{hw - r*math.Sin(θ), hh - r*math.Cos(θ), 60}
        cb := &Circle{hw - r*math.Sin(-θ), hh - r*math.Cos(-θ), 60}
    
        m := image.NewRGBA(image.Rect(0, 0, w, h))
        for x := 0; x < w; x++ {
            for y := 0; y < h; y++ {
                c := color.RGBA{
                    cr.Brightness(float64(x), float64(y)),
                    cg.Brightness(float64(x), float64(y)),
                    cb.Brightness(float64(x), float64(y)),
                    255,
                }
                m.Set(x, y, c)
            }
        }
    
        f, err := os.OpenFile("rgb.png", os.O_WRONLY|os.O_CREATE, 0600)
        if err != nil {
            fmt.Println(err)
            return
        }
        defer f.Close()
        png.Encode(f, m)
    }

    運行結果:
    這里寫圖片描述

    獲取一張圖片的尺寸

    package main
    
    import (
        "fmt"
        "image"
        _ "image/jpeg"
        _ "image/png"
        "os"
    )
    
    func main() {
        width, height := getImageDimension("rgb.png")
        fmt.Println("Width:", width, "Height:", height)
    }
    
    func getImageDimension(imagePath string) (int, int) {
        file, err := os.Open(imagePath)
        defer file.Close()
        if err != nil {
            fmt.Fprintf(os.Stderr, "%v\n", err)
        }
    
        image, _, err := image.DecodeConfig(file)
        if err != nil {
            fmt.Fprintf(os.Stderr, "%s: %v\n", imagePath, err)
        }
        return image.Width, image.Height
    }
    

    生成縮略圖
    借助github開源項目:
    https://github.com/disintegration/imaging
    文檔地址:
    http://godoc.org/github.com/disintegration/imaging

    這里寫圖片描述

    版權聲明:本文為wangshubo1989原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
    本文鏈接:https://blog.csdn.net/wangshubo1989/article/details/74010989

    智能推薦

    Go語言學習之archive/zip、compress/zlib、compress/gzip包(the way to go)

    生命不止,繼續go go go !!! 曾經的曾經,寫過這篇博客,介紹過使用archive/zip: Go實戰–壓縮zip和解壓縮unzip的應用(The way to go) zip zlib gzip zlib是一種數據壓縮程序庫,它的設計目標是處理單純的數據(而不管數據的來源是什么)。 gzip是一種文件壓縮工具(或該壓縮工具產生的壓縮文件格式),它的設計目標是處理單個的文件。g...

    Go語言學習之new與make(The way to go)

    生命不止,繼續 go go go !!! 博客《Go語言學習之指針(The way to go)》中介紹了golang中的指針,我們用到了new: 可以看到new返回的是指針。 博客《Go語言學習之Arrays和Slices (The way to go)》中介紹了slice,用到了make: 我想大多數人對new和make挺懵懂的,尤其是從c或c++過來的程序員。那么今天就詳細介紹一下new與m...

    Go語言學習之encoding/xml(The way to go)

    生命不止,繼續 go go go !!! 介紹了encoding/json包的使用,就沒有理由不介紹encoding/xml包。 xml vs json xml和json都是文本表示的數據格式: 跨平臺 跨系統交換數據 但是,XML更適合標記文檔,JSON更適合數據交互。 兩者最大的不同在于,XML是一個完整的標記語言,而JSON不是。XML利用標記語言的特性提供了絕佳的延展性(如XPath),在...

    Go實戰--go中使用libphonenumber(The way to go)

    生命不止,繼續 Go go go !!! 之前寫過關于libphonenumber的相關博客: 《libphonenumber–windows上編譯libphonenumber.lib以及使用(C++、VS2015)》 也就是C++語言在中,使用libphonenumber。 今天就跟大家一起學習一下,如何在go語言中使用libphonenumber。 何為libphonenumber...

    Go實戰--go中使用cookie(The way to go)

    聲明不止,繼續 Go go go !!! 何為cookie Cookie,有時也用其復數形式 Cookies,指某些網站為了辨別用戶身份、進行 session 跟蹤而儲存在用戶本地終端上的數據(通常經過加密)。定義于 RFC2109 和 2965 中的都已廢棄,最新取代的規范是 RFC6265[1] 。(可以叫做瀏覽器緩存) wiki: An HTTP cookie (also called we...

    猜你喜歡

    Go實戰--go中使用rpc(The way to go)

    生命不止,繼續 go go go !!! 今天聊聊golang中如何使用rpc,各位先稍安勿躁,我們先介紹一些基本的知識。 TCP和Http 這個stackoverlow上的回答,覺得很好,搬過來: HTTP is a layer built ontop of the TCP layer to some what standardize data transmission. So naturall...

    Go語言學習之sync包(臨時對象池Pool、互斥鎖Mutex、等待Cond)(the way to go)

    生命不止,繼續 go go go!!! golang的特點就是語言層面支持并發,并且實現并發非常簡單,只需在需要并發的函數前面添加關鍵字go。 但是如何處理go并發機制中不同goroutine之間的同步與通信,golang 中提供了sync包來解決相關的問題,當然還有其他的方式比如channel,原子操作atomic等等,這里先介紹sync包的用法. 這里,跟大家一些學習golang的標準庫,sy...

    Go實戰--golang生成uuid(The way to go)

    生命不止,繼續 go go go !!! 在做server開發的時候,難免用到uuid,這里與大家分享一下golang中如何生成uuid。 什么是uuid? uuid是Universally Unique Identifier的縮寫,即通用唯一識別碼。 uuid的目的是讓分布式系統中的所有元素,都能有唯一的辨識資訊,而不需要透過中央控制端來做辨識資訊的指定。如此一來,每個人都可以建立不與其它人沖突...

    HTML中常用操作關于:頁面跳轉,空格

    1.頁面跳轉 2.空格的代替符...

    freemarker + ItextRender 根據模板生成PDF文件

    1. 制作模板 2. 獲取模板,并將所獲取的數據加載生成html文件 2. 生成PDF文件 其中由兩個地方需要注意,都是關于獲取文件路徑的問題,由于項目部署的時候是打包成jar包形式,所以在開發過程中時直接安照傳統的獲取方法沒有一點文件,但是當打包后部署,總是出錯。于是參考網上文章,先將文件讀出來到項目的臨時目錄下,然后再按正常方式加載該臨時文件; 還有一個問題至今沒有解決,就是關于生成PDF文件...

    精品国产乱码久久久久久蜜桃不卡