-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathDJZImageScaleToTotalPixels.py
More file actions
62 lines (51 loc) · 1.99 KB
/
DJZImageScaleToTotalPixels.py
File metadata and controls
62 lines (51 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import math
import comfy.utils
class DJZImageScaleToTotalPixels:
upscale_methods = ["nearest-exact", "bilinear", "area", "bicubic", "lanczos"]
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
}),
"downscale_factor": ("INT", {
"default": 64,
"min": 1,
"max": 512,
"step": 1
}),
}
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "upscale"
CATEGORY = "image/upscaling"
def upscale(self, image, upscale_method, megapixels, downscale_factor):
samples = image.movedim(-1, 1)
total = int(megapixels * 1024 * 1024)
# Calculate initial scale factor
scale_by = math.sqrt(total / (samples.shape[3] * samples.shape[2]))
# Calculate target dimensions
target_width = samples.shape[3] * scale_by
target_height = samples.shape[2] * scale_by
# Round to nearest multiple of downscale_factor to ensure divisibility
width = round(target_width / downscale_factor) * downscale_factor
height = round(target_height / downscale_factor) * downscale_factor
# Ensure minimum dimensions (at least 1 * downscale_factor)
width = max(width, downscale_factor)
height = max(height, downscale_factor)
s = comfy.utils.common_upscale(samples, width, height, upscale_method, "disabled")
s = s.movedim(1, -1)
return (s,)
NODE_CLASS_MAPPINGS = {
"DJZImageScaleToTotalPixels": DJZImageScaleToTotalPixels,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"DJZImageScaleToTotalPixels": "DJZ Scale to Total Pixels",
}