gpt4 book ai didi

image - 调整电影图像(动画 GIF)大小时的问题

转载 作者:行者123 更新时间:2023-12-04 13:11:54 25 4
gpt4 key购买 nike

我在调整电影图像的大小时遇到​​问题。如果您不知道,它们是仅对图像的一部分进行动画处理的 gif,与对整个图像进行动画处理的常规 gif 相比。这是一个 node.js 示例:

// ![http://i.imgur.com/Qb1m0.gif][1]

var gm = require('gm')

var file = 'Qb1m0.gif',
frags = file.split('.')

gm(file)
//.noProfile()
//.quality(80)
.resize(200, 200)
.write(frags[0] + '_200.' + frags[1], function(err) {
if (err) console.error(err)
})

// Result:
// ![http://i.imgur.com/eFqak.gif][2]

等效的 cmd 行代码是:
gm convert Qb1m0.gif -resize 200x200 cinema_200.gif

知道是怎么回事吗?

原始动画 GIF:

Original

调整大小的动画 GIF:

Resized

最佳答案

我确实比 GraphicsMagick 更了解 ImageMagick。对于 ImageMagick,以下声明适用:

  • 如果 GIF 包含透明度,则几乎不可能使用简单的命令行(例如您提供的命令行)直接调整动画 GIF 的大小。
  • 如果动画 GIF 不包含透明度,则更难做到这一点。

  • 我认为这对于 GraphicsMagick 来说是一样的,真​​的。

    背景说明

    如果您使用 identify 查看原始 GIF ,您会看到并非每个框架都具有相同的尺寸:
    identify Qb1m0.gif Qb1m0.gif[1]  GIF 720x416 720x416+0+0    8-bit PseudoClass 256c 920KB 0.000u 0:00.009 Qb1m0.gif[1]  GIF 471x122 720x416+160+75 8-bit PseudoClass 256c 920KB 0.000u 0:00.009 Qb1m0.gif[2]  GIF 471x121 720x416+160+76 8-bit PseudoClass 256c 920KB 0.000u 0:00.009 Qb1m0.gif[3]  GIF 471x123 720x416+160+75 8-bit PseudoClass 256c 920KB 0.000u 0:00.009 Qb1m0.gif[4]  GIF 471x123 720x416+160+75 8-bit PseudoClass 256c 920KB 0.000u 0:00.009 Qb1m0.gif[5]  GIF 471x123 720x416+160+75 8-bit PseudoClass 256c 920KB 0.000u 0:00.009 Qb1m0.gif[6]  GIF 471x123 720x416+160+75 8-bit PseudoClass 256c 920KB 0.000u 0:00.009 Qb1m0.gif[7]  GIF 471x123 720x416+160+75 8-bit PseudoClass 256c 920KB 0.000u 0:00.009 Qb1m0.gif[8]  GIF 471x122 720x416+160+76 8-bit PseudoClass 256c 920KB 0.000u 0:00.009 Qb1m0.gif[9]  GIF 471x123 720x416+160+75 8-bit PseudoClass 256c 920KB 0.000u 0:00.009 Qb1m0.gif[10] GIF 471x123 720x416+160+75 8-bit PseudoClass 256c 920KB 0.000u 0:00.000 Qb1m0.gif[11] GIF 471x123 720x416+160+75 8-bit PseudoClass 256c 920KB 0.000u 0:00.000 Qb1m0.gif[12] GIF 471x123 720x416+160+75 8-bit PseudoClass 256c 920KB 0.000u 0:00.000 Qb1m0.gif[13] GIF 471x122 720x416+160+76 8-bit PseudoClass 256c 920KB 0.000u 0:00.000 Qb1m0.gif[14] GIF 471x123 720x416+160+75 8-bit PseudoClass 256c 920KB 0.000u 0:00.000 Qb1m0.gif[15] GIF 471x123 720x416+160+75 8-bit PseudoClass 256c 920KB 0.000u 0:00.000

    These variations in the dimensions are the result of frame optimizing the animated GIF has been subjected to in order to reduce file size.

    There are other optimizations at play too, which do reduce the number of colors used. Both these types of optimizations don't combine well with the -resize operation.

    -resize is designed for single images, and to make the resulting single image look as good as possible. This very often adds new color values to the image. This does contradict what GIF is designed for: using a strictly limited color table (maximum of 256 colors). In an animation sequence, the next image/frame's -resize may result in a completely different color table than the previous one produced -- but for a well working animation you'd need a common color table across all frames.

    -resize handles each and every frame image totally separately from the other images and does not take into account 'frame optimizations' (which have the tendency to create a different width+height for each frame that's placed on the common canvas with its own offset).

    Thus the resized images are far from ideal for saving to the limited GIF file format for single images, let alone for multiple frames of an animated GIF. Heavy color reductions in the resized images are the result.

    Then there is the transparency problem: most animated GIFs do make heavy use of transparency. Transparency is frequently used to even achieve compression optimizations where normally the image's appearance wouldn't require transparency at all.

    What happens in this case is this: -resize creates semi-transparent pixels in the overlay images. When the images are saved back to the GIF file format, these pixels are then converted to either full transparency or full opacity: both produce a heavy color distortion for the resulting animation, away from the original color.

    General procedure

    Generally the best procedure to resize animated GIFs is this:

    1. Coalesce (de-optimize) the animation. This will create individual images of equal size for all frames of the animation.

    2. Undergo a complete GIF optimization sequence for the animation: not just for frame optimization, but also for color optimization.

    'Simple' command

    To still try your luck with running a 'simple' resize command, you could try this:

    convert                        \
    http://i.imgur.com/Qb1m0.gif \
    -coalesce \
    -resize 200x200 \
    cinema_200.gif

    结果:

    Result

    此命令可以解决帧优化问题。它将以增加文件大小为代价来纠正由此产生的问题。

    但是,当涉及到边缘时,它可能仍会显示“楼梯”伪影,因为调整大小的帧会出现可怕的锯齿。这是因为抗锯齿需要边缘周围的半透明颜色,但 GIF 不能保存由 -resize 生成的半透明颜色。运算符(operator)。

    关于image - 调整电影图像(动画 GIF)大小时的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12293832/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com