Advertisement
Sovun

Untitled

Sep 2nd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. from PIL import Image
  4. import os
  5.  
  6. #constant values (paths)
  7. images = "images"
  8. annotations = "annotations/"
  9. output = "fragments"
  10.  
  11. def main():
  12.  
  13. pictures = os.listdir(images) #получаем картинки
  14. for picture in pictures:
  15. truePath = images+'/'+picture #полный путь до картинки
  16.  
  17. #получим координаты
  18. coords = open(annotations+picture[0:len(picture)-4]+'.txt', 'r')
  19. rules= coords.readlines() # получили наборы координат
  20. i = 0
  21. for rule in rules: #для каждого правила этой картиночки
  22. #ОБРЕЗКА
  23. im = Image.open(truePath)#открыли оригинал
  24. coords = rule.split(',') #получили координаты фигуры
  25. im = im.crop((int(coords[0]), int(coords[1]), int(coords[2]), int(coords[3])))#вырезали
  26. print((int(coords[0]), int(coords[1]), int(coords[2]), int(coords[3])))
  27. newFileName = '{0}{1}_{2}.png'.format(output+"/", picture[0:len(picture)-4], str(i)) #собрали новое название
  28. f = open(newFileName, 'w')
  29. print(newFileName)
  30. im.save(newFileName)
  31. f.close()
  32. #GRAYSCALE
  33. im = Image.open(truePath)#открыли оригинал
  34. coords = rule.split(',') #получили координаты фигуры
  35. im = im.crop((int(coords[0]), int(coords[1]), int(coords[2]), int(coords[3])))#вырезали
  36.  
  37. im = im.convert("L")
  38. newFileName = '{0}{1}_{2}_gray.png'.format(output+"_grayscale/", picture[0:len(picture)-4], str(i)) #собрали новое название
  39. f = open(newFileName, 'w')
  40. print(newFileName)
  41. im.save(newFileName)
  42. f.close()
  43. #FLIP
  44. im = Image.open(truePath)#открыли оригинал
  45. coords = rule.split(',') #получили координаты фигуры
  46. im = im.crop((int(coords[0]), int(coords[1]), int(coords[2]), int(coords[3])))#вырезали
  47. im = im.transpose(Image.FLIP_LEFT_RIGHT)
  48. newFileName = '{0}{1}_{2}_flip.png'.format(output+"_flip/", picture[0:len(picture)-4], str(i)) #собрали новое название
  49. f = open(newFileName, 'w')
  50. print(newFileName)
  51. im.save(newFileName)
  52. f.close()
  53. i+=1
  54. print(newFileName)
  55.  
  56. if __name__ == '__main__':
  57. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement