0

I want to make GIF from video frames. Per frame (UIImage) size is at most 960 * 540. As GIF is bound to 256 colors, I got more colors issues. frames are poorly generated. After so many research, I've found Dithering approach that may reduce color bandings.
I didn't go with this approach because default time complexity of dithering is O(N^2) where N is the multiplication of Image's width and height. I don't know how to reduce color banding efficiently without dithering for 200 frames. Is there any other technique or filter available which can reduce color banding efficiently?
Original Video : Original video
The GIF I produced : my GIF
The GIF I need : better-GIF

My Approach: First I saved all frames as UIImage from Video. then I created GIF using following code :

func generateImagesToGif(photos: [UIImage], filename: String) -> Bool {
        let documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let path = documentsDirectoryPath.appending(filename)
        let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]]
        let gifProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: 0.5]]
        let cfURL = URL(fileURLWithPath: path) as CFURL
        if let destination = CGImageDestinationCreateWithURL(cfURL, kUTTypeGIF, photos.count, nil) {
            CGImageDestinationSetProperties(destination, fileProperties as CFDictionary?)
            for photo in photos {
                CGImageDestinationAddImage(destination, photo.cgImage!, gifProperties as CFDictionary?)
            }
            return CGImageDestinationFinalize(destination)
        }
        return false
    }

Update : Have a look this (High Quality GIF). Their GIF looks like original video. Very low color bandings!

sagarthecoder
  • 137
  • 1
  • 9
  • What are 'color bandings'? – El Tomato Aug 08 '21 at 07:54
  • https://en.wikipedia.org/wiki/Colour_banding @ElTomato – sagarthecoder Aug 08 '21 at 07:57
  • Oh, okay. Thanks. I thought they are called color-bindings. – El Tomato Aug 08 '21 at 07:58
  • But these approach returned poor result. I need to produce better GIF which looks more natural. @EricAya – sagarthecoder Aug 08 '21 at 08:39
  • May be in this case, dithering is not the proper way I guess. – sagarthecoder Aug 08 '21 at 08:41
  • 1
    Most dithering algorithms don't have a complexity of O(N²) but just O(N), e.g. Floyd-Steinberg dithering. And dithering is required to avoid the banding due to color quantization. – Codo Aug 08 '21 at 09:26
  • So , in my case to produce more natural GIF, what should I do now? Which approach I need to follow? @Codo – sagarthecoder Aug 08 '21 at 09:33
  • 1
    GIF images are restricted to 256 colors. So a *color quantization* step is needed to find the closest color from the table. It should be combined with a *dithering* step to distribute the quantization error to neighboring pixels. I am not aware of a built-in iOS framework that can do this. *CIDither* certainly doesn't help as it is a filter to add noise. And I don't think the *Accelerate* framework helps either. You will probably have to look for a specialized color quantization library such as [https://github.com/ImageOptim/libimagequant](libimagequant). – Codo Aug 08 '21 at 13:37
  • Check https://github.com/topics/color-quantization . One relatively simple algo you could do is popularity algorithm. It reduces number of colors and seeks the nearest (RGB wise) one still in palette for every pixel. – Kamil.S Aug 08 '21 at 15:33
  • Here is an example of Floyd-Steinberg dithering from 24 bit to 16 bit color: https://stackoverflow.com/questions/11640017/what-is-a-good-optimized-c-c-algorithm-for-converting-a-24-bit-bitmap-to-16-b You could try implementing something similar to process pixels of your UIImage to 8 bit color https://en.wikipedia.org/wiki/List_of_monochrome_and_RGB_color_formats#8-bit_RGB_(also_known_as_3-3-2_bit_RGB) before you add them to your gif file. – Leszek Szary Aug 08 '21 at 15:47
  • Hi @LeszekSzary, I don't have permission to comment from stackOverflow. can you help me ? I go through to your link. which value I need to change if I convert UIImage to 8 bit color? I go though this answer https://stackoverflow.com/a/17438757/13671576 – sagarthecoder Aug 08 '21 at 17:17
  • Look this. (https://www.reddit.com/r/HighQualityGifs/comments/p5115d/some_parts_of_this_movie_feel_like_they_have_aged/ ) Their GIF is like original video. It looks very low color banding. – sagarthecoder Aug 16 '21 at 05:07

0 Answers0