BITMAP.py 540 B

123456789101112131415161718192021222324252627
  1. from array import array
  2. import numpy as np
  3. import cv2 as cv
  4. # 加载图片到灰度图像
  5. img = cv.imread('C:\TSP\png.png', cv.IMREAD_GRAYSCALE)
  6. h,w = img.shape
  7. print(h,w)
  8. print(img.size)
  9. f = open('C:\TSP\BITMAP', 'w')
  10. line = '%d %d %d\n' % (h,w, img.size)
  11. f.write(line)
  12. lst = ['0'] * w
  13. for m in range(h):
  14. for n in range(w):
  15. if img[m,n] == 0:
  16. lst[n] = '1'
  17. else:
  18. lst[n] = '0'
  19. line = ' '.join(lst)
  20. f.write(line+'\n')
  21. # cv.imshow('image',img)
  22. # cv.waitKey(5000)
  23. # cv.destroyAllWindows()