trim.py 781 B

12345678910111213141516171819202122232425262728293031323334
  1. import os
  2. import sys
  3. import numpy as np
  4. from PIL import Image
  5. height_trim = 16
  6. width_trim = 20
  7. def trim(image):
  8. arr = np.asarray(image).tolist()
  9. arr = arr[height_trim:-height_trim]
  10. if len(arr[0]) == 215:
  11. arr = [row[(width_trim-1):-width_trim] for row in arr]
  12. else:
  13. arr = [row[width_trim:-width_trim] for row in arr]
  14. arr = np.asarray(arr)
  15. image_new = Image.fromarray(arr.astype(np.uint8))
  16. return image_new
  17. def main():
  18. dir_name = sys.argv[1]
  19. for i, file_name in enumerate(os.listdir('./' + dir_name)):
  20. path = './' + dir_name + '/' + file_name
  21. print(path)
  22. image = Image.open(path)
  23. image_new = trim(image)
  24. image.close()
  25. image_new.save(path)
  26. if __name__ == '__main__':
  27. main()