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
fc99fa56
Commit
fc99fa56
authored
Aug 17, 2023
by
comfyanonymous
Browse files
Add node to scale image to a total amount of pixels keeping aspect.
parent
eb5c991a
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
0 deletions
+28
-0
comfy_extras/nodes_post_processing.py
comfy_extras/nodes_post_processing.py
+28
-0
No files found.
comfy_extras/nodes_post_processing.py
View file @
fc99fa56
...
@@ -2,6 +2,7 @@ import numpy as np
...
@@ -2,6 +2,7 @@ import numpy as np
import
torch
import
torch
import
torch.nn.functional
as
F
import
torch.nn.functional
as
F
from
PIL
import
Image
from
PIL
import
Image
import
math
import
comfy.utils
import
comfy.utils
...
@@ -209,9 +210,36 @@ class Sharpen:
...
@@ -209,9 +210,36 @@ class Sharpen:
return
(
result
,)
return
(
result
,)
class
ImageScaleToTotalPixels
:
upscale_methods
=
[
"nearest-exact"
,
"bilinear"
,
"area"
,
"bicubic"
]
crop_methods
=
[
"disabled"
,
"center"
]
@
classmethod
def
INPUT_TYPES
(
s
):
return
{
"required"
:
{
"image"
:
(
"IMAGE"
,),
"upscale_method"
:
(
s
.
upscale_methods
,),
"megapixels"
:
(
"FLOAT"
,
{
"default"
:
1.0
,
"min"
:
0.01
,
"max"
:
16.0
,
"step"
:
0.01
}),
}}
RETURN_TYPES
=
(
"IMAGE"
,)
FUNCTION
=
"upscale"
CATEGORY
=
"image/upscaling"
def
upscale
(
self
,
image
,
upscale_method
,
megapixels
):
samples
=
image
.
movedim
(
-
1
,
1
)
total
=
int
(
megapixels
*
1024
*
1024
)
scale_by
=
math
.
sqrt
(
total
/
(
samples
.
shape
[
3
]
*
samples
.
shape
[
2
]))
width
=
round
(
samples
.
shape
[
3
]
*
scale_by
)
height
=
round
(
samples
.
shape
[
2
]
*
scale_by
)
s
=
comfy
.
utils
.
common_upscale
(
samples
,
width
,
height
,
upscale_method
,
"disabled"
)
s
=
s
.
movedim
(
1
,
-
1
)
return
(
s
,)
NODE_CLASS_MAPPINGS
=
{
NODE_CLASS_MAPPINGS
=
{
"ImageBlend"
:
Blend
,
"ImageBlend"
:
Blend
,
"ImageBlur"
:
Blur
,
"ImageBlur"
:
Blur
,
"ImageQuantize"
:
Quantize
,
"ImageQuantize"
:
Quantize
,
"ImageSharpen"
:
Sharpen
,
"ImageSharpen"
:
Sharpen
,
"ImageScaleToTotalPixels"
:
ImageScaleToTotalPixels
,
}
}
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