-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdan200-realesrgan-upscale.py
124 lines (100 loc) · 4.43 KB
/
dan200-realesrgan-upscale.py
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
#!/usr/bin/env python
# --------------------
# Real-ESRGAN Upscaler
# This plugin will upscale images using the Real-ESGRAN upscaling engine
# --------------------
from gimpfu import *
import math
import subprocess
import os
# The location of the Real-ESRGAN EXE
# Edit these variables if realesgran is installed in a different location
# Real-ESRGAN can be downloaded from: https://github.com/xinntao/Real-ESRGAN
REALESRGAN_EXE = "realesrgan-ncnn-vulkan.exe"
REALESRGAN_PATH = os.path.dirname(__file__) + "\\realesrgan\\" + REALESRGAN_EXE
def upscale_layer(img, layer, scale) :
if gimp.pdb.gimp_item_is_group(layer):
# Upscale children
numChildren, childIDs = gimp.pdb.gimp_item_get_children(layer)
for childID in childIDs:
child = gimp.Item.from_id(childID)
upscale_layer(img, child, scale)
else:
# Get layer box
x, y = gimp.pdb.gimp_drawable_offsets(layer)
w, h = layer.width, layer.height
if gimp.pdb.gimp_item_is_text_layer(layer):
# Scale the bounding box
gimp.pdb.gimp_layer_set_offsets(layer, x * scale, y * scale)
gimp.pdb.gimp_text_layer_resize(layer, w * scale, h * scale)
# Scale the text properties
fontSize, fontSizeUnit = gimp.pdb.gimp_text_layer_get_font_size(layer)
gimp.pdb.gimp_text_layer_set_font_size(layer, fontSize * scale, fontSizeUnit)
indent = gimp.pdb.gimp_text_layer_get_indent(layer)
gimp.pdb.gimp_text_layer_set_indent(layer, indent * scale)
lineSpacing = gimp.pdb.gimp_text_layer_get_line_spacing(layer)
gimp.pdb.gimp_text_layer_set_line_spacing(layer, lineSpacing * scale)
letterSpacing = gimp.pdb.gimp_text_layer_get_letter_spacing(layer)
gimp.pdb.gimp_text_layer_set_letter_spacing(layer, letterSpacing * scale)
else:
# Save the layer to disk
tempImagePath = gimp.pdb.gimp_temp_name("png")
gimp.pdb.file_png_save_defaults( img, layer, tempImagePath, tempImagePath )
# Upscale it
tempOutputImagePath = gimp.pdb.gimp_temp_name("png")
subprocess.call([REALESRGAN_EXE, "-i", tempImagePath, "-o", tempOutputImagePath, "-s", str(scale)], executable=REALESRGAN_PATH)
# Load it back in
tempImage = gimp.pdb.file_png_load(tempOutputImagePath, tempOutputImagePath)
tempLayer = tempImage.layers[0]
# Resize and clear the original layer
gimp.pdb.gimp_layer_resize(layer, w * scale, h * scale, 0, 0)
gimp.pdb.gimp_layer_set_offsets(layer, x * scale, y * scale)
gimp.pdb.gimp_edit_clear(layer)
# Copy the loaded image into the original layer
gimp.pdb.gimp_edit_copy(tempLayer)
fltLayer = gimp.pdb.gimp_edit_paste(layer, False)
gimp.pdb.gimp_floating_sel_anchor(fltLayer)
# Clean up
gimp.pdb.gimp_image_delete(tempImage)
os.remove(tempImagePath)
os.remove(tempOutputImagePath)
def dan200_realesrgan_upscale(img, layer, scale, currentLayerOnly) :
# Check realesrgan is installed
if not os.path.exists(REALESRGAN_PATH):
gimp.message("Could not find " + REALESRGAN_PATH + "\Real-ESRGAN can be downloaded from https://github.com/xinntao/Real-ESRGAN")
return
# Start
gimp.progress_init("Please wait ...")
gimp.pdb.gimp_image_undo_group_start(img)
try:
gimp.pdb.gimp_selection_none(img)
if currentLayerOnly:
# Resize the current layer
upscale_layer(img, layer, scale)
else:
# Resize each layer
for layer in img.layers:
upscale_layer(img, layer, scale)
# Resize the image
gimp.pdb.gimp_image_resize_to_layers(img)
except Exception as err:
gimp.message("Unexpected error: " + str(err))
# Finish
pdb.gimp_image_undo_group_end(img)
pdb.gimp_progress_end()
register(
"dan200-realesgran-upscale",
"Upscale images using the Real-ESGRAN upscaling engine",
"Upscale images using the Real-ESGRAN upscaling engine",
"Daniel Ratcliffe",
"Daniel Ratcliffe",
"2022",
"<Image>/Tools/Comic Tools/Real-ESRGAN Upscale",
"RGB*, GRAY*",
[
(PF_SPINNER, "scale", "Scale", 4, (2, 4, 1)),
(PF_BOOL, "currentLayerOnly", "Current Layer Only", False)
],
[],
dan200_realesrgan_upscale)
main()