![](/img/trans.png)
[英]How to decode this binary image file if none of image processing python libraries (Pillow and cv2) are unable to read?
[英]How to read Pillow image to cv2
我想在不保存bw
的情況下執行此操作,以避免每次保存時都必須讀取文件,並直接對其進行轉換,以便可以使用 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)
有任何想法嗎?
如果您的目標是將圖像轉換為灰度,然后對其進行遮罩,使亮度低於 80 的所有內容均為白色,而高於 80 的所有內容均為黑色,您可以在 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.