the result processing images of this script (after step 5, before step 6) is following the pattern of ascending names but not choosing the right images without error. example:
source image files:
5-cach-phoi-o-voi-ao-blazer-thanh-lich_0.jpg #error
5-cach-phoi-o-voi-ao-blazer-thanh-lich_1.jpg #not error
5-cach-phoi-o-voi-ao-blazer-thanh-lich_2.jpg #error
5-cach-phoi-o-voi-ao-blazer-thanh-lich_3.jpg #error
5-cach-phoi-o-voi-ao-blazer-thanh-lich_4.jpg #not error
5-cach-phoi-o-voi-ao-blazer-thanh-lich_5.jpg #not error
5-cach-phoi-o-voi-ao-blazer-thanh-lich_6.jpg #error
5-cach-phoi-o-voi-ao-blazer-thanh-lich_7.jpg #not error
5-cach-phoi-o-voi-ao-blazer-thanh-lich_8.jpg #error
5-cach-phoi-o-voi-ao-blazer-thanh-lich_9.jpg #not error
def is_image_readable(file_path):
"""
Check if an image file is readable.
code
Args:
file_path (str): Path to the image file.
Returns:
bool: True if the image is readable, False otherwise.
"""
try:
with Image.open(file_path) as img:
img.verify()
return True
except Exception:
return False
def delete_unreadable_images(folder_path):
"""
Delete unreadable image files within a folder.
code
Args:
folder_path (str): Path to the folder to scan.
"""
supported_formats = ('.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff', '.webp')
deleted_files = 0
for root, _, files in os.walk(folder_path):
for file in files:
if file.lower().endswith(supported_formats):
file_path = os.path.join(root, file)
if not is_image_readable(file_path):
try:
os.remove(file_path)
logging.info(f"Deleted unreadable image: {file_path}")
deleted_files += 1
except Exception as e:
logging.error(f"Error deleting {file_path}: {e}")
logging.info(f"Total unreadable image files deleted: {deleted_files}")
def count_image_files(folder_path, extensions=('.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff', '.webp')):
"""
Count the number of image files in a folder.
code
Args:
folder_path (str): Path to the folder.
extensions (tuple): Tuple of file extensions to count.
Returns:
int: Number of image files.
"""
return len([f for f in os.listdir(folder_path) if f.lower().endswith(extensions)])
def delete_subfolders_with_few_images(folder_path, threshold=3):
"""
Delete subfolders containing a number of image files less than or equal to a threshold.
code
Args:
folder_path (str): Path to the parent folder.
threshold (int): Threshold number of images. Default is 3.
"""
supported_formats = ('.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff', '.webp')
deleted_folders = 0
for root, dirs, _ in os.walk(folder_path, topdown=False):
for dir_name in dirs:
subfolder_path = os.path.join(root, dir_name)
try:
image_count = count_image_files(subfolder_path, extensions=supported_formats)
if image_count <= threshold:
shutil.rmtree(subfolder_path)
logging.info(f"Deleted subfolder: {subfolder_path} (contained {image_count} image files)")
deleted_folders += 1
except Exception as e:
logging.error(f"Error processing subfolder {subfolder_path}: {e}")
logging.info(f"Total subfolders deleted: {deleted_folders}")
def trim_image_files(folder_path, target_count=4):
"""
Trim the number of image files in subfolders to a target count.
code
Args:
folder_path (str): Path to the parent folder.
target_count (int): Desired maximum number of images per subfolder. Default is 4.
"""
supported_formats = ('.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff', '.webp')
# Sorting logic to prioritize certain files
def file_sort_key(x):
match = re.search(r'(\d+).(jpg|jpeg|png|bmp|gif|tiff|webp)$', x, re.IGNORECASE)
index = int(match.group(1)) if match else 0
return (
not x.lower().endswith('_0.jpg'), # Prefer files not ending with '_0'
index, # Lower index preferred
x.lower() # Lexicographical order
)
for root, _, files in os.walk(folder_path):
image_files = [f for f in files if f.lower().endswith(supported_formats)]
image_count = len(image_files)
if image_count <= target_count:
continue
image_files.sort(key=file_sort_key, reverse=True)
files_to_delete = image_files[target_count:]
for file in files_to_delete:
file_path = os.path.join(root, file)
try:
os.remove(file_path)
logging.info(f"Deleted excess image: {file_path}")
except Exception as e:
logging.error(f"Error deleting {file_path}: {e}")
def is_image_all_white(img):
"""
Check if an image is entirely white.
code
Args:
img (PIL.Image.Image): The image to check.
Returns:
bool: True if the image is all white, False otherwise.
"""
try:
if img.mode != 'RGB':
img = img.convert('RGB')
extrema = img.getextrema()
# Extrema is a list where each item is a tuple of (min, max) for each channel
# For white image, all channels should have min and max as 255
return all(channel_extremum == (255, 255) for channel_extremum in extrema)
except Exception as e:
logging.error(f"Error checking if image is all white: {e}")
return False
def delete_blank_images(folder_path):
"""
Delete images that are entirely white.
code
Args:
folder_path (str): Path to the folder to scan.
"""
supported_formats = ('.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff', '.webp')
deleted_files = 0
for root, _, files in os.walk(folder_path):
for file in files:
if file.lower().endswith(supported_formats):
file_path = os.path.join(root, file)
try:
with Image.open(file_path) as img:
img = apply_exif_orientation(img)
if is_image_all_white(img):
os.remove(file_path)
logging.info(f"Deleted blank (all white) image: {file_path}")
deleted_files += 1
except Exception as e:
logging.error(f"Error processing {file_path} for blank check: {e}")
logging.info(f"Total blank images deleted: {deleted_files}")
def convert_and_resize_images(folder_path, target_width=1200):
"""
Convert all images to JPG and resize them to a target width while maintaining aspect ratio.
code
Args:
folder_path (str): Path to the folder containing images.
target_width (int): The target width for the resized images. Default is 1200 pixels.
"""
# Define supported image formats excluding '.jpg' and '.jpeg'
supported_formats = ('.png', '.bmp', '.gif', '.tiff', '.webp')
for root, _, files in os.walk(folder_path):
for file in files:
file_lower = file.lower()
file_path = os.path.join(root, file)
try:
# Handle .jpg and .jpeg files
if file_lower.endswith(('.jpg', '.jpeg')):
with Image.open(file_path) as img:
img = apply_exif_orientation(img)
# Resize if necessary (regardless of size)
if img.width != target_width:
ratio = target_width / float(img.width)
new_size = (target_width, int(img.height * ratio))
img = img.resize(new_size, Image.LANCZOS)
img.save(file_path, format='JPEG', quality=85)
logging.info(f"Resized image: {file_path}")
else:
logging.info(f"Image already at target size: {file_path}")
# Handle other supported formats
elif file_lower.endswith(supported_formats):
with Image.open(file_path) as img:
img = apply_exif_orientation(img)
# Convert to RGB if necessary
if img.mode in ("RGBA", "P", "LA"):
img = img.convert("RGB")
logging.debug(f"Converted image mode to RGB: {file_path}")
# Resize if necessary (regardless of size)
if img.width != target_width:
ratio = target_width / float(img.width)
new_size = (target_width, int(img.height * ratio))
img = img.resize(new_size, Image.LANCZOS)
logging.info(f"Resized image: {file_path}")
# Save as JPG
new_file_path = os.path.splitext(file_path)[0] + '.jpg'
img.save(new_file_path, 'JPEG', quality=85)
logging.info(f"Converted and saved image as JPG: {new_file_path}")
# Remove the original file
os.remove(file_path)
logging.info(f"Removed original image: {file_path}")
else:
logging.debug(f"Unsupported file format, skipping: {file_path}")
except Exception as e:
logging.error(f"Error processing {file_path}: {e}")
def apply_exif_orientation(img):
"""
Apply EXIF orientation to the image to ensure correct dimensions.
code
Args:
img (PIL.Image.Image): The image to be oriented.
Returns:
PIL.Image.Image: The oriented image.
"""
try:
exif = img._getexif()
if exif is not None:
for key, value in exif.items():
tag = ExifTags.TAGS.get(key, key)
if tag == 'Orientation':
orientation = value
break
else:
orientation = None
if orientation == 3:
img = img.rotate(180, expand=True)
logging.debug("Applied EXIF orientation: rotated 180 degrees")
elif orientation == 6:
img = img.rotate(270, expand=True)
logging.debug("Applied EXIF orientation: rotated 270 degrees")
elif orientation == 8:
img = img.rotate(90, expand=True)
logging.debug("Applied EXIF orientation: rotated 90 degrees")
except Exception as e:
logging.warning(f"Could not apply EXIF orientation: {e}")
return img
def rename_jpg_files(image_folder_path):
"""
Rename .jpg files in each subfolder based on the subfolder name.
code
Args:
image_folder_path (str): Path to the parent folder containing subfolders with images.
"""
def sanitize_filename(name):
"""
Sanitize the folder name by removing accents, converting to lowercase,
and replacing spaces and special characters with hyphens.
Args:
name (str): Original folder name.
Returns:
str: Sanitized folder name.
"""
# Normalize the string to NFD to separate accents from characters
nfkd_form = unicodedata.normalize('NFD', name)
# Encode to ASCII bytes, ignore characters that can't be encoded
only_ascii = nfkd_form.encode('ASCII', 'ignore').decode('utf-8')
# Replace any non-alphanumeric characters with hyphens
sanitized = re.sub(r'[^a-z0-9]+', '-', only_ascii.lower()).strip('-')
return sanitized
for root, dirs, files in os.walk(image_folder_path):
for dir_name in dirs:
subfolder_path = os.path.join(root, dir_name)
sanitized_base = sanitize_filename(dir_name)
if not sanitized_base:
logging.warning(f"Subfolder '{dir_name}' results in an empty base name after sanitization. Skipping...")
continue
# Gather all .jpg and .jpeg files
jpg_files = [f for f in os.listdir(subfolder_path) if f.lower().endswith(('.jpg', '.jpeg'))]
# Sort files to ensure consistent renaming order
jpg_files.sort()
for index, filename in enumerate(jpg_files, start=1):
original_file_path = os.path.join(subfolder_path, filename)
# Define the new filename
new_filename = f"{sanitized_base}-{index}.jpg"
new_file_path = os.path.join(subfolder_path, new_filename)
# Check if the new filename already exists to avoid overwriting
if os.path.exists(new_file_path):
logging.warning(
f"Cannot rename '{original_file_path}' to '{new_file_path}': Target file already exists.")
continue
try:
os.rename(original_file_path, new_file_path)
logging.info(f"Renamed '{original_file_path}' to '{new_file_path}'")
except Exception as e:
logging.error(f"Error renaming '{original_file_path}' to '{new_file_path}': {e}")
def delete_small_html_folders(folder_path, min_char_count=6000):
"""
Delete folders containing .html files with content less than a minimum character count.
code
Args:
folder_path (str): Path to the parent folder.
min_char_count (int): Minimum character count required. Default is 6000.
"""
for dirpath, _, filenames in os.walk(folder_path, topdown=False):
delete_folder = False
for filename in filenames:
if filename.lower().endswith('.html'):
file_path = os.path.join(dirpath, filename)
try:
with open(file_path, 'r', encoding='utf-8', errors='ignore') as file:
content = file.read()
if len(content) < min_char_count:
delete_folder = True
break
except Exception as e:
logging.error(f"Error reading {file_path}: {e}")
if delete_folder:
try:
shutil.rmtree(dirpath)
logging.info(f"Deleted folder due to small HTML content: {dirpath}")
except Exception as e:
logging.error(f"Error deleting folder {dirpath}: {e}")
def remove_blank_lines(file_path):
"""
Remove blank lines from a file.
code
Args:
file_path (str): Path to the file.
"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
non_blank_lines = [line.rstrip() for line in lines if line.strip()]
with open(file_path, 'w', encoding='utf-8') as file:
file.write('\n'.join(non_blank_lines))
except Exception as e:
logging.error(f"Error processing {file_path}: {e}")
def remove_img_tags(file_path):
"""
Remove all tags from an HTML file.
code
Args:
file_path (str): Path to the HTML file.
"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# Remove img tags using regex
cleaned_content = re.sub(r'<img\b[^>]>', '', content, flags=re.IGNORECASE)
with open(file_path, 'w', encoding='utf-8') as file:
file.write(cleaned_content)
except Exception as e:
logging.error(f"Error processing {file_path}: {e}")
def replace_h1_tag(file_path):
"""
Replace with in an HTML file.
code
Args:
file_path (str): Path to the HTML file.
"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
modified_content = content.replace('<h1 style="text-align: center;">', '<h1>')
with open(file_path, 'w', encoding='utf-8') as file:
file.write(modified_content)
except Exception as e:
logging.error(f"Error processing {file_path}: {e}")
def capitalize_first_letter_in_tags(content):
"""
Capitalize the first letter of text inside HTML tags.
code
Args:
content (str): The HTML content as a string.
Returns:
str: Modified content with capitalized text.
"""
# Function to capitalize text inside tags
def capitalize_match(match):
tag = match.group(1)
text = match.group(2).strip()
if text:
return f"<{tag}>{text[0].upper()}{text[1:]}</{tag}>"
else:
return match.group(0)
# Regex pattern to match tags and their content
pattern = r'<([a-z0-9]+)(?:\s+[^>])?>([^<]+)</\1>'
modified_content = re.sub(pattern, capitalize_match, content, flags=re.IGNORECASE)
return modified_content
def combine_html_tags_into_same_line(content):
"""
Combine specific HTML tags into the same line by removing unnecessary line breaks.
code
Args:
content (str): The HTML content as a string.
Returns:
str: Modified content with specific tags combined into the same line.
"""
# Replace '>' followed by any whitespace and newline followed by '<' with '> <'
content = re.sub(r'>\s*\n\s*<', '> <', content)
return content
def combine_blockquote_lines_in_content(content):
"""
Combine broken lines in HTML content.
code
Args:
content (str): HTML content as a string.
Returns:
str: Modified content with combined blockquotes.
"""
lines = content.splitlines()
combined_lines = []
skip_next = False
for i, line in enumerate(lines):
if skip_next:
skip_next = False
continue
line_stripped = line.strip()
# Check for opening <blockquote> without closing tag
if '<blockquote>' in line_stripped and '</blockquote>' not in line_stripped:
if i + 1 < len(lines):
next_line = lines[i + 1].strip()
combined_line = line_stripped + ' ' + next_line
combined_lines.append(combined_line)
skip_next = True
else:
combined_lines.append(line_stripped)
# Check for closing </blockquote> without opening tag
elif '</blockquote>' in line_stripped and '<blockquote>' not in line_stripped:
if combined_lines:
combined_lines[-1] += ' ' + line_stripped
else:
combined_lines.append(line_stripped)
else:
combined_lines.append(line_stripped)
return '\n'.join(combined_lines)
def process_html_file(file_path):
"""
Apply processing functions to an HTML file.
code
Args:
file_path (str): Path to the HTML file.
"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# Remove <img> tags
content = re.sub(r'<img\b[^>]>', '', content, flags=re.IGNORECASE)
# Replace <h1 style="text-align: center;"> with <h1>
content = content.replace('<h1 style="text-align: center;">', '<h1>')
# Capitalize first letter inside tags
content = capitalize_first_letter_in_tags(content)
# Combine <blockquote> lines
content = combine_blockquote_lines_in_content(content)
# Remove blank lines
content = '\n'.join([line for line in content.splitlines() if line.strip()])
# Combine specific HTML tags into the same line
# content = combine_html_tags_into_same_line(content)
with open(file_path, 'w', encoding='utf-8') as file:
file.write(content)
logging.info(f"Processed HTML file: {file_path}")
except Exception as e:
logging.error(f"Error processing {file_path}: {e}")
def read_image_html_from_csv(csv_file_path):
"""
Read image HTML snippets from a CSV file.
code
Args:
csv_file_path (str): Path to the CSV file.
Returns:
dict: Dictionary mapping folder names to lists of image HTML lines.
"""
image_html_dict = {}
try:
with open(csv_file_path, 'r', encoding='utf-8') as file:
csv_reader = csv.reader(file)
for row in csv_reader:
if len(row) == 2:
folder_name = row[0].strip()
if folder_name not in image_html_dict:
image_html_dict[folder_name] = []
image_html_dict[folder_name].append(row[1].strip())
except Exception as e:
logging.error(f"Error reading CSV file {csv_file_path}: {e}")
return image_html_dict
def read_links_from_txt(txt_file_path):
"""
Read links from a TXT file.
code
Args:
txt_file_path (str): Path to the TXT file.
Returns:
list: List of links.
"""
try:
with open(txt_file_path, 'r', encoding='utf-8') as file:
return [line.strip() for line in file if line.strip()]
except Exception as e:
logging.error(f"Error reading TXT file {txt_file_path}: {e}")
return []
def insert_image_html(html_content, image_html_lines):
"""
Insert image HTML lines into the HTML content at specified positions.
code
Args:
html_content (str): Original HTML content.
image_html_lines (list): List of image HTML lines to insert.
Returns:
str: Modified HTML content with images inserted.
"""
content_length = len(html_content)
insertion_points = [
int(content_length * 0.125),
int(content_length * 0.375),
int(content_length * 0.625),
int(content_length * 0.875)
]
for i, point in enumerate(insertion_points):
start_of_line = html_content.rfind('\n', 0, point)
end_of_line = html_content.find('\n', point)
if start_of_line == -1:
start_of_line = 0
if end_of_line == -1:
end_of_line = content_length
if point - start_of_line <= end_of_line - point:
insertion_index = start_of_line + 1
else:
insertion_index = end_of_line + 1
if i < len(image_html_lines):
html_content = html_content[:insertion_index] + image_html_lines[i] + '\n' + html_content[insertion_index:]
return html_content
def insert_link_at_end(html_content, link):
"""
Insert a link at the end of the "Xem thêm: " section in the HTML content.
code
Args:
html_content (str): Original HTML content.
link (str): The link to insert.
Returns:
str: Modified HTML content with the link inserted.
"""
last_xem_them = html_content.rfind("Xem thêm: ")
if last_xem_them != -1:
insertion_point = last_xem_them + len("Xem thêm: ")
return html_content[:insertion_point] + link + html_content[insertion_point:]
else:
return html_content.rstrip() + "\nXem thêm: " + link
def insert_formatted_title(html_content):
"""
Insert a formatted title after the first tag.
code
Args:
html_content (str): Original HTML content.
Returns:
str: Modified HTML content with the formatted title inserted.
"""
# Find the title
title_match = re.search(r'<h1\b[^>]><strong>(.?)</strong></h1>', html_content, re.IGNORECASE)
if not title_match:
return html_content # No title found, return unchanged content
title = title_match.group(1)
# Prepare the formatted title
if '?' in title:
formatted_title = f'<h2><strong>{title} Bài viết dưới đây sẽ giải thích cho bạn:</strong></h2>'
else:
formatted_title = f'<h2><strong>{title} là gì? Bài viết dưới đây sẽ giải thích cho bạn:</strong></h2>'
# Find the first </blockquote> and insert the formatted title after it
blockquote_end = html_content.find('</blockquote>')
if blockquote_end != -1:
# Find the end of the line containing </blockquote>
end_of_line = html_content.find('\n', blockquote_end)
if end_of_line == -1:
end_of_line = len(html_content)
insertion_point = end_of_line + 1
return html_content[:insertion_point] + formatted_title + '\n' + html_content[insertion_point:]
else:
return html_content # No </blockquote> found, return unchanged content
def insert_title_in_blockquote(html_content):
"""
Insert the title followed by " - " immediately after the opening tag.
code
Args:
html_content (str): Original HTML content.
Returns:
str: Modified HTML content with the title inserted in blockquote.
"""
# Find the title
title_match = re.search(r'<h1\b[^>]><strong>(.*?)</strong></h1>', html_content, re.IGNORECASE)
if not title_match:
return html_content # No title found, return unchanged content
title = title_match.group(1)
# Find the first <blockquote> tag
blockquote_start = html_content.find('<blockquote>')
if blockquote_start == -1:
return html_content # No <blockquote> found, return unchanged content
# Insert the title followed by " - " just after the opening <blockquote> tag
insertion_point = blockquote_start + len('<blockquote>')
return html_content[:insertion_point] + f'{title} - ' + html_content[insertion_point:]
def remove_h1_strong_tags(folder_path):
"""
Remove and tags from the first line of HTML files.
code
Args:
folder_path (str): Path to the folder.
"""
for subdir, _, files in os.walk(folder_path):
for file in files:
if file.lower().endswith('.html'):
file_path = os.path.join(subdir, file)
try:
with open(file_path, 'r', encoding='utf-8') as file_obj:
lines = file_obj.readlines()
if lines:
lines[0] = lines[0].replace('<h1><strong>', '').replace('</strong></h1>', '')
with open(file_path, 'w', encoding='utf-8') as file_obj:
file_obj.writelines(lines)
logging.info(f"Removed <h1><strong> tags in: {file_path}")
except Exception as e:
logging.error(f"Error processing {file_path}: {e}")
def add_xem_them_to_html(folder_path):
"""
Add the string "Xem thêm: " at the end of each HTML file.
code
Args:
folder_path (str): Path to the folder.
"""
for root, _, files in os.walk(folder_path):
for file in files:
if file.lower().endswith('.html'):
file_path = os.path.join(root, file)
try:
# Ensure we append to the file
with open(file_path, 'a', encoding='utf-8') as f:
f.write('\n\nXem thêm: ')
logging.info(f"Added 'Xem thêm: ' to {file_path}")
except Exception as e:
logging.error(f"Error processing {file_path}: {e}")
def remove_quotes_from_html(folder_path):
"""
Remove all quotation marks from HTML files.
code
Args:
folder_path (str): Path to the folder.
"""
for root, _, files in os.walk(folder_path):
for file in files:
if file.lower().endswith('.html'):
file_path = os.path.join(root, file)
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Remove all quotation marks
content = content.replace('"', '').replace('“', '').replace('”', '')
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
logging.info(f"Removed quotation marks from: {file_path}")
except Exception as e:
logging.error(f"Error processing {file_path}: {e}")
def capitalize_after_colon_in_html(folder_path):
"""
Capitalize the first letter after a colon in HTML files.
code
Args:
folder_path (str): Path to the folder.
"""
pattern = re.compile(r'(: )([a-z])')
for root, _, files in os.walk(folder_path):
for file in files:
if file.lower().endswith('.html'):
file_path = os.path.join(root, file)
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Capitalize first letter after ": "
modified_content = pattern.sub(lambda m: m.group(1) + m.group(2).upper(), content)
if content != modified_content:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(modified_content)
logging.info(f"Capitalized text after colon in: {file_path}")
except Exception as e:
logging.error(f"Error processing {file_path}: {e}")
def process_html_files(folder_path):
"""
Process all HTML files in a folder, applying several cleaning functions.
code
Args:
folder_path (str): Path to the folder.
"""
for subdir, _, files in os.walk(folder_path):
for file in files:
if file.lower().endswith('.html'):
file_path = os.path.join(subdir, file)
process_html_file(file_path)
def insert_image_and_links_into_html(html_folder_path, csv_file_path, links_file_path):
"""
Insert image HTML snippets and links into HTML files based on CSV and TXT inputs.
code
Args:
html_folder_path (str): Path to the folder containing HTML files.
csv_file_path (str): Path to the CSV file containing image HTML snippets.
links_file_path (str): Path to the TXT file containing links.
"""
image_html_dict = read_image_html_from_csv(csv_file_path)
links = read_links_from_txt(links_file_path)
link_index = 0
all_html_files = []
for root, dirs, files in os.walk(html_folder_path):
for file in files:
if file.lower().endswith('.html'):
all_html_files.append(os.path.join(root, file))
all_html_files.sort()
for file_path in all_html_files:
folder_name = os.path.basename(os.path.dirname(file_path))
try:
with open(file_path, 'r', encoding='utf-8') as f:
html_content = f.read()
# Insert title in blockquote
html_content = insert_title_in_blockquote(html_content)
# Insert formatted title
html_content = insert_formatted_title(html_content)
# Insert image HTML if the folder name matches
if folder_name in image_html_dict:
html_content = insert_image_html(html_content, image_html_dict[folder_name])
# Insert link at the end
if link_index < len(links):
html_content = insert_link_at_end(html_content, links[link_index])
link_index += 1
# Save the modified content
with open(file_path, 'w', encoding='utf-8') as f:
f.write(html_content)
# Reset link index if we've used all links
if link_index >= len(links):
link_index = 0
logging.info(f"Inserted images and links into: {file_path}")
except Exception as e:
logging.error(f"Error processing {file_path}: {e}")
def main(image_folder_path, html_folder_path, csv_file_path, links_file_path):
"""
Main function to execute all processing steps.
code
Args:
image_folder_path (str): Path to the folder to process images.
html_folder_path (str): Path to the folder to process HTML files.
csv_file_path (str): Path to the CSV file for image HTML snippets.
links_file_path (str): Path to the TXT file containing links.
"""
# ------------------------------
# Image Processing
# ------------------------------
logging.info("Starting image processing...")
# Step 1: Delete unreadable image files
delete_unreadable_images(image_folder_path)
# Step 2: Delete blank (all white) images
delete_blank_images(image_folder_path)
# Step 3: Delete subfolders with few images
delete_subfolders_with_few_images(image_folder_path, threshold=3)
# Step 4: Trim image files in remaining subfolders
trim_image_files(image_folder_path, target_count=4)
# Step 5: Convert and resize images
convert_and_resize_images(image_folder_path, target_width=1200)
# Step 6: Rename .jpg files based on subfolder names
rename_jpg_files(image_folder_path)
logging.info("Image processing completed.")
# ------------------------------
# HTML Processing
# ------------------------------
logging.info("Starting HTML processing...")
# Step 6: Delete small HTML folders
delete_small_html_folders(html_folder_path, min_char_count=6000)
# Step 7: Process HTML files (cleaning)
process_html_files(html_folder_path)
# Step 8: Additional HTML modifications
capitalize_after_colon_in_html(html_folder_path)
# Uncomment the following lines if needed
# add_xem_them_to_html(html_folder_path)
# remove_h1_strong_tags(html_folder_path)
# remove_quotes_from_html(html_folder_path)
# Step 9: Insert images and links into HTML files
insert_image_and_links_into_html(html_folder_path, csv_file_path, links_file_path)
logging.info("HTML processing completed.")
if name == "main":
main(folder_path, html_folder_path, csv_file_path, links_file_path)
To ensure that your script retains the correct images (_1.jpg, _4.jpg, _5.jpg, and _7.jpg) after step 5, you need to modify the trim_image_files function. The current implementation sorts the images in descending order due to the reverse=True parameter, which results in higher-indexed images being retained and the lower-indexed ones being deleted. This is why you're seeing images like _6.jpg and _8.jpg being kept instead of the desired _1.jpg, _4.jpg, _5.jpg, and _7.jpg.
Here's how you can modify the script to achieve the desired outcome:
Remove the reverse=True Parameter:
Sorting without reversing will keep the lower-indexed images first, which aligns with your desired result.
Refine the Sorting Key:
Ensure that the sorting key correctly extracts the numerical index from the filenames and sorts them in ascending order.
Update the trim_image_files Function:
Implement the changes to correctly sort and trim the image files.
Below is the updated trim_image_files function with detailed comments explaining each change:
python
def trim_image_files(folder_path, target_count=4):
"""
Trim the number of image files in subfolders to a target count.
Args:
folder_path (str): Path to the parent folder.
target_count (int): Desired maximum number of images per subfolder. Default is 4.
"""
supported_formats = ('.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff', '.webp')
# Improved sorting logic to prioritize lower indices
def file_sort_key(x):
"""
Extract the numerical index from the filename for sorting.
Args:
x (str): Filename.
Returns:
int: Extracted index or a large number if not found.
"""
match = re.search(r'_(\d+)\.(jpg|jpeg|png|bmp|gif|tiff|webp)$', x, re.IGNORECASE)
if match:
return int(match.group(1))
else:
# If no index is found, assign a large number to push it to the end
return float('inf')
for root, _, files in os.walk(folder_path):
image_files = [f for f in files if f.lower().endswith(supported_formats)]
image_count = len(image_files)
if image_count <= target_count:
continue
# Sort images in ascending order based on their numerical index
image_files.sort(key=file_sort_key)
# Select files to delete beyond the target count
files_to_delete = image_files[target_count:]
for file in files_to_delete:
file_path = os.path.join(root, file)
try:
os.remove(file_path)
logging.info(f"Deleted excess image: {file_path}")
except Exception as e:
logging.error(f"Error deleting {file_path}: {e}")
Explanation of Changes:
Sorting Without Reversing:
Old Line:
python
image_files.sort(key=file_sort_key, reverse=True)
New Line:
python
image_files.sort(key=file_sort_key)
By removing reverse=True, the images are now sorted in ascending order based on their numerical index.
Enhanced Sorting Key:
The updated file_sort_key function now accurately extracts the numerical index from filenames like 5-cach-phoi-o-voi-ao-blazer-thanh-lich_1.jpg by using a regular expression. If the index is not found, it assigns a very high value (float('inf')) to ensure such files are placed at the end.
Robust Sorting Mechanism:
This ensures that files are sorted correctly based on their numerical suffix, preventing unintended deletions.
Full Updated Script Snippet:
Below is the complete updated trim_image_files function integrated into your original script for clarity.
python
def trim_image_files(folder_path, target_count=4):
"""
Trim the number of image files in subfolders to a target count.
Args:
folder_path (str): Path to the parent folder.
target_count (int): Desired maximum number of images per subfolder. Default is 4.
"""
supported_formats = ('.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff', '.webp')
# Improved sorting logic to prioritize lower indices
def file_sort_key(x):
"""
Extract the numerical index from the filename for sorting.
Args:
x (str): Filename.
Returns:
int: Extracted index or a large number if not found.
"""
match = re.search(r'_(\d+)\.(jpg|jpeg|png|bmp|gif|tiff|webp)$', x, re.IGNORECASE)
if match:
return int(match.group(1))
else:
# If no index is found, assign a large number to push it to the end
return float('inf')
for root, _, files in os.walk(folder_path):
image_files = [f for f in files if f.lower().endswith(supported_formats)]
image_count = len(image_files)
if image_count <= target_count:
continue
# Sort images in ascending order based on their numerical index
image_files.sort(key=file_sort_key)
# Select files to delete beyond the target count
files_to_delete = image_files[target_count:]
for file in files_to_delete:
file_path = os.path.join(root, file)
try:
os.remove(file_path)
logging.info(f"Deleted excess image: {file_path}")
except Exception as e:
logging.error(f"Error deleting {file_path}: {e}")
Final Steps:
Replace the Existing Function:
Replace your current trim_image_files function with the updated version provided above.
Run the Script:
Execute your script as usual. It will now retain the lowest-indexed images up to the target_count (default is 4), ensuring that _1.jpg, _4.jpg, _5.jpg, and _7.jpg are kept while deleting _9.jpg and any other excess images.
Verify the Results:
After running the script, verify that only the desired images remain in your folder:
Backup Your Data:
Before running the modified script, create a backup of your images to prevent accidental data loss.
Logging Verification:
Review the logs generated by the script to ensure that the correct files are being deleted.
Flexible Target Count:
If you need to retain a different number of images per folder in the future, you can adjust the target_count parameter accordingly.
Conclusion:
By implementing the above changes, your script will correctly retain the desired images based on their numerical indices, eliminating the unintended deletion of important files. This ensures your image processing workflow remains error-free and aligns with your specific requirements.