hbldh / hitherdither Goto Github PK
View Code? Open in Web Editor NEWDithering algorithms for arbitrary palettes in PIL
License: MIT License
Dithering algorithms for arbitrary palettes in PIL
License: MIT License
There's no import statement for the cluster dot dithering module, so it's not accessible.
ordered/__init__.py
needs from . import cluster
added to it, which makes it accessible.
Currently grayscale and BW images does not work very well att all with the palette object. Needs to be implemented.
Trying to install but it is showing some errors!
C:\Users\akans>pip install git+https://www.github.com/hbldh/hitherdither
Collecting git+https://www.github.com/hbldh/hitherdither
Cloning https://www.github.com/hbldh/hitherdither to c:\users\akans\appdata\local\temp\pip-req-build-guyhoi6g
Running command git clone -q https://www.github.com/hbldh/hitherdither 'C:\Users\akans\AppData\Local\Temp\pip-req-build-guyhoi6g'
ERROR: Command errored out with exit status 1:
command: 'C:\Anaconda\python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\akans\\AppData\\Local\\Temp\\pip-req-build-guyhoi6g\\setup.py'"'"'; __file__='"'"'C:\\Users\\akans\\AppData\\Local\\Temp\\pip-req-build-guyhoi6g\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base pip-egg-info
cwd: C:\Users\akans\AppData\Local\Temp\pip-req-build-guyhoi6g\
Complete output (11 lines):
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Anaconda\lib\site-packages\setuptools\__init__.py", line 12, in <module>
from setuptools.extension import Extension
File "C:\Anaconda\lib\site-packages\setuptools\extension.py", line 7, in <module>
from setuptools.dist import _get_unpatched
File "C:\Anaconda\lib\site-packages\setuptools\dist.py", line 16, in <module>
import pkg_resources
File "C:\Anaconda\lib\site-packages\pkg_resources.py", line 1479, in <module>
register_loader_type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
As of Pillow 4.0.0 (2017-01-01), _makeself() was replaced by _new: https://github.com/python-pillow/Pillow/blob/master/CHANGES.rst#400-2017-01-01
Use Image._new() instead of _makeself() #2248 [homm]
Issue: python-pillow/Pillow#2248
Changing the two references in palette.py
from _makeself to _new makes hitherdither work with recent Pillow releases.
I attempted to create a palette with a PNG w/ a RGBA paltette converted to a JPEG w/ a RGB palette for dithering using the hitherdither.palette.Palette.create_by_median_cut(img)
method.
img = Image.open('phcpd.png')
img.load()
background = Image.new("RGB", img.size, (255, 255, 255))
background.paste(img, mask=img.split()[3])
background.save('ikasdf.jpg', "JPEG", quality=80)
img = Image.open('ikasdf.jpg')
palette = hitherdither.palette.Palette.create_by_median_cut(img)
img_dithered = hitherdither.ordered.bayer.bayer_dithering(img, palette, [256 / 4, 256 / 4, 256 / 4], order=8)
img_dithered.save("test.png")
print(type(img_dithered))
However, this error resulted:
Traceback (most recent call last):
File "C:/Users/megag/PycharmProjects/SerenityBot/main_PIL_test.py", line 227, in <module>
dither_test()
File "C:/Users/megag/PycharmProjects/SerenityBot/main_PIL_test.py", line 27, in dither_test
palette = hitherdither.palette.Palette.create_by_median_cut(img)
File "C:\Users\megag\PycharmProjects\SerenityBot\venv\lib\site-packages\hitherdither\palette.py", line 185, in create_by_median_cut
new_bins += median_cut(bin, dim)
File "C:\Users\megag\PycharmProjects\SerenityBot\venv\lib\site-packages\hitherdither\palette.py", line 168, in median_cut
mins = p.min(axis=0)
File "C:\Users\megag\PycharmProjects\SerenityBot\venv\lib\site-packages\numpy\core\_methods.py", line 44, in _amin
return umr_minimum(a, axis, None, out, keepdims, initial, where)
ValueError: zero-size array to reduction operation minimum which has no identity
A link to the image is available here: phcpd.png.
I've noticed that images produced with hitherdither are slightly different than those produced with other programs using the same algorithms.
I tested this using Floyd-Steinberg on a solid 50% gray image, which should produce a uniform checkerboard pattern.
Input | Output | Expected (from PIL) |
---|---|---|
I've found that by rounding when distributing the quantization error, the above image renders correctly, while others are closer but still different.
ni[yn, xn] += np.round(quantization_error * diffusion_coefficient)
Maybe that's as good as it's gonna get.
This should be available via pypi.
Without an easy dependency option, I would have just lifted the needed code but, it's a bit of a labyrinth in that needed functions are found bouncing around init files etc. Needs to be easier than just writing the functions myself.
hitherdither has a matrix for the Stevenson-Arce error diffusion dither, as see here:
hitherdither/hitherdither/diffusion.py
Lines 97 to 111 in 27c9392
As noted here:
Stevenson-Arce is designed to work on hexagonal arrays and will obviously give ugly results on regular square pixel images.
And this source describes Stevenson-Arce like this:
Stevenson-Arce:
* . 32
12 . 26 . 30 . 16
. 12 . 26 . 12 .
5 . 12 . 12 . 5 / 200
This obviously shows a hexagonal arrangement.
So where did you get your numbers from, or how did you derive them? Do you have any example pictures of what this looks like?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.