Resize animated GIFs with ruby?

I am trying to resize GIF images to different sizes. I use the RMagick library in ruby. But it seems that for some gif images, the file size increases even when I reduce the GIF. And I am resizing the image in the same aspect ratio. Here is my code.

require 'rmagick'
path = "/path/to/file/"
s_image = "image.gif" # image is 320*320
t_image = "t.gif"
file_name = path+s_image
file = File.new(file_name)
list = Magick::ImageList.new.from_blob file.read
list = list.coalesce.remap
list.each do |x|
  x.resize_to_fill!(256,256)
end
File.open("#{path+t_image}", 'wb') { |f| f.write list.to_blob }

What am I missing?

+4
source share
1 answer

35 . , , . , . gif - ( ), .

, , , .

, , . GIMP .

, , , . . x, y , 1/4 . , . 35 , , .

, ImageMagick ( Rmagick Ruby) GIF , . optimize_layers, . , .remap, , , .

require 'rmagick'
path = "/path/to/file/"
s_image = "s_image.gif" # image is 320*320
t_image = "t_image.gif"
file_name = path+s_image
file = File.new(file_name)
list = Magick::ImageList.new.from_blob file.read

# This renders out each GIF frame in full, prior to re-sizing
# Note I have removed the .remap because it alters pixel values
# between frames, making it hard to optimise
list = list.coalesce

list.each do |x|
  x.resize_to_fill!(256,256)
end

# Re-optimize the GIF frames
list = list.optimize_layers( Magick::OptimizeLayer )

File.open("#{path+t_image}", 'wb') { |f| f.write list.to_blob }
+7

Source: https://habr.com/ru/post/1527917/


All Articles