You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
674 B

import cv2
import numpy as np
from PIL import Image
def convert(filepath, savepath):
gray= cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
h, w = gray.shape[:2]
out = np.zeros(gray.shape, np.uint8)
for i in range(h):
for j in range(w):
pix = gray[i][j]
if pix < 50:
out[i][j] = 0
elif pix < 200:
out[i][j] = 0.25 * pix
else:
out[i][j] = 255
out = np.around(out)
out = out.astype(np.uint8)
cv2.imwrite(savepath, out)
if __name__ == '__main__':
filepath = 'pic/image1.jpg'
savepath = 'pic/convert.jpg'
convert(filepath, savepath)