Golang,在图像下载后,透明背景变为黑色。

huangapple go评论118阅读模式
英文:

Golang, transparent background appears black once image is downloaded

问题

起初我以为这是Mac的问题,因为使用了暗黑模式,但事实并非如此。

我的软件在图像中插入一个标志。

插入完成后,可以将图像保存为png、jpeg或pdf格式。
即使将整个图像应用白色背景,但在将其下载为png格式时,标志周围仍然有黑色背景。
只有在PDF版本中,标志才能正确显示白色背景。

以下是转换的代码:

func moveLogoPosition(mainImage, logo image.Image, poseX, poseY, width, size int, excavate bool) {
	const regularMainImageSize = 300

	m := resize.Resize(uint(75), 0, logo, resize.Bilinear)
	sr := m.Bounds()

	xOrigin := mainImage.Bounds().Size().X/2 - sr.Size().X/2
	yOrigin := mainImage.Bounds().Size().Y/2 - sr.Size().Y/2
	xFinal := xOrigin + sr.Bounds().Size().X
	yFinal := yOrigin + sr.Bounds().Size().Y

	r := image.Rectangle{
		Min: image.Point{X: xOrigin + poseX, Y: yOrigin + poseY},
		Max: image.Point{X: xFinal + poseX, Y: yFinal + poseY},
	}

	draw.Draw(mainImage.(*image.NRGBA), r, m, sr.Min, draw.Src)
}

我想知道是否有什么遗漏,我应该将背景绘制为白色吗?老实说,我对如何做这个操作不太确定。

另外,主图像的透明度没有问题!

我附上了当前结果的示例(png格式)

Golang,在图像下载后,透明背景变为黑色。

英文:

At first I thought it was a mac issue because of dark mode, but no.

My software inserts a logo within an image.

Once the insertion is done, the image can be saved either as a png, jpeg or pdf.
Even after applying a white background to the whole image, when downloading it as a png the logo has a black background surrounding it.
Only from the PDF version, the logo is correctly displayed with a white background.

Here is the transformation:

func moveLogoPosition(mainImage, logo image.Image, poseX, poseY, width, size int, excavate bool) {
	const regularMainImageSize = 300

	m := resize.Resize(uint(75), 0, logo, resize.Bilinear)
	sr := m.Bounds()

	xOrigin := mainImage.Bounds().Size().X/2 - sr.Size().X/2
	yOrigin := mainImage.Bounds().Size().Y/2 - sr.Size().Y/2
	xFinal := xOrigin + sr.Bounds().Size().X
	yFinal := yOrigin + sr.Bounds().Size().Y

	r := image.Rectangle{
		Min: image.Point{X: xOrigin + poseX, Y: yOrigin + poseY},
		Max: image.Point{X: xFinal + poseX, Y: yFinal + poseY},
	}

	draw.Draw(mainImage.(*image.NRGBA), r, m, sr.Min, draw.Src)
}

I'm wondering if I am missing something, should I draw the background white? I'm quite unsure how to do that to be honest.

On the other hand, the main image has no issue with transparency!

I'm joining an example of a current result (as a png)

Golang,在图像下载后,透明背景变为黑色。

答案1

得分: 0

好的,以下是翻译好的内容:

好的,所以如果你试图让透明度自行处理,你会遇到很多问题,这取决于操作系统和PNG的质量等因素。

因此,我们强制将透明像素绘制为白色(在我们的情况下),以确保:
不仅“未挖掘的透明”部分正确显示为白色,而且确保如果使用Illustrator或其他软件打开标志/图像,则“透明度”现在是白色并得到正确支持。

这是我添加的代码:

white := color.RGBA{255, 255, 255, 255}
draw.Draw(mainImage, r, &image.Uniform{C: white}, image.Point{}, draw.Src)
draw.Draw(mainImage, r, m, sr.Min, draw.Over)

我不知道这是否会对任何人有所帮助,但这是我在Go中找到的唯一替代方法。

英文:

Alright, so if you try to let transparency behave by itself you'll encounter too many issues depending on the OS, the quality of the PNG etc.

Thus we force draw transparent pixels to white (in our case) to make sure that:
not only the "non excavated transparent" parts are correctly displayed as white, but also to make sure that if the logo / image where to be opened with illustrator or else, the "transparency" now white is correctly supported.

Here is what I added:

white := color.RGBA{255, 255, 255, 255}
draw.Draw(mainImage, r, &image.Uniform{C: white}, image.Point{}, draw.Src)
draw.Draw(mainImage, r, m, sr.Min, draw.Over)

I don't know if that will ever help anyone, but that's the only alternative I found with Go.

huangapple
  • 本文由 发表于 2022年1月28日 03:03:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/70884297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定