-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
182 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 16, | ||
"id": "83bb7943", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import cv2\n", | ||
"import numpy as np\n", | ||
"\n", | ||
"def selective_blur(image_path, outputPath, blur_strength=10, focus_area_fraction=1):\n", | ||
" # Load the image\n", | ||
" image = cv2.imread(image_path)\n", | ||
" \n", | ||
" if image is None:\n", | ||
" print(\"Error loading image\")\n", | ||
" return\n", | ||
" \n", | ||
" # Ensure blur_strength is an odd number\n", | ||
" if blur_strength % 2 == 0:\n", | ||
" blur_strength += 1\n", | ||
"\n", | ||
" # Dimensions of the image\n", | ||
" height, width = image.shape[:2]\n", | ||
" \n", | ||
" # Calculate the size of the central area to keep in focus\n", | ||
" central_width = int(width * focus_area_fraction)\n", | ||
" central_height = int(height * focus_area_fraction)\n", | ||
" \n", | ||
" # Calculate the starting point of the central region\n", | ||
" start_x = (width - central_width) // 2\n", | ||
" start_y = (height - central_height) // 2\n", | ||
" \n", | ||
" # Region of Interest (ROI) in the center of the image\n", | ||
" center_region = image[start_y:start_y+central_height, start_x:start_x+central_width]\n", | ||
" \n", | ||
" # Apply Gaussian Blur to the center region\n", | ||
" blurred_center = cv2.GaussianBlur(center_region, (blur_strength, blur_strength), 0)\n", | ||
" \n", | ||
" # Replace the center region of the original image with blurred version\n", | ||
" image[start_y:start_y+central_height, start_x:start_x+central_width] = blurred_center\n", | ||
" # Display the original and modified images\n", | ||
" #cv2.imshow(\"Blurred Center Image\", image)\n", | ||
" #cv2.waitKey(0)\n", | ||
" #cv2.destroyAllWindows()\n", | ||
" \n", | ||
" # Save the result if desired\n", | ||
" cv2.imwrite(outputPath, image)\n", | ||
"\n", | ||
"# Usage\n", | ||
"inputPath = \"/Users/rai/Documents/GitHub/html5up-dimension/images/bg_original.jpg\"\n", | ||
"outputPath = \"/Users/rai/Documents/GitHub/html5up-dimension/images/bg.jpg\"\n", | ||
"image = selective_blur(inputPath, outputPath)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "470ebab4", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import cv2\n", | ||
"import numpy as np\n", | ||
"\n", | ||
"def radial_gradient_blur(image_path, outputPath):\n", | ||
" # Load the image\n", | ||
" image = cv2.imread(image_path)\n", | ||
" \n", | ||
" if image is None:\n", | ||
" print(\"Error loading image\")\n", | ||
" return\n", | ||
"\n", | ||
" # Dimensions of the image\n", | ||
" height, width = image.shape[:2]\n", | ||
" center_x, center_y = width // 2, height // 2\n", | ||
"\n", | ||
" # Create an empty output image\n", | ||
" blurred_image = np.zeros_like(image)\n", | ||
"\n", | ||
" # Maximum blur radius\n", | ||
" max_radius = int(np.sqrt(center_x**2 + center_y**2))\n", | ||
"\n", | ||
" # Create radial blur effect\n", | ||
" for y in range(height):\n", | ||
" for x in range(width):\n", | ||
" # Calculate distance to the center\n", | ||
" distance = int(np.sqrt((center_x - x)**2 + (center_y - y)**2))\n", | ||
" blur_amount = int(20 * (distance / max_radius)) # Change '20' to adjust max blur strength\n", | ||
"\n", | ||
" # Ensure blur_amount is odd and at least 1\n", | ||
" blur_amount = max(1, blur_amount)\n", | ||
" if blur_amount % 2 == 0:\n", | ||
" blur_amount += 1\n", | ||
"\n", | ||
" # Blur the pixel based on distance to the center\n", | ||
" region = image[max(0, y - blur_amount // 2):min(height, y + blur_amount // 2 + 1),\n", | ||
" max(0, x - blur_amount // 2):min(width, x + blur_amount // 2 + 1)]\n", | ||
" blurred_pixel = cv2.GaussianBlur(region, (blur_amount, blur_amount), 0)\n", | ||
" blurred_image[y, x] = blurred_pixel[blur_amount // 2, blur_amount // 2]\n", | ||
"\n", | ||
" # Display the original and modified images\n", | ||
" cv2.imshow(\"Original Image\", image)\n", | ||
" #cv2.imshow(\"Radially Blurred Image\", blurred_image)\n", | ||
" #cv2.waitKey(0)\n", | ||
" #cv2.destroyAllWindows()\n", | ||
"\n", | ||
" # Save the result if desired\n", | ||
" cv2.imwrite(outputPath, image)\n", | ||
"\n", | ||
"# Usage\n", | ||
"inputPath = \"/Users/rai/Documents/GitHub/html5up-dimension/images/bg_original.jpg\"\n", | ||
"outputPath = \"/Users/rai/Documents/GitHub/html5up-dimension/images/bg.jpg\"\n", | ||
"image = radial_gradient_blur(inputPath,outputPath)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "6df86bb8", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from PIL import Image\n", | ||
"\n", | ||
"inputPath = \"/Users/rai/Documents/GitHub/rs3966.github.io/images/theLabPoster.jpg\"\n", | ||
"outputPath = \"/Users/rai/Documents/GitHub/rs3966.github.io/images/theLabPoster_sized.jpg\"\n", | ||
"fraction = 0.5\n", | ||
"# Open an image file\n", | ||
"with Image.open(inputPath) as img:\n", | ||
" # Calculate the new size, 75% of the original size\n", | ||
" new_width = int(img.width * fraction)\n", | ||
" new_height = int(img.height * fraction)\n", | ||
" \n", | ||
" # Resize the image\n", | ||
" resized_img = img.resize((new_width, new_height), Image.ANTIALIAS)\n", | ||
" \n", | ||
" # Save the resized image\n", | ||
" resized_img.save(outputPath)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "4fdcdb95", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.12" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters