Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
ComfyUI
Commits
23c5d178
Commit
23c5d178
authored
Oct 31, 2023
by
tsone
Browse files
Added Bayer dithering to Quantize node.
parent
c837a173
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
9 deletions
+37
-9
comfy_extras/nodes_post_processing.py
comfy_extras/nodes_post_processing.py
+37
-9
No files found.
comfy_extras/nodes_post_processing.py
View file @
23c5d178
...
...
@@ -126,7 +126,7 @@ class Quantize:
"max"
:
256
,
"step"
:
1
}),
"dither"
:
([
"none"
,
"floyd-steinberg"
],),
"dither"
:
([
"none"
,
"floyd-steinberg"
,
"bayer-2"
,
"bayer-4"
,
"bayer-8"
,
"bayer-16"
],),
},
}
...
...
@@ -135,19 +135,47 @@ class Quantize:
CATEGORY
=
"image/postprocessing"
def
quantize
(
self
,
image
:
torch
.
Tensor
,
colors
:
int
=
256
,
dither
:
str
=
"FLOYDSTEINBERG"
):
def
bayer
(
im
,
pal_im
,
order
):
def
normalized_bayer_matrix
(
n
):
if
n
==
0
:
return
np
.
zeros
((
1
,
1
),
"float32"
)
else
:
q
=
4
**
n
m
=
q
*
normalized_bayer_matrix
(
n
-
1
)
return
np
.
bmat
(((
m
-
1.5
,
m
+
0.5
),
(
m
+
1.5
,
m
-
0.5
)))
/
q
num_colors
=
len
(
pal_im
.
getpalette
())
//
3
spread
=
2
*
256
/
num_colors
bayer_n
=
int
(
math
.
log2
(
order
))
bayer_matrix
=
torch
.
from_numpy
(
spread
*
normalized_bayer_matrix
(
bayer_n
)
+
0.5
)
result
=
torch
.
from_numpy
(
np
.
array
(
im
).
astype
(
np
.
float32
))
tw
=
math
.
ceil
(
result
.
shape
[
0
]
/
bayer_matrix
.
shape
[
0
])
th
=
math
.
ceil
(
result
.
shape
[
1
]
/
bayer_matrix
.
shape
[
1
])
tiled_matrix
=
bayer_matrix
.
tile
(
tw
,
th
).
unsqueeze
(
-
1
)
result
.
add_
(
tiled_matrix
[:
result
.
shape
[
0
],:
result
.
shape
[
1
]]).
clamp_
(
0
,
255
)
result
=
result
.
to
(
dtype
=
torch
.
uint8
)
im
=
Image
.
fromarray
(
result
.
cpu
().
numpy
())
im
=
im
.
quantize
(
palette
=
pal_im
,
dither
=
Image
.
Dither
.
NONE
)
return
im
def
quantize
(
self
,
image
:
torch
.
Tensor
,
colors
:
int
,
dither
:
str
):
batch_size
,
height
,
width
,
_
=
image
.
shape
result
=
torch
.
zeros_like
(
image
)
dither_option
=
Image
.
Dither
.
FLOYDSTEINBERG
if
dither
==
"floyd-steinberg"
else
Image
.
Dither
.
NONE
for
b
in
range
(
batch_size
):
tensor_image
=
image
[
b
]
img
=
(
tensor_image
*
255
).
to
(
torch
.
uint8
).
numpy
()
p
i
l_im
age
=
Image
.
fromarray
(
img
,
mode
=
'RGB'
)
im
=
Image
.
fromarray
((
image
[
b
]
*
255
).
to
(
torch
.
uint8
).
numpy
(),
mode
=
'RGB'
)
p
a
l_im
=
im
.
quantize
(
colors
=
colors
)
# Required as described in https://github.com/python-pillow/Pillow/issues/5836
palette
=
pil_image
.
quantize
(
colors
=
colors
)
# Required as described in https://github.com/python-pillow/Pillow/issues/5836
quantized_image
=
pil_image
.
quantize
(
colors
=
colors
,
palette
=
palette
,
dither
=
dither_option
)
if
dither
==
"none"
:
quantized_image
=
im
.
quantize
(
palette
=
pal_im
,
dither
=
Image
.
Dither
.
NONE
)
elif
dither
==
"floyd-steinberg"
:
quantized_image
=
im
.
quantize
(
palette
=
pal_im
,
dither
=
Image
.
Dither
.
FLOYDSTEINBERG
)
elif
dither
.
startswith
(
"bayer"
):
order
=
int
(
dither
.
split
(
'-'
)[
-
1
])
quantized_image
=
Quantize
.
bayer
(
im
,
pal_im
,
order
)
quantized_array
=
torch
.
tensor
(
np
.
array
(
quantized_image
.
convert
(
"RGB"
))).
float
()
/
255
result
[
b
]
=
quantized_array
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment