from pathlib import Path test=sorted(Path("C:/Users/").glob('*.jpg')) print (test)
Так получилось но, он указывает полный путь и всё в 1 строке, не понятно как дальше с этим работать, как по очереди их обработать?
from pathlib import Path test=sorted(Path("C:/Users/").glob('*.jpg')) print (test)
oDoDooDoDo А вы кто? Мальчик, девочка, сколько вам лет 8 или 80? С вами можно пожестче общаться или помягче?
не понятно как дальше с этим работать, как по очереди их обработать?
doza_andА это имеет значение? Тягость знаний ограничена чем то? И идиоты вроде меня не могут задавать интересующие им вопросы? Общайтесь как хотите. Я пользуюсь форумом, чтобы облегчить себе жизнь, да метод обучения ужасный, я знаю фор иф вайл и прочее но мне это понятнее когда они участвуют в задаче которая мне интересна. Мне просто не хватит запаса интереса и времени если я буду изучать азы прочитать кучу статей и книг, пойти на курсы отучиться пол года, чтобы я смог пользоваться итератами и смог одновременно уменьшить все картинки в определенной папке?
oDoDo А вы кто? Мальчик, девочка, сколько вам лет 8 или 80? С вами можно пожестче общаться или помягче?
oDoDoКонечно это важно. От этого существенно зависит куда вас посылать. :) Если семилетнее дарование то оно не знает подходов с систематическим изучением учебников, у него нет за плечами опыта университетского курса матанализа или систематической работы за стойкой в ресторане. Тогда имеет смысл послать к учебнику. Если предпенсионное развлечение для души, то тут как я думаю, важен процесс а не результат и время за которое он получен. Тогда достаточно пол строчки от того кому не лень писать, чтобы в сотый раз вывести из тупика.
А это имеет значение?
for i in Path("C:/Users/").glob('*.jpg'): обработать(i) def обработать(i): .....
oDoDo
Изображение лежит в папке, нужно сохранить ширину и высоту в разные переменные, как это сделать?
oDoDo
Спасибо, а как брать картинки по очереди из папки и проделывать те же действия?
oDoDoВот ты таким образом медленно подходишь к проектированию программы.
А как по очереди обработать изображения в папке?
Как указать путь к папке > найти все изображения> создать список > обработать по списку
def evaluate_everything():
pass
evaluate_everything()
def evaluate_everything():
return 'result'
result = evaluate_everything()
print(result)
def evaluate_everything():
numbers = get_numbers()
squares = get_squares(numbers)
sum_value = get_sum(squares)
out = sum_value
return out
result = evaluate_everything()
print(result)
def get_numbers():
return [1, 2]
def get_squares(numbers):
return [1, 4]
def get_sum(numbers):
return 1 + 4
def evaluate_everything():
numbers = get_numbers()
squares = get_squares(numbers)
sum_value = get_sum(squares)
out = sum_value
return out
result = evaluate_everything()
print(result)
def get_numbers():
return [1, 2, 3, 4, 5, 6, 7, 8, 9]
def get_squares(numbers):
return [i * i for i in numbers]
def get_sum(numbers):
return sum(numbers)
def evaluate_everything():
numbers = get_numbers()
squares = get_squares(numbers)
sum_value = get_sum(squares)
out = sum_value
return out
result = evaluate_everything()
print(result)
def get_numbers():
return range(1, 10)
def get_squares(numbers):
return (i * i for i in numbers)
def get_sum(numbers):
return sum(numbers)
def evaluate_everything():
numbers = get_numbers()
squares = get_squares(numbers)
sum_value = get_sum(squares)
out = sum_value
return out
result = evaluate_everything()
print(result)
>>> def get_numbers(): ... return range(1, 10) ... >>> def get_squares(numbers): ... return (i * i for i in numbers) ... >>> def get_sum(numbers): ... return sum(numbers) ... >>> def evaluate_everything(): ... numbers = get_numbers() ... squares = get_squares(numbers) ... sum_value = get_sum(squares) ... out = sum_value ... return out ... >>> result = evaluate_everything() >>> print(result) 285 >>>
from pathlib import Path from PIL import Image k=0 kartinki=sorted(Path("C:/Users/").glob('*.jpg'))+sorted(Path("C:/Users/").glob('*.png')) def perebor(i): k+1 print(i) image = Image.open(i) width, height = image.size saved = "resize"+k+".jpg" # Задать уникальное имя resized_img = image.resize((width, height), Image.ANTIALIAS) resized_img.save(saved) for i in kartinki: perebor(i)
nm = f"resize{k}.jpg"
oDoDo
Изображение лежит в папке, нужно сохранить ширину и высоту в разные переменные, как это сделать?
def save_size_of_image(inpath, outpath):
pass
save_size_of_image('input path', 'output path')
def save_size_of_image(inpath, outpath):
pass
save_size_of_image(r'C:\Users\pic.jpg', r'C:\Users\pic_output.txt')
def save_size_of_image(inpath, outpath):
print('Processed', inpath)
print('Saved to', outpath)
save_size_of_image(r'C:\Users\pic.jpg', r'C:\Users\pic_output.txt')
>>> def save_size_of_image(inpath, outpath): ... print('Processed', inpath) ... print('Saved to', outpath) ... >>> save_size_of_image(r'C:\Users\pic.jpg', r'C:\Users\pic_output.txt') Processed C:\Users\pic.jpg Saved to C:\Users\pic_output.txt >>>
def save_size_of_image(inpath, outpath):
print('Processed', inpath)
print('Saved to', outpath)
save_size_of_image(r'C:\Users\pic.jpg', r'C:\Users\pic_output.txt')
def save_size_of_image(inpath, outpath):
image = open_image(inpath)
image_size = get_size(image)
close_image(image)
output_file = open_output_file(outpath)
save_image_size_to_output_file(image_size, output_file)
close_output_file(output_file)
save_size_of_image(r'C:\Users\pic.jpg', r'C:\Users\pic_output.txt')
def open_image(path):
pass
def get_size(image):
pass
def close_image(image):
pass
def open_output_file(path):
pass
def save_image_size_to_output_file(image_size, output_file):
pass
def close_output_file(output_file):
pass
def save_size_of_image(inpath, outpath):
image = open_image(inpath)
image_size = get_size(image)
close_image(image)
output_file = open_output_file(outpath)
save_image_size_to_output_file(image_size, output_file)
close_output_file(output_file)
save_size_of_image(r'C:\Users\pic.jpg', r'C:\Users\pic_output.txt')
def open_image(path):
return '[image object]'
def get_size(image):
return (1024, 768)
def close_image(image):
print('image object', str(image), 'closed')
def open_output_file(path):
return '[file object]'
def save_image_size_to_output_file(image_size, output_file):
print('image_size', str(image_size), 'saved to output_file', str(output_file))
def close_output_file(output_file):
print('output file', str(output_file), 'closed')
def save_size_of_image(inpath, outpath):
image = open_image(inpath)
image_size = get_size(image)
close_image(image)
output_file = open_output_file(outpath)
save_image_size_to_output_file(image_size, output_file)
close_output_file(output_file)
save_size_of_image(r'C:\Users\pic.jpg', r'C:\Users\pic_output.txt')
>>> def open_image(path): ... return '[image object]' ... >>> def get_size(image): ... return (1024, 768) ... >>> def close_image(image): ... print('image object', str(image), 'closed') ... >>> def open_output_file(path): ... return '[file object]' ... >>> def save_image_size_to_output_file(image_size, output_file): ... print('image_size', str(image_size), 'saved to output_file', str(output_file)) ... >>> def close_output_file(output_file): ... print('output file', str(output_file), 'closed') ... >>> def save_size_of_image(inpath, outpath): ... image = open_image(inpath) ... image_size = get_size(image) ... close_image(image) ... output_file = open_output_file(outpath) ... save_image_size_to_output_file(image_size, output_file) ... close_output_file(output_file) ... >>> save_size_of_image(r'C:\Users\pic.jpg', r'C:\Users\pic_output.txt') image object [image object] closed image_size (1024, 768) saved to output_file [file object] output file [file object] closed >>>
def open_image(path):
return '[image object]'
def get_size(image):
return (1024, 768)
def close_image(image):
print('image object', str(image), 'closed')
def open_output_file(path):
return '[file object]'
def save_image_size_to_output_file(image_size, output_file):
print('image_size', str(image_size), 'saved to output_file', str(output_file))
def close_output_file(output_file):
print('output file', str(output_file), 'closed')
def save_size_of_image(inpath, outpath):
image = open_image(inpath)
image_size = get_size(image)
close_image(image)
output_file = open_output_file(outpath)
save_image_size_to_output_file(image_size, output_file)
close_output_file(output_file)
save_size_of_image(r'C:\Users\pic.jpg', r'C:\Users\pic_output.txt')
from PIL import Image
def open_image(path):
image = Image.open(path)
return image
def get_size(image):
return (1024, 768)
def close_image(image):
print('image object', str(image), 'closed')
def open_output_file(path):
return '[file object]'
def save_image_size_to_output_file(image_size, output_file):
print('image_size', str(image_size), 'saved to output_file', str(output_file))
def close_output_file(output_file):
print('output file', str(output_file), 'closed')
def save_size_of_image(inpath, outpath):
image = open_image(inpath)
image_size = get_size(image)
close_image(image)
output_file = open_output_file(outpath)
save_image_size_to_output_file(image_size, output_file)
close_output_file(output_file)
save_size_of_image(r'C:\Users\pic.jpg', r'C:\Users\pic_output.txt')
from PIL import Image
def open_image(path):
image = Image.open(path)
return image
def get_size(image):
return image.size
def close_image(image):
print('image object', str(image), 'closed')
def open_output_file(path):
return '[file object]'
def save_image_size_to_output_file(image_size, output_file):
print('image_size', str(image_size), 'saved to output_file', str(output_file))
def close_output_file(output_file):
print('output file', str(output_file), 'closed')
def save_size_of_image(inpath, outpath):
image = open_image(inpath)
image_size = get_size(image)
close_image(image)
output_file = open_output_file(outpath)
save_image_size_to_output_file(image_size, output_file)
close_output_file(output_file)
save_size_of_image(r'C:\Users\pic.jpg', r'C:\Users\pic_output.txt')
from PIL import Image
def open_image(path):
image = Image.open(path)
return image
def get_size(image):
return image.size
def close_image(image):
image.close()
def open_output_file(path):
return '[file object]'
def save_image_size_to_output_file(image_size, output_file):
print('image_size', str(image_size), 'saved to output_file', str(output_file))
def close_output_file(output_file):
print('output file', str(output_file), 'closed')
def save_size_of_image(inpath, outpath):
image = open_image(inpath)
image_size = get_size(image)
close_image(image)
output_file = open_output_file(outpath)
save_image_size_to_output_file(image_size, output_file)
close_output_file(output_file)
save_size_of_image(r'C:\Users\pic.jpg', r'C:\Users\pic_output.txt')
from PIL import Image
def open_image(path):
image = Image.open(path)
return image
def get_size(image):
return image.size
def close_image(image):
image.close()
def open_output_file(path):
output_file = open(path, 'w', encoding='utf-8')
return output_file
def save_image_size_to_output_file(image_size, output_file):
print('image_size', str(image_size), 'saved to output_file', str(output_file))
def close_output_file(output_file):
print('output file', str(output_file), 'closed')
def save_size_of_image(inpath, outpath):
image = open_image(inpath)
image_size = get_size(image)
close_image(image)
output_file = open_output_file(outpath)
save_image_size_to_output_file(image_size, output_file)
close_output_file(output_file)
save_size_of_image(r'C:\Users\pic.jpg', r'C:\Users\pic_output.txt')
from PIL import Image
def open_image(path):
image = Image.open(path)
return image
def get_size(image):
return image.size
def close_image(image):
image.close()
def open_output_file(path):
output_file = open(path, 'w', encoding='utf-8')
return output_file
def save_image_size_to_output_file(image_size, output_file):
width, height = image_size
print('width:', width, file=output_file)
print('height:', height, file=output_file)
def close_output_file(output_file):
print('output file', str(output_file), 'closed')
def save_size_of_image(inpath, outpath):
image = open_image(inpath)
image_size = get_size(image)
close_image(image)
output_file = open_output_file(outpath)
save_image_size_to_output_file(image_size, output_file)
close_output_file(output_file)
save_size_of_image(r'C:\Users\pic.jpg', r'C:\Users\pic_output.txt')
from PIL import Image
def open_image(path):
image = Image.open(path)
return image
def get_size(image):
return image.size
def close_image(image):
image.close()
def open_output_file(path):
output_file = open(path, 'w', encoding='utf-8')
return output_file
def save_image_size_to_output_file(image_size, output_file):
width, height = image_size
print('width:', width, file=output_file)
print('height:', height, file=output_file)
def close_output_file(output_file):
output_file.close()
def save_size_of_image(inpath, outpath):
image = open_image(inpath)
image_size = get_size(image)
close_image(image)
output_file = open_output_file(outpath)
save_image_size_to_output_file(image_size, output_file)
close_output_file(output_file)
save_size_of_image(r'C:\Users\pic.jpg', r'C:\Users\pic_output.txt')
>>> from PIL import Image >>> >>> >>> def open_image(path): ... image = Image.open(path) ... return image ... >>> def get_size(image): ... return image.size ... >>> def close_image(image): ... image.close() ... >>> def open_output_file(path): ... output_file = open(path, 'w', encoding='utf-8') ... return output_file ... >>> def save_image_size_to_output_file(image_size, output_file): ... width, height = image_size ... print('width:', width, file=output_file) ... print('height:', height, file=output_file) ... >>> def close_output_file(output_file): ... output_file.close() ... >>> def save_size_of_image(inpath, outpath): ... image = open_image(inpath) ... image_size = get_size(image) ... close_image(image) ... output_file = open_output_file(outpath) ... save_image_size_to_output_file(image_size, output_file) ... close_output_file(output_file) ... >>> save_size_of_image('d/pic.png', 'd/pic_output.txt') >>>
[guest@localhost img_size_save]$ ls
d
[guest@localhost img_size_save]$ ls d
pic_output.txt pic.png
[guest@localhost img_size_save]$ cat d/pic_output.txt
width: 1024
height: 768
[guest@localhost img_size_save]$
py.user.nextОчень хорошая подача информации,спасибо