How to create optimised and progressive Images (JPG, PNG) in Go lang

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

How to create optimised and progressive Images (JPG, PNG) in Go lang

问题

我正在使用以下Go代码来调整我的JPEG和PNG格式的图像大小。那么,如何使用Imagick将它们转换为渐进和优化的格式呢?我正在使用ImageMagick 6.9.3-8 Q16 x86_64ubuntu 14.04上。

我之所以说优化,是因为我使用了以下命令来测试图像大小是否减小。但是,它增加了输出文件的大小。

命令:

convert -strip -interlace Plane input-file.jpg output-file.jpg

Go代码:

        size = fmt.Sprintf("%dx%d^+0+0", w, h)
		tx := mw.TransformImage("", size)
		tx.SetImageGravity(imagick.GRAVITY_CENTER)
		offsetX := -(int(w) - int(tx.GetImageWidth())) / 2
		offsetY := -(int(h) - int(tx.GetImageHeight())) / 2
		err := tx.ExtentImage(w, h, offsetX, offsetY)
英文:

I am using the following code in Go to resize my images in JPEG and PNG format. So, how do I convert them to progressive and optimized using Imagick. I'm using ImageMagick 6.9.3-8 Q16 x86_64 on ubuntu 14.04

I am saying optimized reason being I used the following command to test whether image size gets reduced or not. But, it increases the output file size.

Command :

convert -strip -interlace Plane input-file.jpg output-file.jpg

Go code :

        size = fmt.Sprintf("%dx%d^+0+0", w, h)
		tx := mw.TransformImage("", size)
		tx.SetImageGravity(imagick.GRAVITY_CENTER)
		offsetX := -(int(w) - int(tx.GetImageWidth())) / 2
		offsetY := -(int(h) - int(tx.GetImageHeight())) / 2
		err := tx.ExtentImage(w, h, offsetX, offsetY)

答案1

得分: 3

您的convert命令行会剥离图像并给它一个平面交错方案。等效的Go代码应该调用mw.StripImage()和mw.SetImageInterlaceScheme(INTERLACE_PLANE)。

[编辑] 您是在尝试遵循这个示例吗?如果是这样,-interlace Plane 负责使图像渐进,但这并不会使它变小。实现这一点的部分是 -quality 80,您可以通过调用mw.SetImageCompressionQuality(80)在Go中实现。

英文:

Your convert command line strips the image and gives it a planar interlace scheme. An equivalent Go code should call mw.StripImage() and mw.SetImageInterlaceScheme(INTERLACE_PLANE).

[edit] Are you trying to follow this example ? If so, -interlace Plane is responsible for making the image progressive, but this doesn’t make it any smaller. The part that does that is -quality 80, which you can implement in Go by calling mw.SetImageCompressionQuality(80).

答案2

得分: 0

使用ResizeImage代替

tx.ResizeImage(w, h, imagick.FILTER_LANCZOS, 1)

参考调整大小示例

英文:

Use ResizeImage instead

tx.ResizeImage(w, h, imagick.FILTER_LANCZOS, 1)

See the resizing example

huangapple
  • 本文由 发表于 2017年3月17日 21:19:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/42858632.html
匿名

发表评论

匿名网友

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

确定