I have this problem that I have been working on recently and I have kind of reached a dead end as I am not sure why the image saved when re opened it is loaded as black image. I have (original) as my base image and I am trying to write a code to threshold the crack and make them appear in white while the rest of the image is black. I have managed to go as far as to create such image.
However, I cannot process it further because it consist of 2 channels and all cv2 and PIL libraries works with n = 1 and n = 3 channels images. I figured I can save the image and then loaded again into the system. However, when doing so the image is being loaded as black circle even though it appears that it was saved correctly in my system. The saved image (Saved) shows my image when opening it with windows photo, which doesn't look like what it seems when it is saved. However, when opening it with windows paint it looks correct and when I save it and loaded back into my python code everything works fine.
So my question is there a way to open each saved image in paint save it and close it so I can further process them, or is there another way around better than my method. The resulted image I am looking to have is shown in FinalImage.
I would appreciate if one can help me with that. For some reason I cant seem to upload the images and the code at the same time. I don't know why
import cv2
import numpy as np
from PIL import Image, ImageDraw
from PIL import Image, ImageOps#Read Image
img = cv2.imread('Input-Set/test_test_recon_Export0171.tif')# Convert into gray scale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# Image processing ( smoothing )
# Averaging
blur = cv2.blur(gray,(3,3))#Threshold The Image
ret,th1 = cv2.threshold(blur,145,255,cv2.THRESH_BINARY)
inverted = np.invert(th1)#Threshold The Background
ret,th2 = cv2.threshold(blur,1,255,cv2.THRESH_BINARY)
#inverted2 = np.invert(th2)#Mask Background over Threshold Image
masked = cv2.bitwise_and(inverted, inverted, mask=th2)#Crop AOI
height = 998
width = 1024
lum_img = Image.new('L', [height,width] , 0)draw = ImageDraw.Draw(lum_img)
draw.pieslice([(16,37), (960,985)], 0, 360, fill = 255, outline = "white")
img_arr =np.array(th1)
lum_img_arr =np.array(lum_img)final_img_arr = np.dstack((lum_img_arr,img_arr))
FinalImage = Image.fromarray(final_img_arr)
inverted2 = np.invert(FinalImage)
FinalImage2 = Image.fromarray(inverted2)Image.fromarray(inverted2).save('Output-Set/result.tif')
# img2 = Image.open("Output-Set/result.tif")
img2 = cv2.imread('Output-Set/result.tif')