Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang png draw transparent

Tags:

image

go

I am trying to draw multiple transparent images to form a big one and save it as PNG

func generateUserImage(username string, items []models.Item) error {
    imageFile, err := os.Create("public/items/users/" + username + ".png")
    if err != nil {
        return err
    }
    profileImage := image.NewRGBA(image.Rect(0, 0, 261, 336))
    for _, item := range items {
        revel.INFO.Println(item)
        itemFile, err := os.Open("public/items/universe/" + item.Type + "/" + item.Name + ".png")
        if err != nil {
            return err
        }
        itemImage, err := png.Decode(itemFile)
        if err != nil {
            return err
        }
        draw.Draw(profileImage, profileImage.Bounds(), itemImage, image.Point{0, 0}, draw.Src)
        itemFile.Close()
    }
    err = png.Encode(imageFile, profileImage)
    if err != nil {
        return err
    }
    defer imageFile.Close()
    return nil
}

Everything seems to be working fine except that the final image will only contain the LAST image of the range loop (even tho the range loops 5 times). All images are .png and with transparent background. Here is a demo on how images look

enter image description here

You can try to save the image and see that the background is transparent... So I have no idea why the final image only contains 1 image and not all

Thanks

like image 452
Raggaer Avatar asked Sep 06 '25 03:09

Raggaer


1 Answers

As mentioned in the comments.

draw.Draw(profileImage, profileImage.Bounds(), itemImage, image.Point{0, 0}, draw.Over)
like image 94
Raggaer Avatar answered Sep 07 '25 19:09

Raggaer