BITMAP.py 627 B

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