• Stars
    star
    194
  • Rank 200,219 (Top 4 %)
  • Language
    Go
  • License
    GNU General Publi...
  • Created over 5 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Blurhash implementation in pure Go (Decode/Encode)

go-blurhash Build Status Go Report Card codecov GoDoc

go-blurhash is a pure Go implementation of the BlurHash algorithm, which is used by Mastodon an other Fediverse software to implement a swift way of preloading placeholder images as well as hiding sensitive media. Read more about it here.

tl;dr: BlurHash is a compact representation of a placeholder for an image.

This library allows generating the BlurHash of a given image, as well as reconstructing a blurred version with specified dimensions from a given BlurHash.

This library is based on the following reference implementations:

BlurHash is written by Dag Ã…gren / Wolt.

Before After
Image alt text "LFE.@D9F01_2%L%MIVD*9Goe-;WB"
Hash "LFE.@D9F01_2%L%MIVD*9Goe-;WB" alt text

Installation

From source

go get -u github.com/buckket/go-blurhash

Usage

go-blurhash exports three functions:

func blurhash.Encode(xComponents, yComponents int, rgba image.Image) (string, error)
func blurhash.Decode(hash string, width, height, punch int) (image.Image, error)
func blurhash.Components(hash string) (xComponents, yComponents int, err error)

Here’s a simple demonstration. Check pkg.go.dev for the full documentation.

package main

import (
	"fmt"
	"github.com/buckket/go-blurhash"
	"image/png"
	"os"
)

func main() {
	// Generate the BlurHash for a given image
	imageFile, _ := os.Open("test.png")
	loadedImage, err := png.Decode(imageFile)
	str, _ := blurhash.Encode(4, 3, loadedImage)
	if err != nil {
		// Handle errors
	}
	fmt.Printf("Hash: %s\n", str)

	// Generate an image for a given BlurHash
	// Width will be 300px and Height will be 500px
	// Punch specifies the contrasts and defaults to 1
	img, err := blurhash.Decode(str, 300, 500, 1)
	if err != nil {
		// Handle errors
	}
	f, _ := os.Create("test_blur.png")
	_ = png.Encode(f, img)
	
	// Get the x and y components used for encoding a given BlurHash
	x, y, err := blurhash.Components("LFE.@D9F01_2%L%MIVD*9Goe-;WB")
	if err != nil {
		// Handle errors
	}
	fmt.Printf("xComponents: %d, yComponents: %d", x, y)
}

Limitations

  • Presumably a bit slower than the C implementation (TODO: Benchmarks)

Notes

  • As mentioned here, it’s best to generate very small images (~32x32px) via BlurHash and scale them up to the desired dimensions afterwards for optimal performance.
  • Since #2 we diverted from the reference implementation by memorizing sRGBtoLinear values, thus increasing encoding speed at the cost of higher memory usage.
  • Starting with v1.1.0 the signature of blurhash.Encode() has changed slightly (see #3).

License

GNU GPLv3+

More Repositories

1

twtxt

Decentralised, minimalist microblogging service for hackers.
Python
1,920
star
2

ofgrab

Archive media from onlyfans.com (deprecated)
Python
26
star
3

twtlmirror

Mirrors a Twitter home timeline to one Mastodon account
Go
9
star
4

kindle-abfahrt

Public transportation depatures for the Kindle Paperwhite
Go
7
star
5

rfk

Radio freies Krautchan MK III (deprecated)
PHP
7
star
6

erica

Supybot/Limnoria plugins
Python
6
star
7

pkgproxy

A caching proxy server specifically designed for caching Arch GNU/Linux packages for pacman
Go
5
star
8

subkacker

Parse and search .srt files to ease word mixing efforts for YouTube poops
Go
4
star
9

trk

Mirror
Go
3
star
10

twtstats

graphing fun
Python
3
star
11

go-mastopush

Decrypt and parse Web Push Notifications sent by Mastodon
Go
3
star
12

vlr-matchups

Who’s playing whom?
Jupyter Notebook
3
star
13

adventofcode

My solutions for http://adventofcode.com/
Go
2
star
14

fjw

Post von Wagner (RSS Feed)
Go
2
star
15

krautdmp

Krautchan Dumper
Perl
2
star
16

mumbi

Control mumbi m-FS300 remote power sockets with BeagleBone, Raspberry Pi and others
C
2
star
17

mtg-deck-editor

Magic: The Gathering – Deck Editor (fork)
Python
2
star
18

hundekacke

Source files for my YouTube Poop videos
Shell
2
star
19

twtblocks

Scrape the Twiter API to find accounts you’re blocked from
Go
2
star
20

sedeprot

A self-enforcing protocol for collaborative decision-making
Python
1
star
21

spacenews

Das könnte ich mir stundenlang durchlesen
Python
1
star
22

weeaboo-faces

Just another emoji collection
1
star
23

countryballs

ALL YOUR BALLS ARE BELONG TO US
1
star
24

sclock

Battery powered driver for slave clocks, build around the <strike>ATmega328P</strike> ATtiny814
C
1
star
25

dotfiles

These are my dotfiles
Shell
1
star
26

omnibooru

Dump various *boorus (deprecated)
Perl
1
star
27

pyKC

Feed your snake with your favourite imageboard.
Python
1
star
28

nnsb

Zeitgeschichtliches Archiv (keine Entwicklung)
1
star
29

hl2info

Query a HL2 based server (pls ignore)
C
1
star
30

hwpuzzle

An AoC inspired AVR experimentation board
HTML
1
star
31

supybot-supytube

YouTube/Vimeo video information. For Supybot/Limnoria.
Python
1
star
32

LG-BVG

Garantiert unfreundlich und mit weniger Grüßen, dank maschinellem Lernen.
Python
1
star
33

der_gentleman

Instagram comment scraper gone Twitter bot
Go
1
star
34

altlast.sh

remove uninteresting twitter followings
Shell
1
star
35

docker-htwtxt

1
star
36

ffmpeg-fun

Shell
1
star