登录
首页 >  Golang >  Go问答

给图像调色板增加颜色

来源:stackoverflow

时间:2024-02-25 14:21:24 451浏览 收藏

编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天golang学习网就整理分享《给图像调色板增加颜色》,文章讲解的知识点主要包括,如果你对Golang方面的知识点感兴趣,就不要错过golang学习网,在这可以对大家的知识积累有所帮助,助力开发能力的提升。

问题内容

我正在 go 中创建一个实用程序,它将加载图像(png 或 gif)并将其与另一个图像覆盖,为了帮助调试这个问题,我编写了一些代码来突出显示图像的边框透明度为红色,因此红色 color.color{r:255,g:0,b:0,a:255} 需要添加到调色板(如果调色板中尚不存在),所有这些都使用与以下相同的代码图像叠加过程,但更容易理解,因此我将在这里分享该代码。

除了一个方面之外,我的工作正常,那就是我需要将叠加层的调色板添加到源图像中。

我能够提取并附加到源图像的调色板,并创建一个新的 image.paletted,其中包含源图像中的所有 image.image.pix 数据,并且 image.paletted.palette 包含新的颜色,但是当使用 image.paletted.setcolorindex() 时(使用 image.paletted.palette.index() 获取正确的索引并将结果保存到文件后,没有任何更改。

奇怪的是,如果我注释掉所有调色板代码,覆盖图像将写入文件,但颜色会调整为源图像中最近的邻居(根据 go 文档)。

我不确定为什么修改调色板会阻止我写入图像。

这是循环遍历边缘像素并向其绘制红色的代码。

func applyattachment(img image.image, augment *augment.augment, attachment *augment.attachment, debug bool) (appliedimage image.image, err error) {
    edgepoints := findedge(img, edgefromstring(attachment.name))
    if debug {
        img, err = addtopallete(img, red)
        if err != nil {
            logger.warn("error adding to pallete: %s", err)
        }
        for _, pt := range edgepoints {
            if err = setimagepixel(&img, pt.x, pt.y, red); err != nil {
                logger.warn("error setting image pixel: %s", err)
            }
        }
    } 
    ...

这是 addtopalette 的代码

// addtopallete adds a color to the palette of an image.
func addtopallete(addto image.image, c color.color) (output image.image, err error) {
    srcpallete := extractpallete(addto)
    if !palletecontains(srcpallete, c) {
        srcpallete = append(srcpallete, c)
    }

    pimg := image.newpaletted(addto.bounds(), srcpallete)
    ipimg := pimg.subimage(pimg.bounds())
    output, err = imageontoimage(&ipimg, &addto, augment.point{x: 0, y: 0})
    return
}

这是 imageontoimage 的代码(它将一个 image.image 写入另一个图像)

// ImageOntoImage does exactly that
func ImageOntoImage(
    source *image.Image,
    overlay *image.Image,
    startAt augment.Point,
) (output image.Image, err error) {
    output = *source
    for curY := startAt.Y; curY < startAt.Y+(*overlay).Bounds().Max.Y; curY++ {
        for curX := startAt.X; curX < startAt.X+(*overlay).Bounds().Max.X; curX++ {
            c := output.At(curX, curY)
            oc := (*overlay).At(curX-startAt.X, curY-startAt.Y)
            cc := combineColors(c, oc)
            if err = SetImagePixel(&output, curX, curY, cc); err != nil {
                err = errors.New("Cannot modify image")
                return
            }
        }
    }
    return
}

// SetImagePixel sets a single pixel of an image to another color
func SetImagePixel(
    source *image.Image,
    x int,
    y int,
    color color.Color,
) (err error) {
    if poutput, ok := (*source).(*image.Paletted); ok {
        cindex := poutput.Palette.Index(color)
        poutput.SetColorIndex(x, y, uint8(cindex))
        asImg := image.Image(poutput)
        source = &asImg
    } else if coutput, ok := (*source).(Changeable); ok {
        coutput.Set(x, y, color)
    } else {
        err = errors.New("Cannot modify image")
        return
    }
    return
}


总而言之,当我不展开调色板时,我可以在图像上设置像素,但像素会更改为最接近的颜色,当我展开调色板时,像素不会设置,(或也许,最接近的颜色以某种方式近似透明?)


解决方案


所以我觉得有点尴尬。 我所有的调色板代码都工作正常。 我被指针烧伤了。 通过创建新的调色板图像,我失去了与返回原始图像的指针的连接,因此设置像素在 go 的范围界定中丢失了。 这就是为什么注释掉调色板代码修复了它,设置新像素时仍然使用原始指针,当我创建调色板图像时,我超出了原始指针的范围,虽然它正确设置了像素,但保存image 函数仍然引用原始加载的图像,因此它基本上是创建该图像的副本。

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>