Reputation: 457
I want to do this without saving bw
to avoid having to read the file every time I save, and transform it directly so that it can be read with opencv.
#Image Pillow open
img = Image.open('/content/drive/My Drive/TESTING/Placas_detectadas/HCPD24.png')
gray = img.convert('L')
bw = gray.point(lambda x: 0 if x<80 else 255, '1')
bw.save('xd.png')
#Imagen from opencv
im = cv2.imread('/content/xd.png')
im = ~im
cv2_imshow(im)
cv2.imwrite('wena.png',im)
Any ideas?
Upvotes: 0
Views: 510
Reputation: 168863
If your goal is to transform the image into grayscale, then mask it so everything less bright than 80 is white, and everything above that is black, you can do it all rather quickly in OpenCV:
import cv2
im = cv2.imread('a.jpg')
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) # Grayscale image
_, im = cv2.threshold(im, 80, 255, cv2.THRESH_BINARY_INV) # Threshold at brightness 80 and invert
cv2.imwrite('converted.png', im) # Write output
Upvotes: 2