Skip to content

Commit 27633be

Browse files
committed
refactored photo renaming scripts
1 parent de91049 commit 27633be

9 files changed

+88
-102
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ archives/
1717
**/archives/
1818
album-photos/renamed/
1919
**/__pycache__/
20+
21+
scripts/photos/

photos/filenames.md

Lines changed: 0 additions & 51 deletions
This file was deleted.

photos/scripts/photo-renaming-script.sh

Lines changed: 0 additions & 27 deletions
This file was deleted.

photos/scripts/step00_create_srcsets.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

scripts/step00_create_srcsets.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# script for creating srcset image copies
2+
3+
import step01_add_dimensions_to_original_image_names
4+
import step02_create_resized_image_copies_for_srcset
5+
import step03_copy_orig_image_rename_for_srcset
6+
import step04_cleanup
7+
8+
start='photos/input/' # dir must exist before running this code
9+
renamed='photos/work_in_process/'
10+
dest='photos/srcset/'
11+
archives = 'photos/archives/'
12+
# renamed='/Users/blauerbock/workspaces/react-photo-album-main/examples/react-lightbox-photo-album/photos/renamed/'
13+
# dest='/Users/blauerbock/workspaces/react-photo-album-main/examples/react-lightbox-photo-album/photos/dest/'
14+
15+
if __name__ == "__main__":
16+
step01_add_dimensions_to_original_image_names.rename_images_add_dimensions(start, renamed)
17+
step02_create_resized_image_copies_for_srcset.create_resized_copies(renamed, dest)
18+
step03_copy_orig_image_rename_for_srcset.copy_and_rename_jpg_images(renamed, dest)
19+
step04_cleanup.cleanup_files(start, archives)
20+
step04_cleanup.cleanup_files(renamed, archives)

photos/scripts/step01_add_dimensions_to_original_image_names.py renamed to scripts/step01_add_dimensions_to_original_image_names.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@
1212
from PIL import Image
1313
import shutil
1414

15-
def rename_images_add_dimensions(start_dir, renamed_dir):
15+
def rename_images_add_dimensions(start_dir, renamed_dir, test=False):
16+
if test:
17+
print('cwd =', os.getcwd())
1618
if not os.path.exists(renamed_dir):
1719
os.makedirs(renamed_dir)
1820
for filename in os.listdir(start_dir):
1921
file_lower = filename.lower()
2022
if file_lower.endswith('.jpg') or file_lower.endswith('.jpeg'):
2123
name, ext = os.path.splitext(filename)
22-
ext = '.jpg' # always use .jpg
24+
if test:
25+
print("name:", name, ", ext:", ext)
26+
ext = '.jpg' # always use .jpg, not .jpeg
2327
img_path = os.path.join(start_dir, filename)
2428
try:
2529
with Image.open(img_path) as img:
@@ -28,9 +32,17 @@ def rename_images_add_dimensions(start_dir, renamed_dir):
2832
print(f"Could not open {filename}: {e}")
2933
continue
3034
new_name = f"{name}.{width}x{height}{ext}"
35+
if test:
36+
print("new_name: ", new_name)
3137
new_path = os.path.join(renamed_dir, new_name)
3238
shutil.copy2(img_path, new_path)
3339

3440
# start='/Users/blauerbock/workspaces/react-photo-album-main/examples/react-lightbox-photo-album/photos/start/'
3541
# renamed='/Users/blauerbock/workspaces/react-photo-album-main/examples/react-lightbox-photo-album/photos/renamed/'
3642
# rename_images_add_dimensions(start, renamed)
43+
44+
if __name__ == "__main__":
45+
test_dir1 = 'photos/test1' # original photo named XXXX.YYY.jpg
46+
test_dir2 = 'photos/test2' # original photo with dimensions added to name
47+
# i.e., XXXX.YYY.WIDTHxHEIGHT.jpg
48+
rename_images_add_dimensions(test_dir1, test_dir2, test=True)

photos/scripts/step02_create_resized_image_copies_for_srcset.py renamed to scripts/step02_create_resized_image_copies_for_srcset.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
# This function will create resized copies of each JPG image in the 'start' directory, saving
2-
# them in the 'destination' directory with the specified filename format.
1+
# This function will create a srcset of resized copies of each JPG image
2+
# in the 'start' directory, saving them in the destination
3+
# directory with the specified filename format.
34

45
import os
56
from PIL import Image
67

7-
def create_resized_copies(start_dir, dest_dir):
8+
def create_resized_copies(start_dir, dest_dir, test=False):
89
widths = [1080, 640, 384, 256, 128, 96, 64, 48]
910
if not os.path.exists(dest_dir):
1011
os.makedirs(dest_dir)
@@ -21,10 +22,16 @@ def create_resized_copies(start_dir, dest_dir):
2122
name, ext = os.path.splitext(filename)
2223
new_filename = f"{name}.{w}w{ext}"
2324
dest_path = os.path.join(dest_dir, new_filename)
25+
if test:
26+
print("dest_path:", dest_path)
2427
resized_img.save(dest_path, "JPEG")
2528

2629
# start='/Users/blauerbock/workspaces/react-photo-album-main/examples/react-lightbox-photo-album/photos/start/'
2730
# renamed='/Users/blauerbock/workspaces/react-photo-album-main/examples/react-lightbox-photo-album/photos/renamed/'
2831
# dest='/Users/blauerbock/workspaces/react-photo-album-main/examples/react-lightbox-photo-album/photos/dest/'
2932
# create_resized_copies(start, dest)
3033

34+
if __name__ == "__main__":
35+
test_dir2 = 'photos/test2'
36+
test_dir3 = 'photos/test3'
37+
create_resized_copies(test_dir2, test_dir3, test=True)

photos/scripts/step03_copy_orig_image_rename_for_srcset.py renamed to scripts/step03_copy_orig_image_rename_for_srcset.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import shutil
33
from PIL import Image
44

5-
def copy_and_rename_jpg_images(src_dir, dst_dir):
6-
# src_dir = 'renamed'
7-
# dst_dir = 'destination'
5+
# add width to the end of the original full-sized image name and copy the
6+
# renamed image to the 'dst_dir' directory.
7+
def copy_and_rename_jpg_images(src_dir, dst_dir, test=False):
88
if not os.path.exists(dst_dir):
99
os.makedirs(dst_dir)
1010
for filename in os.listdir(src_dir):
@@ -20,9 +20,16 @@ def copy_and_rename_jpg_images(src_dir, dst_dir):
2020
new_filename = f"{name}.{width}w{ext}"
2121
dst_path = os.path.join(dst_dir, new_filename)
2222
shutil.copy2(src_path, dst_path)
23-
print(f"Copied and renamed: {src_path} -> {dst_path}")
23+
if test:
24+
print(f"Copied and renamed: {src_path} -> {dst_path}")
2425

2526

2627
# renamed='/Users/blauerbock/workspaces/react-photo-album-main/examples/react-lightbox-photo-album/photos/renamed/'
2728
# dest='/Users/blauerbock/workspaces/react-photo-album-main/examples/react-lightbox-photo-album/photos/dest/'
2829
# copy_and_rename_jpg_images(renamed, dest)
30+
31+
if __name__ == "__main__":
32+
src_dir = "photos/test2"
33+
dst_dir = "photos/test3"
34+
copy_and_rename_jpg_images(src_dir, dst_dir, test=True)
35+

scripts/step04_cleanup.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import shutil
2+
import os
3+
4+
5+
def cleanup_files(src_dir, dest_dir):
6+
# source_directory = "path/to/source_folder" # Replace with your source directory path
7+
# destination_directory = "path/to/destination_folder" # Replace with your destination directory path
8+
9+
# Create the destination directory if it doesn't exist
10+
os.makedirs(dest_dir, exist_ok=True)
11+
12+
# Get a list of all files in the source directory
13+
files_to_move = os.listdir(src_dir)
14+
15+
# Iterate through each file and move it
16+
for filename in files_to_move:
17+
source_path = os.path.join(src_dir, filename)
18+
destination_path = os.path.join(dest_dir, filename)
19+
20+
# Check if it's a file (and not a subdirectory) before moving
21+
if os.path.isfile(source_path):
22+
shutil.move(source_path, destination_path)
23+
print(f"Moved '{filename}' to '{dest_dir}'")
24+
else:
25+
print(f"Skipping '{filename}' as it is not a file.")
26+
27+
28+
if __name__ == "__main__":
29+
src_dir = "photos/input/" # Replace with the actual path to your file
30+
dest_dir = "photos/archives/" # Replace with the actual path to your destination directory
31+
cleanup_files(src_dir, dest_dir)

0 commit comments

Comments
 (0)