mirror of
https://github.com/kohya-ss/sd-scripts.git
synced 2026-04-09 06:45:09 +00:00
retain alpha in pil_resize backport #1619
This commit is contained in:
@@ -83,13 +83,20 @@ def setup_logging(args=None, log_level=None, reset=False):
|
|||||||
|
|
||||||
|
|
||||||
def pil_resize(image, size, interpolation=Image.LANCZOS):
|
def pil_resize(image, size, interpolation=Image.LANCZOS):
|
||||||
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
has_alpha = image.shape[2] == 4 if len(image.shape) == 3 else False
|
||||||
|
|
||||||
|
if has_alpha:
|
||||||
|
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGRA2RGBA))
|
||||||
|
else:
|
||||||
|
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
||||||
|
|
||||||
# use Pillow resize
|
|
||||||
resized_pil = pil_image.resize(size, interpolation)
|
resized_pil = pil_image.resize(size, interpolation)
|
||||||
|
|
||||||
# return cv2 image
|
# Convert back to cv2 format
|
||||||
resized_cv2 = cv2.cvtColor(np.array(resized_pil), cv2.COLOR_RGB2BGR)
|
if has_alpha:
|
||||||
|
resized_cv2 = cv2.cvtColor(np.array(resized_pil), cv2.COLOR_RGBA2BGRA)
|
||||||
|
else:
|
||||||
|
resized_cv2 = cv2.cvtColor(np.array(resized_pil), cv2.COLOR_RGB2BGR)
|
||||||
|
|
||||||
return resized_cv2
|
return resized_cv2
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user