第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Golang 如何將圖像相互連接/附加

Golang 如何將圖像相互連接/附加

Go
千萬里不及你 2021-12-20 17:06:33
Go 有很棒的圖像處理和數(shù)據(jù)庫,但是我無法從較小的圖像創(chuàng)建一個大圖像。有誰知道如何在 Golang 中獲取兩個 png 或 jpeg 文件并將它們連接起來形成一個包含兩個(或更多)文件的大圖像?我目前正在閱讀這樣的 png 文件:imgFile, err := os.Open(path)if err != nil {    return Image{}, err}img, _, err := image.Decode(imgFile)if err != nil {    return Image{}, err}rgba := image.NewRGBA(img.Bounds())if rgba.Stride != rgba.Rect.Size().X*4 {    return Image{}, fmt.Errorf("unsupported stride")}draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src)我對如何獲取此 png RGBA 數(shù)據(jù)并與其他 RGBA 數(shù)據(jù)連接和/或?qū)⑵浣M合成“空”圖像感到困惑。
查看完整描述

3 回答

?
飲歌長嘯

TA貢獻1951條經(jīng)驗 獲得超3個贊

創(chuàng)建一個新的空圖像 (NewRGBA),其邊界足夠大以容納兩個圖像。然后使用該Draw方法在這個新的大圖像的適當部分繪制每個圖像。


以下是代碼步驟。


加載兩個圖像。


imgFile1, err := os.Open("test1.jpg")

if err != nil {

    fmt.Println(err)

}

imgFile2, err := os.Open("test2.jpg")

if err != nil {

    fmt.Println(err)

}

img1, _, err := image.Decode(imgFile1)

if err != nil {

    fmt.Println(err)

}

img2, _, err := image.Decode(imgFile2)

if err != nil {

    fmt.Println(err)

}

讓我們在第一個圖像的右側(cè)繪制第二個圖像。所以它的起點應(yīng)該是第一個圖像的寬度在(w, 0)哪里w。第一個圖像的右下角將是第二個圖像的左下角。


//starting position of the second image (bottom left)

sp2 := image.Point{img1.Bounds().Dx(), 0}

它應(yīng)該在一個足以容納它的矩形中。


//new rectangle for the second image

r2 := image.Rectangle{sp2, sp2.Add(img2.Bounds().Size())}

現(xiàn)在創(chuàng)建一個大矩形,其寬度足以容納兩個圖像。


//rectangle for the big image

r := image.Rectangle{image.Point{0, 0}, r2.Max}

注意此大圖像將具有第二個圖像的高度。如果第一張圖像更高,它將被裁剪。


創(chuàng)建一個新圖像。


rgba := image.NewRGBA(r)

現(xiàn)在您可以將這兩個圖像繪制到這個新圖像中


draw.Draw(rgba, img1.Bounds(), img1, image.Point{0, 0}, draw.Src)

draw.Draw(rgba, r2, img2, image.Point{0, 0}, draw.Src)

由于我們在r2第一張圖片的右側(cè)創(chuàng)建了它,因此第二張圖片將被繪制到右側(cè)。


最后就可以導出了。


out, err := os.Create("./output.jpg")

if err != nil {

    fmt.Println(err)

}


var opt jpeg.Options

opt.Quality = 80


jpeg.Encode(out, rgba, &opt)


查看完整回答
反對 回復(fù) 2021-12-20
?
jeck貓

TA貢獻1909條經(jīng)驗 獲得超7個贊

如果你把一些東西變成函數(shù)并創(chuàng)建一個結(jié)構(gòu)來理解每個像素,你的生活會容易得多。


// Create a struct to deal with pixel

type Pixel struct {

    Point image.Point

    Color color.Color

}


// Keep it DRY so don't have to repeat opening file and decode

func OpenAndDecode(filepath string) (image.Image, string, error) {

    imgFile, err := os.Open(filepath)

    if err != nil {

        panic(err)

    }

    defer imgFile.Close()

    img, format, err := image.Decode(imgFile)

    if err != nil {

        panic(err)

    }

    return img, format, nil

}


// Decode image.Image's pixel data into []*Pixel

func DecodePixelsFromImage(img image.Image, offsetX, offsetY int) []*Pixel {

    pixels := []*Pixel{}

    for y := 0; y <= img.Bounds().Max.Y; y++ {

        for x := 0; x <= img.Bounds().Max.X; x++ {

            p := &Pixel{

                Point: image.Point{x + offsetX, y + offsetY},

                Color: img.At(x, y),

            }

            pixels = append(pixels, p)

        }

    }

    return pixels

}


查看完整回答
反對 回復(fù) 2021-12-20
?
慕森卡

TA貢獻1806條經(jīng)驗 獲得超8個贊

我正是為此目的建立了一個圖書館。


您可以按如下方式使用它;


import gim "github.com/ozankasikci/go-image-merge"


grids := []*gim.Grid{

    {ImageFilePath: "test1.jpg"},

    {ImageFilePath: "test2.png"},

}


// merge the images into a 2x1 grid

rgba, err := gim.New(grids, 2, 1).Merge()


// save the output to jpg or png

file, err := os.Create("file/path.jpg|png")

err = jpeg.Encode(file, rgba, &jpeg.Options{Quality: 80})

https://github.com/ozankasikci/go-image-merge


查看完整回答
反對 回復(fù) 2021-12-20
  • 3 回答
  • 0 關(guān)注
  • 206 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號