Это видео недоступно.
Сожалеем об этом.
Python Resize Multiple Images
HTML-код
- Опубликовано: 13 авг 2024
- In this Python Pillow tutorial, we will go over how to display image properties, preview images, loop through a set of images, create a list of images, and resize multiple images.
Here is another example that saves the resized images in a new folder with the existing filename.
from PIL import Image
import glob
import os
# new folder path (may need to alter for Windows OS)
# change path to your path
path = 'path/Resized_Shapes'
# create new folder
if not os.path.exists(path):
os.makedirs(path)
# loop over existing images and resize
# change path to your path
for filename in glob.glob('path/Shapes/*.png'):
img = Image.open(filename).resize((1200,1200))
# save resized images to new folder with existing filename
img.save('{}{}{}'.format(path,'/',os.path.split(filename)[1]))
Thanks Ryan, in a blink of an eye resized thousands of images. Great
this is not showing me any output in the output folder
Make sure the paths are correct. I just tested the code and it works on Mac and PC. If using PC make path string a raw string or using double backslashes.
I was beating my head for this and found the solution, thanks man!
Very nice video, just jump right into coding, no excessive talking, I love it, keep up the good work.
Much appreciated!
I tried this, but get an error Traceback with im = image.open(img_path) ? but cannot see why.
import glob
img_path = '/Volumes/Elements/PlmEvents/PrintTest/Capture03.jpg'
im = image.open(img_path)
print('{}' .format(im.format))
print('size/; {}' .format(im.size))
print('image mode: {}') .format(im.mode)
im.show()
Are you on a Mac or Windows machine? My first guess is that the path does not look right. Is that the full path? If you are on Windows, you may need to put in double slashes or make it a raw string. If you are on a Mac, find the image file, right click, hit the option key and copy as path name - for the full path name. Or you can put the image file in your working directory and just use the image file name and extension.
this video is very helpful, thanks :)
Glad it was helpful!
Thank you, Ryan! Thank you, Guido von Rossum! #python #jupyternotebook
Thank you man
Thank you bro
shall you add zooming , scalling ,.... using python sir
muchas gracias, me ayudó mucho!
Cheers! Plain and simple - and works!
thanks a lot
Thanks
Hi.
How can i save these resized files by their original name, but in a new folder? and not rename it?
Use the same filename in save. This will overwrite so keep a copy of the original images if needed.
for filename in glob.glob('path/*.jpeg'):
img = Image.open(filename).resize((350,350))
img.save(filename)
showing the error print{'{}'.format(im.format)}
^
SyntaxError: invalid syntax
print() uses round brackets, not curly brackets.
Hi, thanks for this. How can this be used to resize image files with multiple extensions? ie to resize all files in a folder irrespective of the extension
Let me look into and I'll try to get back to you.
Thank you.
Code below resizes images with different types/extensions. I have not tried with all image types/extensions but works with jpg and png as an example. Also, a side note, if the originals should not be changed, keep a backup somewhere they can't be overwritten.
from PIL import Image
import glob
import os
# change path to your path
# create list of paths to different images
# list of different image types/extensions (add extensions as needed)
img_types = ['*.JPG', '*.png']
images = []
for i in img_types:
images.extend(glob.glob(f'/Users/username/Desktop/Shapes/{i}'))
# create new folder
new_folder = '/Users/username/Desktop/Resized_Images'
if not os.path.exists(new_folder):
os.makedirs(new_folder)
# resize images and save to new folder
for i in images:
img = Image.open(i).resize((100,100))
img.save(f'{new_folder}/{os.path.split(i)[1]}')
@@RyanNoonan thank u so much u don't know how much time u saved me with this code
Hi, thank you! It works! you rock. Would it require much to turn this into a program that I can put on my website? :D
Thanks. I have not tried to turn this into a website program, etc. yet. I'll try to look into that sometime.
@@RyanNoonan Thanks bud. By the way, is there any way to create a new folder on Desktop to save the files in? For instance a friend of mine would like to use a program like this. I dont know how to retrieve their PC username, since it's different to mine. I hope you know what I mean. Completely beginner to Python here.. so I basically have no idea
For them to pick destination folder would require some kind of GUI?
To create a new folder for files on your computer, see my pinned comment at the top for an example. You would just need to put in the path(s). Does that help?
@@RyanNoonan Yes, it makes sense. What i'm wondering tho, is how other people can save on their desktop without editing the code. To have some kind of save destination input.
I can't resize please help me