[ANN] Blurhash 0.1.0 is out!

I am pleased to announce the release of a new library Blurhash 0.1.0!

This library implements an encoder for BlurHash in OCaml. BlurHash allows us to convert an image into a short string that represents a lightweight, ‘blurred’ version of the input image. It can be used as a placeholder on a Web page, for example.

The only function this library provides is Blurhash.blur_hash_for_pixels, which takes the same arguments as blurHashForPixels in the C reference implementation.

Although it does not depend on any image library, you can use (e.g.,) camlimages to load an image from a file.

let load_image_as_rgb24 ~path =
  match OImages.(load path [] |> tag) with
  | Rgb24 img -> img
  | Rgba32 img -> img#to_rgb24
  | Index8 img -> img#to_rgb24
  | Index16 img -> img#to_rgb24
  | Cmyk32 _ -> failwith "Not supported image type: Cmyk32"

let blurhash ~x_components ~y_components src =
  Blurhash.blur_hash_for_pixels ~x_components ~y_components ~width:src#width
    ~height:src#height ~bytes_per_row:(src#width * 3) src#dump

let test_encode_case1 () =
  let src = load_image_as_rgb24 ~path:"../../../test/test.ppm" in
  let hash = blurhash ~x_components:4 ~y_components:3 src in
  Alcotest.(check string) "test1" "LFE.@D9F01_2%L%MIVD*9Goe-;WB" hash;
  ()
14 Likes

Super useful. Thanks!

Is there any place where you are using it?

Yes! I originally wrote this library for my personal project Waq, an ActivityPub server. Waq tries to provide an API compatible with Mastodon, and Mastodon uses BlurHash for its thumbnails in its API, so I needed to calculate BlurHash in Waq.

5 Likes