Search code examples
imageimage-processingdeep-learningh.264data-augmentation

Is it possible to apply h.264 compression to image?


Currently, I am applying h.264 compression to a single image.
As you can see in the picture below, I want to compress that image based on h.264 with 40 constant quantization.

enter image description here However, there are no relevant functionality anywhere, such as Python-based libraries (opencv, ffmpeg).

Also, there is no github for applying to single image and well-structured h.264 algorithm.

So, is there any github implemetation or library to do it?

Thanks in advance.


Solution

  • There are cases (like academic purposes) that is does make sense to encode a single frame video.

    There is an existing image format named HEIF that based on HEVC (H.265) codec, but for H.264, you must encode a video file.

    You may use FFmpeg command line tool for creating a single frame video file:

    ffmpeg -i input.png -vcodec libx264 -qp 40 -pix_fmt yuv420p single_frame.mp4
    

    (Assuming your input image is input.png).

    The -qp 40 argument applies constant quantization 40.

    qp (qp)
    Set constant quantization rate control method parameter

    See: libx264 documentation

    Note:
    Since it's a single frame, the frame type is going to be an I-Frame.


    In case you prefer elementary H.264 stream (without MP4 container), you may use:

    ffmpeg -i input.png -vcodec libx264 -qp 40 -pix_fmt yuv420p single_frame.264
    

    There are Python bindings for FFmpeg, like ffmpeg-python.
    You may use it, for encoding a NumPy array that represents a raw video frame.
    You may also use a pipe to FFmpeg sub-process as the following sample.


    You can find source code for H.264 encoder in GitHub (here for example).
    As far a I know, the sources are implemented in C.

    Without looking at the sources, I don't think you are going to consider them as "well-structured".