from PIL import Image import numpy as np import matplotlib.pyplot as plt Load the image…
- User
from PIL import Image import numpy as np import matplotlib.pyplot as plt
Load the image
image_path = '/Users/abhi/Downloads/Zerodur/Sample 1_Rough.tif' image = Image.open(image_path)
Convert to grayscale
gray_image = image.convert("L")
Perform binarization
threshold = 180 # Set threshold level binary_image = gray_image.point(lambda x: 255 if x > threshold else 0, mode='1')
Increase DPI for higher resolution output
fig, axes = plt.subplots(1, 2, figsize=(12, 6), dpi=150)
axes[0].imshow(gray_image, cmap='gray') axes[0].set_title('Grayscale Image') axes[0].axis('off')
axes[1].imshow(binary_image, cmap='gray') axes[1].set_title('Binary Image') axes[1].axis('off')
plt.show()
show original image and Binary image in the output, not the grascale image
- Assistant
To display the original image alongside the binary image (instead of the grayscale image), you just need to modify the first subplot to show the original image rather than the grayscale one.
Here's your updated code snippet with this change applied:
from PIL import Image import numpy as np import matplotlib.pyplot as plt # Load the image image_path = '/Users/abhi/Downloads/Zerodur/Sample 1_Rough.tif' image = Image.open(image_path) # Convert to grayscale gray_image = image.convert("L") # Perform binarization threshold = 180 # Set threshold level binary_image = gray_image.point(lambda x: 255 if x > threshold else 0, mode='1') # Increase DPI for higher resolution output fig, axes = plt.subplots(1, 2, figsize=(12, 6), dpi=150) # Show original image (not grayscale) axes[0].imshow(image) axes[0].set_title('Original Image') axes[0].axis('off') # Show binary image axes[1].imshow(binary_image, cmap='gray') axes[1].set_title('Binary Image') axes[1].axis('off') plt.show()Explanation:
- The line
axes[0].imshow(gray_image, cmap='gray')is replaced byaxes[0].imshow(image)to show the original color image. - The binary image display remains unchanged.
This will show the original image on the left and the binary image on the right. Let me know if you want to keep grayscale as well or have any further modifications!
- The line