Skip to content

Convert any visual media to GIF-files

License

Notifications You must be signed in to change notification settings

Hawkynt/AnythingToGif

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

113 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AnythingToGif

License Language Last CommitActivity GitHub release Downloads

This is a versatile tool designed to convert a wide variety of visual media formats into high-quality GIFs (with a hard 'G'), supporting TrueColor images. This utility excels in converting both still images and video files into GIFs, ensuring superior color fidelity and efficient processing.

  • Command line interface
AnythingToGif 1.0.2.0
Copyright (C) 2025 Hawkynt
Converts anything to GIF format.

Usage: AnythingToGif [<input>] [<options>] | <input> <output> [<options>]

  -a, --useAntRefinement                (Default: false) Whether to apply Ant-tree like iterative refinement after
                                        initial quantization.
  -b, --firstSubImageInitsBackground    (Default: true) Whether the first sub-image initializes the background.
  -c, --colorOrdering                   (Default: MostUsedFirst) Color ordering mode.
  -d, --ditherer                        (Default: FloydSteinberg) Ditherer to use.
  --bayer                               (Default: 0) Generate 2^n Bayer matrix (e.g., --bayer 4 creates 16x16 matrix).
                                        When specified, overrides --ditherer. Valid range: 1-8.
  -f, --useBackFilling                  (Default: false) Whether to use backfilling.
  -i, --antIterations                   (Default: 25) Number of iterations for Ant-tree like refinement.
  -m, --metric                          (Default: Default) Color distance metric to use.
  -n, --noCompression                   (Default: false) Whether to use compressed GIF files or not.
  -p, --usePca                          (Default: false) Use PCA (Principal Component Analysis) preprocessing before
                                        quantization.
  -q, --quantizer                       (Default: Octree) Quantizer to use.
  --help                                Display this help screen.
  --version                             Display version information.
  input (pos. 0)                        Input directory or file. If not specified, defaults to the current directory.
  output (pos. 1)                       Output directory or file. If not specified, defaults to the current directory.

Color Distance Metrics:
  Default: Let application decide
  Euclidean: Euclidean
  EuclideanRGBOnly: Euclidean (RGB only)
  Manhattan: Manhattan
  ManhattanRGBOnly: Manhattan (RGB only)
  CompuPhase: CompuPhase
  EuclideanBT709: Weighted Euclidean (BT.709)
  EuclideanNommyde: Weighted Euclidean (Nommyde)
  WeightedEuclideanLowRed: Weighted Euclidean (low red component)
  WeightedEuclideanHighRed: Weighted Euclidean (high red component)
  ManhattanBT709: Weighted Manhattan (BT.709)
  ManhattanNommyde: Weighted Manhattan (Nommyde)
  WeightedManhattanLowRed: Weighted Manhattan (low red component)
  WeightedManhattanHighRed: Weighted Manhattan (high red component)
  PNGQuant: PNGQuant
  WeightedYuv: Weighted YUV
  WeightedYCbCr: Weighted YCbCr
  CieDe2000: CIEDE2000
  Cie94Textiles: CIE94 Textiles
  Cie94GraphicArts: CIE94 Graphic Arts

Quantizer Modes:
  Ega16: EGA 16-colors
  Vga256: VGA 256-colors
  WebSafe: Web Safe palette
  Mac8Bit: Mac 8-bit system palette
  MedianCut: Median-Cut
  Octree: Octree
  GreedyOrthogonalBiPartitioning: Greedy Orthogonal Bi-Partitioning (Wu)
  VarianceCut: Variance-Cut
  VarianceBased: Variance-Based
  BinarySplitting: Binary Splitting
  Adu: Adaptive Distributing Units

Ditherer Modes:
  None: None
  FloydSteinberg: Floyd-Steinberg
  EqualFloydSteinberg: Equally-Distributed Floyd-Steinberg
  FalseFloydSteinberg: False Floyd-Steinberg
  JarvisJudiceNinke: Jarvis-Judice-Ninke
  Stucki: Stucki
  Atkinson: Atkinson
  Burkes: Burkes
  Sierra: Sierra
  TwoRowSierra: 2-row Sierra
  SierraLite: Sierra Lite
  Pigeon: Pigeon
  StevensonArce: Stevenson-Arce
  ShiauFan: ShiauFan
  ShiauFan2: ShiauFan2
  Fan93: Fan93
  TwoD: 2D
  Down: Down
  DoubleDown: Doubled Down
  Diagonal: Diagonal
  VerticalDiamond: Vertical Diamond
  HorizontalDiamond: Horizontal Diamond
  Diamond: Diamond
  Bayer2x2: Bayer 2x2
  Bayer4x4: Bayer 4x4
  Bayer8x8: Bayer 8x8
  Bayer16x16: Bayer 16x16
  Halftone8x8: Halftone 8x8
  ADitherXorY149: A-Dither XOR-Y149
  ADitherXorY149WithChannel: A-Dither XOR-Y149 with Channel
  ADitherXYArithmetic: A-Dither XY Arithmetic
  ADitherXYArithmeticWithChannel: A-Dither XY Arithmetic with Channel
  ADitherUniform: A-Dither Uniform
  RiemersmaDefault: Riemersma (Default)
  RiemersmaSmall: Riemersma (Small)
  RiemersmaLarge: Riemersma (Large)
  RiemersmaLinear: Riemersma (Linear)
  WhiteNoise: White Noise (50%)
  WhiteNoiseLight: White Noise (30%)
  WhiteNoiseStrong: White Noise (70%)
  BlueNoise: Blue Noise (50%)
  BlueNoiseLight: Blue Noise (30%)
  BlueNoiseStrong: Blue Noise (70%)
  BrownNoise: Brown Noise (50%)
  BrownNoiseLight: Brown Noise (30%)
  BrownNoiseStrong: Brown Noise (70%)
  PinkNoise: Pink Noise (50%)
  PinkNoiseLight: Pink Noise (30%)
  PinkNoiseStrong: Pink Noise (70%)
  KnollDefault: Knoll (Default)
  KnollBayer8x8: Knoll (8x8 Bayer)
  KnollHighQuality: Knoll (High Quality)
  KnollFast: Knoll (Fast)
  NClosestDefault: N-Closest (Default)
  NClosestWeightedRandom5: N-Closest (Weighted Random 5)
  NClosestRoundRobin4: N-Closest (Round Robin 4)
  NClosestLuminance6: N-Closest (Luminance 6)
  NClosestBlueNoise4: N-Closest (Blue Noise 4)
  NConvexDefault: N-Convex (Default)
  NConvexProjection6: N-Convex (Projection 6)
  NConvexSpatialPattern3: N-Convex (Spatial Pattern 3)
  NConvexWeightedRandom5: N-Convex (Weighted Random 5)
  AdaptiveQualityOptimized: Adaptive (Quality Optimized)
  AdaptiveBalanced: Adaptive (Balanced)
  AdaptivePerformanceOptimized: Adaptive (Performance Optimized)
  AdaptiveSmartSelection: Adaptive (Smart Selection)

Color Ordering Modes:
  MostUsedFirst: Ordered by usage, the most used first
  FromCenter: Ordered by distance to center, most close first
  LeastUsedFirst: Ordered by usage, the least used first
  HighLuminanceFirst: Ordered by luminance, the brightest colors first
  LowLuminanceFirst: Ordered by luminance, the darkest colors first
  Random: Purely random

Insufficient arguments try '--help' for help.

Overview

In the 1990s, the GIF file format was the dominant image format on the web, known for its efficiency, portability, and support for animation and transparency. However, due to concerns over patent claims on the LZW compression algorithm, the PNG format was introduced as a replacement, offering several advantages over GIF. Despite these advantages, it was often believed that GIFs were limited to 256-color palettes, making them unsuitable for full-color images. This belief is only partially correct. GIFs can indeed contain many colors by utilizing multiple graphic rendering blocks, each with its own local color table.

High-Color GIFs Explained

GIF files traditionally support an 8-bit pixel format, meaning each pixel value references a table with up to 256 colors. However, this limitation can be circumvented by using multiple graphic rendering blocks within a single GIF image. Each block can have its own local color table of 256 colors, drawn from a 24-bit color space.

To create a high-color GIF, AnythingToGif partitions the color set across multiple frames. The process works as follows:

  1. First Frame: The initial frame contains 256 commonly used colors from the original image, using traditional quantization and dithering techniques to ensure a high-quality base image. This approach ensures that the initial frame is a visually appealing approximation of the final image, allowing the image to visually converge faster. By starting with a high-quality base, the subsequent frames enhance the image more efficiently, leading to a smoother and quicker rendering process. This method improves both the visual quality and the user experience, as the GIF appears more complete and detailed from the beginning. The user can also start with a blank canvas if he choses to do so. This is controlled by this FirstSubImageInitsBackground flag.

  2. Subsequent Frames: Each subsequent frame introduces 255 new colors. The 256th color in each frame is reserved for transparency, allowing the frame to be overlaid on the previous ones without completely obscuring them. This utilizes a feature of the GIF format called local color table which is bound to a single frame.

    Color Ordering Method: The colors for each frame are added based on a selected color ordering method, which determines the sequence in which new colors are introduced. This method ensures that the most visually significant colors are prioritized.

    The options include:

    • Random: Colors are added in a random order.
    • MostUsedFirst: Colors that appear most frequently in the image are added first, ensuring that the most common colors are prioritized.
    • FromCenter: Colors are added starting from the center of the image, moving outward.
    • LeastUsedFirst: Colors that appear least frequently are added first.
    • HighLuminanceFirst: Colors with the highest brightness levels are added first, enhancing bright areas of the image initially.
    • LowLuminanceFirst: Colors with the lowest brightness levels are added first, focusing on darker areas initially.
  3. Layering Frames: The frames are layered on top of each other using a GIF feature called the frame disposal method combined with the lowest possible frame delay of 1/100th second. This method ensures that already existing pixels remain on the screen, untouched by subsequent frames. When combined with transparency, it allows new colors to be added without overwriting previous ones. Only the parts of the image that need new colors are repainted in each frame, making the layering process efficient and preserving the visual integrity of the image as it builds up to the full-color representation.

    • Dynamic frame shift: Only save the changed parts by utilizing frame offsets and arbitrary sized frames.
  4. Incremental Improvement: This layering approach enables the GIF to incrementally improve the color representation of the image. Initially, the image may appear coarse because the first frame contains only the 256 commonly used colors. However, as more frames are rendered, each introducing new colors, the image quality progressively converges closer to the original full-color image.

    The process can be customized using the UseBackFilling flag. When the UseBackFilling flag is enabled, the tool not only paints pixels with exact color matches from the current palette but also approximates other areas with the nearest available colors. This means that even areas without an exact color match are painted in each increment, providing a more visually complete image early on. These areas are repainted in subsequent frames as more colors become available, resulting in a smoother and faster visual convergence.

    Alternatively, if the UseBackFilling flag is disabled, the tool will only paint pixels that have exact color matches in the current palette. This method ensures that each pixel is only painted when its exact color is available, which might result in a more staggered convergence but maintains precise color accuracy throughout the process.

Further Links for this part:

Color Quantization

The initial frame requires an approximate palette that represents the full range of colors in the image. Several methods for color quantization are employed to achieve this, including:

Further Links for this part:

Dithering

Dithering techniques are applied to ensure the first frame provides a good base image. Methods include:

  • None
  • ErrorDiffusion
  • Matrix-based
    • Bayer Matrix (2x2, 4x4, 8x8, 16x16, arbitrary 2^n sizes via CLI)
    • Halftone
    • [Interleaved Gradient Noise]
  • Arithmetic Dither - Procedural spatial dithering with multiple patterns
    • XOR-Y149 pattern
    • XOR-Y149 with channel variation
    • XY Arithmetic pattern
    • XY Arithmetic with channel variation
    • Uniform pattern
  • Riemersma [1] - Space-filling curve based dithering with four variants (Default, Small, Large, Linear)
  • Noise-Based Dithering - Statistical noise patterns for dithering:
    • White Noise: Uniform random distribution across all frequencies, completely uncorrelated
    • Blue Noise: High-frequency emphasis with good spatial distribution, avoids clustering artifacts
    • Brown Noise: Low-frequency emphasis with Brownian motion characteristics, smoother patterns
    • Pink Noise: 1/f noise with balanced frequency distribution between white and brown noise
    • Each type available in Light (30%), Normal (50%), and Strong (70%) intensity variants
  • Average
  • Random
  • Joel Yliluoma's algorithm 1
  • Joel Yliluoma's algorithm 2
  • Joel Yliluoma's algorithm 3
  • Thomas Knoll - Advanced ordered dithering with candidate generation (4 variants: Default, Bayer8x8, High Quality, Fast)
  • N-Closest - Selects from N closest palette colors with multiple strategies (Random, Weighted Random, Round Robin, Luminance, Blue Noise)
  • N-Convex - Creates convex hull from N closest colors for better color mixing (Barycentric, Projection, Spatial Pattern, Weighted Random)
  • Adaptive Dithering - Intelligent algorithm selection based on image analysis:
    • Quality Optimized: Prioritizes visual quality over performance
    • Balanced: Balances quality and performance considerations
    • Performance Optimized: Optimizes for speed while maintaining acceptable quality
    • Smart Selection: Uses ML-like scoring to select optimal algorithm based on image characteristics (color complexity, edge density, gradient smoothness, noise level, detail level)
  • Ostromoukhov Variable-Coefficient Error Diffusion - Advanced error diffusion that varies the dithering kernel based on current pixel value to achieve blue-noise characteristics
  • Yliluoma Ordered Dithering - Joel Yliluoma's arbitrary-palette positional dithering algorithms (1, 2, 3) optimized for better contrast and color fidelity
  • Serpentine/Boustrophedon Scanning - Use --serpentine flag with any matrix-based error diffusion ditherer to apply alternating scan direction per row, eliminating directional artifacts. Works with all MatrixBasedDitherer variants including Floyd-Steinberg, Stucki, JJN, Atkinson, Burkes, Sierra, and others.
  • Structure-Aware Error Diffusion - Modern error diffusion that preserves image structure and details using circular error distribution:
    • Default: Standard structure-aware with 2-pixel radius
    • Priority: Priority-based ordering with 3-pixel radius
    • Large: Extended radius (4 pixels) for maximum quality
  • Dizzy Dithering - Novel 2024 error diffusion algorithm that produces blue noise characteristics through spiral pattern distribution:
    • Default: Standard dizzy dithering with balanced settings
    • High Quality: Reduced randomness for maximum quality
    • Fast: Optimized for speed with smaller spiral radius
  • Barycentric
  • Triangulated Irregular Network
  • Natural Neighbour

Further Links for this part:

Color distance calculation

Further Links for this part:

GIF Format

Due to the nature of this application, AnythingToGif requires fine-grained control over the bytes written to disk. This includes managing local palettes, transparency, frame delays, and frame disposal methods. To achieve this level of precision, the tool incorporates its own GIF writing code, built directly from the GIF specifications. This custom code ensures that every aspect of the GIF format is meticulously handled, allowing for the creation of high-color images and smooth animations. The output is rigorously checked using various GIF debugging tools, ensuring compatibility and optimal performance across different platforms and browsers.

Further Links for this part:

Practical Considerations

While high-color GIFs can accurately represent complex images, they often result in large file sizes due to the numerous frames required. One approach to mitigate this is to encode more image information into the first few frames, creating an approximation of the full image and refining it in subsequent frames. This results in larger files because fewer pixels are transparent, but it improves the visual quality of the initial rendering.

Reducing the number of distinct colors in the image can also help manage file size and loading times. For example, using just 5 frames allows for (5 * 255) + 1 = 1276 (2551 for 10 frames ~0.1sec, 5101 for 20 frames ~0.2sec, 2.550.001 for 100 frames ~1sec) different colors, which is a significant improvement over the traditional 256-color palette.

Converting video

When converting video, AnythingToGif processes changed areas from frame to frame, introducing new frames as necessary using the same algorithms applied to still images. This ensures high-quality color reproduction and smooth transitions at the cost of a higher framerate whenever needed.

  • Frame extrapolation: Get the images from a video.
  • Differential frame encoding: Only process the differencies between each frame.
  • Constant FPS: Switching between constant fps inserting dummy frames as needed or variable frame rate
  • Concatenation: Combining multiple files into one video.

About

Convert any visual media to GIF-files

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

  •  

Packages

No packages published

Contributors 2

  •  
  •  

Languages