VizHash.py: visual hashes in Python
It converts a string to a hash image (almost unique) to give possibility to easily compare with other string hashes. It can be really useful to compare passwords or to identify people from their IP without displaying it.
This implementation results are quite similar to those of VizHash GD and VizHash.js but differ on a certain number of points (I'm working to reduce differences, any help is welcomed!).
VizHash.py is distributed under the zlib/libpng license.
Please note VizHash.py requires the Pillow module.
First usage is with the command line:
$ python vizhash/vizhash.py --size=128x128 "your string" > img.png
You should see appearing a new image which is the hash visualisation of
If module is installed in your default Python path, you can also call the module as following:
$ python -m vizhash.vizhash "your string" > img.png
Second usage is in your source file and it is quite easy:
import vizhash.vizhash img = vizhash.vizhash.draw('your string', size=(128, 128)) fp = open('img.png', 'w') img.save(fp)
Another usage is to use it in your websites / web applications. For instance, with Django, simply add in your
import vizhash.vizhash def vizhash_view(request, string, size=None): try: size = int(size) except TypeError: size = 128 image = vizhash.vizhash.draw(string, (size, size)) response = HttpResponse(content_type="image/png") image.save(response, 'PNG') return response
urlpatterns = patterns( […] url(r'^vizhash/(?P<string>\w+)/((?P<size>\d+)/)?$', views.vizhash_view, name='vizhash'), […] )
Note by this way you reveal your string in the URL and it may not what you want. Feel free to adapt these examples to your needs!
There are some points we can improve in VizHash.py:
- Make resulting images closer than those of other implementations of VizHash.
- Make images more beautiful.
- Add unit tests (because for now it is only a quick refactored and commented piece of code)
- Add it on PyPi
- Make it not depending on Pillow which is kinda overkill (but with good performances and very helpul functions)
Please use the GitHub bugtracker to signal bugs or ideas of improvement.