简体   繁体   中英

How to read Pillow image to cv2

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM