Skip to content

Commit 9525e99

Browse files
adding new scripts for dealing with images
1 parent 4661b85 commit 9525e99

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

Rename-Images-In-Folder.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"__mahmoud_ahmed__"
2+
3+
import os
4+
img_types = ['jpg' , 'png' , 'jpeg']
5+
for root, dirs, files in os.walk('.'):
6+
for i,f in enumerate(files):
7+
absname = os.path.join(root, f)
8+
img_type = absname.split('.')[2]
9+
10+
if img_type in img_types :
11+
newname = '{}.{}'.format(str(i),img_type)
12+
os.rename(f, newname)

add_logo_to_images.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
3+
import os
4+
from PIL import Image
5+
6+
7+
#LOGO_FILENAME = 'catlogo.png'
8+
LOGO_FILENAME = input('Enter The Logo Name With Extension: ')
9+
NEW_FOLDER_NAME = input("Enter The New Folder Name : ")
10+
11+
logoIm = Image.open(LOGO_FILENAME)
12+
logoWidth, logoHeight = logoIm.size
13+
14+
os.makedirs(NEW_FOLDER_NAME, exist_ok=True)
15+
# Loop over all files in the working directory.
16+
for filename in os.listdir('.'):
17+
if not (filename.endswith('.png') or filename.endswith('.jpg')) \
18+
or filename == LOGO_FILENAME:
19+
continue # skip non-image files and the logo file itself
20+
21+
im = Image.open(filename)
22+
width, height = im.size
23+
24+
25+
# Add logo.
26+
print('Adding logo to %s...' % (filename))
27+
im.paste(logoIm, (width - logoWidth, height - logoHeight), logoIm)
28+
29+
# Save changes.
30+
im.save(os.path.join(NEW_FOLDER_NAME, filename))
31+
32+
print("Done Adding Logo To All Images ... ^_^")

copy_certain_files.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Selective copy
2+
3+
'''Walking through a folder tree and searching for files with a certain file extension('.jpg','.pdf')
4+
and copying from whatever location they are present at to a new folder'''
5+
6+
import shutil
7+
8+
import os
9+
10+
# Walking through a folder tree
11+
os.makedirs('newfolder', exist_ok=True)
12+
13+
for folders,subfoldes,filenames in os.walk('.'):
14+
for filename in filenames:
15+
if filename.endswith('.jpg') or filename.endswith('.pdf'):
16+
shutil.copy(file_name,'newfolder')
17+

resize_images.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
3+
import os
4+
from PIL import Image
5+
6+
#SQUARE_FIT_SIZE = 300
7+
SQUARE_FIT_SIZE = int(input(" Enter Size : "))
8+
NEW_FOLDER_NAME = input("Enter The New Folder Name : ")
9+
10+
11+
12+
os.makedirs(NEW_FOLDER_NAME, exist_ok=True)
13+
# Loop over all files in the working directory.
14+
for filename in os.listdir('.'):
15+
if not (filename.endswith('.png') or filename.endswith('.jpg')):
16+
continue # skip non-image files and the logo file itself
17+
18+
im = Image.open(filename)
19+
width, height = im.size
20+
21+
# Check if image needs to be resized.
22+
if width > SQUARE_FIT_SIZE and height > SQUARE_FIT_SIZE:
23+
# Calculate the new width and height to resize to.
24+
if width > height:
25+
height = int((SQUARE_FIT_SIZE / width) * height)
26+
width = SQUARE_FIT_SIZE
27+
else:
28+
width = int((SQUARE_FIT_SIZE / height) * width)
29+
height = SQUARE_FIT_SIZE
30+
31+
# Resize the image.
32+
print('Resizing %s...' % (filename))
33+
im = im.resize((width, height))
34+
35+
36+
37+
# Save changes.
38+
im.save(os.path.join(NEW_FOLDER_NAME, filename))
39+
40+
print('Done Resizing All Images ... ^_^')

0 commit comments

Comments
 (0)