import Image, ImageOps
import glob, os
for infile in glob.glob("images\*\*\*.png"):
file, ext = os.path.splitext(infile)
im = Image.open(infile)
gray = ImageOps.grayscale(im)
gray.save(file + '.png')
import Image, ImageOps
import glob, os
for infile in glob.glob("images\*\*\*.png"):
file, ext = os.path.splitext(infile)
im = Image.open(infile)
gray = ImageOps.grayscale(im)
gray.save(file + '.png')
>>> p = r'images\x\y\*.png'
>>> p
'images\\x\\y\\*.png'
>>> pl = p.split('\\', 1)
>>> pl
['images', 'x\\y\\*.png']
>>> os.path.join(pl[0] + '_2', pl[1])
'images_2/x\\y\\*.png'
>>>
>>> p = r'images\x\y\*.png'
>>> pl = p.split('\\')
>>> pl
['images', 'x', 'y', '*.png']
>>> np = '\\'.join((pl[0] + '_2', '\\'.join(pl[1:])))
>>> np
'images_2\\x\\y\\*.png'
>>>
py.user.nextЕще бы понять, как это встроить в мой скрипт… Я с Python вообще не знакомos.path.join что-то со списками не работает>>> p = r'images\x\y\*.png'
>>> pl = p.split('\\')
>>> pl
['images', 'x', 'y', '*.png']
>>> np = '\\'.join((pl[0] + '_2', '\\'.join(pl[1:])))
>>> np
'images_2\\x\\y\\*.png'
>>>
import os
def ForcedOpen(filename,openkeys):
"""открываем файл и создаем директории если надо"""
dirnm=os.path.dirname(filename)
if not os.path.exists(dirnm):
os.makedirs(dirnm)
return open(filename,openkeys)
def get_new_path(path, suff, sep='\\'):
pl = path.split(sep)
np = sep.join((pl[0] + suff, sep.join(pl[1:])))
return np
>>> def get_new_path(path, suff, sep='\\'):
... pl = path.split(sep)
... np = sep.join((pl[0] + suff, sep.join(pl[1:])))
... return np
...
>>> path = r'images\x\y\1.png'
>>> newpath = get_new_path(path, '_2')
>>> print(newpath)
images_2\x\y\1.png
>>>