Image Processing useful code snippets
from PIL import Image
image = Image .open ("path/to/image.jpg" )
resized_image = image .resize ((width , height ))
rotated_image = image .rotate (angle )
image .save ("path/to/image.jpg" )
pip install opencv-python
Read and display an image
import cv2
# Read an image
img = cv2 .imread ("image.jpg" )
# Display the image
cv2 .imshow ("Image" , img )
cv2 .waitKey (0 )
cv2 .destroyAllWindows ()
import cv2
# Read an image
img = cv2 .imread ("image.jpg" )
# Convert to grayscale
gray = cv2 .cvtColor (img , cv2 .COLOR_BGR2GRAY )
# Display the grayscale image
cv2 .imshow ("Gray Image" , gray )
cv2 .waitKey (0 )
cv2 .destroyAllWindows ()
import cv2
# Read an image
img = cv2 .imread ("image.jpg" )
# Resize the image
resized = cv2 .resize (img , (200 , 200 ))
# Display the resized image
cv2 .imshow ("Resized Image" , resized )
cv2 .waitKey (0 )
cv2 .destroyAllWindows ()
import cv2
# Read an image
img = cv2 .imread ("image.jpg" )
# Convert to grayscale
gray = cv2 .cvtColor (img , cv2 .COLOR_BGR2GRAY )
# Threshold the image
_ , thresh = cv2 .threshold (gray , 128 , 255 , cv2 .THRESH_BINARY )
# Display the thresholded image
cv2 .imshow ("Thresholded Image" , thresh )
cv2 .waitKey (0 )
cv2 .destroyAllWindows ()
from skimage import io
image = io .imread ("image.jpg" )
import matplotlib .pyplot as plt
plt .imshow (image )
plt .show ()
from skimage import transform
resized_image = transform .resize (image , (200 , 200 ))
from skimage import color
gray_image = color .rgb2gray (image )
from skimage import filters
edges = filters .sobel (gray_image )
from skimage import filters
threshold_value = filters .threshold_otsu (gray_image )
binary_image = gray_image > threshold_value