A Clojure implementation of Blurhash.
Blurhash is an algorithm by Dag Ågren of Wolt that decodes an image to a very compact (~ 20-30 bytes) ASCII string representation, which can be then decoded into a blurred placeholder image. See the main repo for the rationale and details.
The encode->hash->decode cycle looks something like this:
→ UIGuXeS@x[xX_MORbuoy?uNGM{nTNHMzIVnn
→
The Clojure implementation is written in CLJC files, so that they can be used from Clojure and ClojureScript code alike.
You can encode an image to a blurhash using the function blurhash.encode/encode
. It takes an image as an RGB matrix, currently represented as nested native vectors. The blurhash.core
namespace contains (Clojure-specific) functions to convert an image file into a matrix, so you can do something like this:
(ns my-namespace
(:require [blurhash.core :refer [file->pixels]
[blurhash.encode :refer [encode]]]))
(def image
(file->pixels "./resources/example.jpg"))
(encode image)
=> "UIGuXeS@x[xX_MORbuoy?uNGM{nTNHMzIVnn"
Here's an example of how to decode a blurhash into a placeholder image:
(ns my-namespace
(:require [blurhash.core :refer [pixels->file]
[blurhash.decode :refer [decode]]]))
(def blurred-image
(decode "UIGuXeS@x[xX_MORbuoy?uNGM{nTNHMzIVnn" 300 236))
(pixels->file blurred-image "blurred-image.jpg")
Copyright (c) 2019 .core
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Can you improve this documentation? These fine people already did:
Tuomo Virolainen & Esko SuomiEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close