main4.py 606 B

12345678910111213141516171819202122
  1. import cv2
  2. def edge(filename):
  3. image = cv2.imread(filename)
  4. # 将BGR图像转换为灰度
  5. gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  6. # 图像反转
  7. inverted_image = 255 - gray_image
  8. blurred = cv2.GaussianBlur(inverted_image, (21, 21), 0)
  9. inverted_blurred = 255 - blurred
  10. pencil_sketch = cv2.divide(gray_image, inverted_blurred, scale=256.0)
  11. # cv2.imshow("原图", image)
  12. # cv2.imshow("铅笔素描", pencil_sketch)
  13. # cv2.waitKey(0)
  14. cv2.imwrite('edges/chbaobao.jpeg', pencil_sketch)
  15. if __name__ == "__main__":
  16. edge('images/chbaobao.jpeg')