Update app.py
Browse files
app.py
CHANGED
|
@@ -9,35 +9,31 @@ import shutil
|
|
| 9 |
|
| 10 |
def process_image(img, strength, upscale=False):
|
| 11 |
if upscale:
|
| 12 |
-
# Resize to 2x using LANCZOS for high-quality smoothing
|
| 13 |
new_size = (img.width * 2, img.height * 2)
|
| 14 |
img = img.resize(new_size, resample=Image.LANCZOS)
|
| 15 |
|
| 16 |
-
# Convert to grayscale and normalize
|
| 17 |
img_cv = np.array(img.convert('L'))
|
| 18 |
img_cv = img_cv.astype(np.float32) / 255.0
|
| 19 |
|
| 20 |
-
# Sobel filters for gradients
|
| 21 |
sobelx = cv2.Sobel(img_cv, cv2.CV_64F, 1, 0, ksize=3)
|
| 22 |
sobely = cv2.Sobel(img_cv, cv2.CV_64F, 0, 1, ksize=3)
|
| 23 |
|
| 24 |
-
|
| 25 |
nx = -sobelx * strength
|
| 26 |
ny = -sobely * strength
|
| 27 |
nz = np.ones_like(img_cv)
|
| 28 |
|
| 29 |
-
|
| 30 |
length = np.sqrt(nx**2 + ny**2 + nz**2)
|
| 31 |
nx /= length
|
| 32 |
ny /= length
|
| 33 |
nz /= length
|
| 34 |
|
| 35 |
-
|
| 36 |
nx = (nx + 1) / 2 * 255
|
| 37 |
ny = (ny + 1) / 2 * 255
|
| 38 |
nz = (nz + 1) / 2 * 255
|
| 39 |
|
| 40 |
-
# Merge to RGB (R=X, G=Y, B=Z)
|
| 41 |
normal_map = np.stack((nx, ny, nz), axis=-1).astype(np.uint8)
|
| 42 |
return Image.fromarray(normal_map)
|
| 43 |
|
|
|
|
| 9 |
|
| 10 |
def process_image(img, strength, upscale=False):
|
| 11 |
if upscale:
|
|
|
|
| 12 |
new_size = (img.width * 2, img.height * 2)
|
| 13 |
img = img.resize(new_size, resample=Image.LANCZOS)
|
| 14 |
|
|
|
|
| 15 |
img_cv = np.array(img.convert('L'))
|
| 16 |
img_cv = img_cv.astype(np.float32) / 255.0
|
| 17 |
|
|
|
|
| 18 |
sobelx = cv2.Sobel(img_cv, cv2.CV_64F, 1, 0, ksize=3)
|
| 19 |
sobely = cv2.Sobel(img_cv, cv2.CV_64F, 0, 1, ksize=3)
|
| 20 |
|
| 21 |
+
|
| 22 |
nx = -sobelx * strength
|
| 23 |
ny = -sobely * strength
|
| 24 |
nz = np.ones_like(img_cv)
|
| 25 |
|
| 26 |
+
|
| 27 |
length = np.sqrt(nx**2 + ny**2 + nz**2)
|
| 28 |
nx /= length
|
| 29 |
ny /= length
|
| 30 |
nz /= length
|
| 31 |
|
| 32 |
+
|
| 33 |
nx = (nx + 1) / 2 * 255
|
| 34 |
ny = (ny + 1) / 2 * 255
|
| 35 |
nz = (nz + 1) / 2 * 255
|
| 36 |
|
|
|
|
| 37 |
normal_map = np.stack((nx, ny, nz), axis=-1).astype(np.uint8)
|
| 38 |
return Image.fromarray(normal_map)
|
| 39 |
|