Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- from PIL import Image
- import os
- #constant values (paths)
- images = "images"
- annotations = "annotations/"
- output = "fragments"
- def main():
- pictures = os.listdir(images) #получаем картинки
- for picture in pictures:
- truePath = images+'/'+picture #полный путь до картинки
- #получим координаты
- coords = open(annotations+picture[0:len(picture)-4]+'.txt', 'r')
- rules= coords.readlines() # получили наборы координат
- i = 0
- for rule in rules: #для каждого правила этой картиночки
- #ОБРЕЗКА
- im = Image.open(truePath)#открыли оригинал
- coords = rule.split(',') #получили координаты фигуры
- im = im.crop((int(coords[0]), int(coords[1]), int(coords[2]), int(coords[3])))#вырезали
- print((int(coords[0]), int(coords[1]), int(coords[2]), int(coords[3])))
- newFileName = '{0}{1}_{2}.png'.format(output+"/", picture[0:len(picture)-4], str(i)) #собрали новое название
- f = open(newFileName, 'w')
- print(newFileName)
- im.save(newFileName)
- f.close()
- #GRAYSCALE
- im = Image.open(truePath)#открыли оригинал
- coords = rule.split(',') #получили координаты фигуры
- im = im.crop((int(coords[0]), int(coords[1]), int(coords[2]), int(coords[3])))#вырезали
- im = im.convert("L")
- newFileName = '{0}{1}_{2}_gray.png'.format(output+"_grayscale/", picture[0:len(picture)-4], str(i)) #собрали новое название
- f = open(newFileName, 'w')
- print(newFileName)
- im.save(newFileName)
- f.close()
- #FLIP
- im = Image.open(truePath)#открыли оригинал
- coords = rule.split(',') #получили координаты фигуры
- im = im.crop((int(coords[0]), int(coords[1]), int(coords[2]), int(coords[3])))#вырезали
- im = im.transpose(Image.FLIP_LEFT_RIGHT)
- newFileName = '{0}{1}_{2}_flip.png'.format(output+"_flip/", picture[0:len(picture)-4], str(i)) #собрали новое название
- f = open(newFileName, 'w')
- print(newFileName)
- im.save(newFileName)
- f.close()
- i+=1
- print(newFileName)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement