-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimage_mod1.py
More file actions
42 lines (29 loc) · 1.02 KB
/
image_mod1.py
File metadata and controls
42 lines (29 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
@author: najmeh
"""
from PIL import Image
import os
# same path with images
image1 = Image.open('image/dog1.jpeg')
image1.show()
#%%
# modify
image1.save('dog11.png') # change the image format
#%% create a "pngs" folder
for f in os.listdir('.'): # give directory to get the files
if f.endswith('.jpeg'):
i = Image.open(f)
fn, fext = os.path.splitext(f)
i.save('pngs/{}.png'.format(fn))
#%%
# resize each image and save it to 300 folder
size_100 = (100,100)
size_200 = (200,200)
for f in os.listdir('image'): # give directory to get the files
if f.endswith('.jpeg'):
i = Image.open(f)
fn, fext = os.path.splitext(f)
i.thumbnail(size_100) # it also keep the aspect ratio
i.save('100/{}_100.p{}'.format(fn, fext)) # rename but keep regular extension
i.thumbnail(size_200) # it also keep the aspect ratio
i.save('200/{}_200.p{}'.format(fn, fext)) # rename but keep regular extension