magick icon indicating copy to clipboard operation
magick copied to clipboard

feature request: flatten gif animations

Open technoweenie opened this issue 10 years ago • 0 comments

Flattening GIF animations is heresy, I know. It was a difficult choice for our avatar system :)

I have a local PR where I tried implementing it in Go, but it blew up when trying to call C.WriteImages() in ToFile():

Assertion failed: (images->signature == MagickSignature), function WriteImages, file magick/constitute.c, line 1340.
diff --git a/magick.go b/magick.go
index 6a5a3b2..8bf45e3 100644
--- a/magick.go
+++ b/magick.go
@@ -458,6 +458,10 @@ func (im *MagickImage) Strip() (err error) {
        return
 }

+func (im *MagickImage) Flatten() {
+       im.ReplaceImage(C.GetFirstImageInList(im.Image))
+}
+
 // ToBlob takes a (transformed) MagickImage and returns a byte slice in the format you specify with extension.
 // Magick uses the extension to transform the image in to the proper encoding (e.g. "jpg", "png")
 func (im *MagickImage) ToBlob(extension string) (blob []byte, err error) {
diff --git a/magick_test.go b/magick_test.go
index ab11620..77ed59f 100644
--- a/magick_test.go
+++ b/magick_test.go
@@ -196,6 +196,19 @@ func TestNegateImage(t *testing.T) {
        assert.T(t, image != nil)
 }

+func TestFlattenImage(t *testing.T) {
+       filename := "test/test_animated.gif"
+       image, error := NewFromFile(filename)
+       assert.T(t, error == nil)
+       assert.T(t, image != nil)
+       assert.T(t, image.Image != nil)
+       assert.T(t, image.ImageInfo != nil)
+       image.Flatten()
+
+       err := image.ToFile("test/test_out2.gif")
+       assert.T(t, err == nil)
+}
+
 func TestToBlob(t *testing.T) {
        image := setupImage(t)
        bytes, err := image.ToBlob("png")

We solve this now by adding [0] to the input file name in the convert args.

technoweenie avatar Nov 10 '14 16:11 technoweenie