设置 image_type 时 RMagick 奇怪的绿色 gif
所以我得到了下面的代码,尝试使用 RMagick 在 ruby 中将 png 转换为 gif,但在图像的某些部分出现了一些奇怪的绿色东西。
require 'RMagick'
include Magick
img = Magick::Image.read(pngPath.open).first
//if comment out the following line, there is no problem
img.image_type=PaletteMatteType
img.transparent("#00FF00")
img.write(gifPath)
PS:如何将深度更改为8或16以及如何将颜色更改为256?深度和颜色的默认值是 8 和 256,对吧?
So I got this following code trying to convert a png to gif in ruby using RMagick, but got some weird green stuff on some part of the image.
require 'RMagick'
include Magick
img = Magick::Image.read(pngPath.open).first
//if comment out the following line, there is no problem
img.image_type=PaletteMatteType
img.transparent("#00FF00")
img.write(gifPath)
PS: how to change the depth to 8 or 16 and How to change colors to 256? The default for depth and colors are 8 and 256 right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
大多数 RMagick 方法返回新图像,而不是就地修改它;某些方法具有用于修改内容的
!
版本(例如缩放
和scale!
),但有些则不然(例如量化
)。特别是,
transparent
返回新图像:因此,只需说
img.transparent("#00FF00")
创建一个具有所需透明度的新图像,然后将其丢弃,因为您忽略了返回值。另外,将某些内容放入较小调色板的常用方法是使用
量化
。我想你想要这样的东西:
至少对我有用。
Most RMagick methods return the new image rather than modifying it in-place; some methods have
!
versions for modifying things in place (such asscale
andscale!
) but some don't (such asquantize
).In particular,
transparent
returns the new image:So just saying
img.transparent("#00FF00")
creates a new image with the desired transparency and then throws it away because you are ignoring the return value.Also, the usual way to drop something down to a smaller palette is to use
quantize
.I think you want something like this:
Works for me at least.
不确定默认值,我会更改一些内容:
如果您需要 RMagick,则无需随后包含 Magick,我认为您可以删除此行。
常量应该以 Magick:: 为前缀,所以它可能应该是
Magick::PaletteMatteType
透明方法有 2 个参数,第一个是你想要透明的颜色,第二个是一种透明度。我认为 Gif 只知道一种,但无论如何,尝试一下
Not sure about the defaults by I would change a couple of thing:
if you require RMagick, there is no need to include Magick afterwards, I think you can delete this line.
Constants should be prefixed by Magick::, so it should probably be
Magick::PaletteMatteType
the transparent method takes 2 arguments, the first one being the color that you want to make transparent, and the second is the kind of transparency. I think Gif knows only one kind, but anyway, try