-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_resize_plus.py
More file actions
171 lines (118 loc) · 4.96 KB
/
image_resize_plus.py
File metadata and controls
171 lines (118 loc) · 4.96 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from typing import Literal
from PIL import Image
from invokeai.invocation_api import (
BaseInvocation,
InputField,
invocation,
InvocationContext,
ImageField,
ImageOutput
)
PIL_RESAMPLING_MODES = Literal[
"nearest",
"box",
"bilinear",
"hamming",
"bicubic",
"lanczos",
]
PIL_RESAMPLING_MAP = {
"nearest": Image.Resampling.NEAREST,
"box": Image.Resampling.BOX,
"bilinear": Image.Resampling.BILINEAR,
"hamming": Image.Resampling.HAMMING,
"bicubic": Image.Resampling.BICUBIC,
"lanczos": Image.Resampling.LANCZOS,
}
RESIZE_MODES = Literal[
"fill",
"stretch",
"fit",
"center",
"crop",
]
@invocation(
"image_resize_plus",
title="Image Resize Plus",
tags=["image", "resize"],
category="image",
version="1.1.0",
)
class ResizeImagePlusInvocation(BaseInvocation):
"""Resizes an image to specific dimensions"""
image: ImageField = InputField(default=None, description="Image to be resize")
width: int = InputField(default=512., description="The width to resize to (px)")
height: int = InputField(default=512., description="The height to resize to (px)")
resample_mode: PIL_RESAMPLING_MODES = InputField(default="bicubic", description="The resampling mode")
resize_mod: RESIZE_MODES = InputField(default="fit", description="The resize mode")
multiple: int = InputField(default=0, description="If set, rounds width and height to nearest multiple of this value")
def invoke(self, context: InvocationContext) -> ImageOutput:
RESIZE_MODES_MAP = {
"fill": self.fill,
"stretch": self.stretch,
"fit": self.fit,
"center": self.center,
"crop": self.crop,
}
image = context.images.get_pil(self.image.image_name)
resample_mode = PIL_RESAMPLING_MAP[self.resample_mode]
image_resize = RESIZE_MODES_MAP[self.resize_mod]
image_out = image_resize(resample_mode, image)
image_dto = context.images.save(image=image_out)
return ImageOutput.build(image_dto)
def __round(self, size: tuple[int, int]) -> tuple[int, int]:
width, height = size
if self.multiple > 0:
def r(v: int) -> int:
return int(round(v / self.multiple) * self.multiple)
return r(width), r(height)
return int(width), int(height)
def fill(self, resample_mode, image):
original_width, original_height = image.size
width_ratio = self.width / original_width
height_ratio = self.height / original_height
resize_ratio = max(width_ratio, height_ratio)
new_width = int(original_width * resize_ratio)
new_height = int(original_height * resize_ratio)
new_width, new_height = self.__round((new_width, new_height))
resized_image = image.resize((new_width, new_height), resample_mode)
final_image = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0))
final_image.paste(resized_image, ((self.width - new_width) // 2, (self.height - new_height) // 2))
return final_image
def stretch(self, resample_mode, image):
final_image = image.resize((self.width, self.height), resample_mode)
return final_image
def fit(self, resample_mode, image):
original_width, original_height = image.size
resize_ratio = original_width / original_height
width = self.width
height = self.height
if (width / height) < resize_ratio:
height = int(width / resize_ratio)
else:
width = int(height * resize_ratio)
width, height = self.__round((width, height))
final_image = image.resize((width, height), resample_mode)
return final_image
def center(self, resample_mode, image):
original_width, original_height = image.size
width_ratio = self.width / original_width
height_ratio = self.height / original_height
scale_ratio = min(width_ratio, height_ratio)
new_width = int(scale_ratio * original_width)
new_height = int(scale_ratio * original_height)
new_width, new_height = self.__round((new_width, new_height))
resized_image = image.resize((new_width, new_height), resample_mode)
final_image = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0))
x = (self.width - new_width) // 2
y = (self.height - new_height) // 2
final_image.paste(resized_image, (x, y))
return final_image
def crop(self, resample_mode, image):
original_width, original_height = image.size
self.width, self.height = self.__round((self.width, self.height))
final_image = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0))
x = (self.width - original_width) // 2
y = (self.height - original_height) // 2
final_image.paste(image, (x, y))
return final_image