Untitled
unknown
plain_text
a year ago
1.5 kB
8
Indexable
import os
import glob
def remove_jpg_images(directory):
# Lấy danh sách tất cả các file ảnh .jpg trong thư mục
jpg_files = glob.glob(os.path.join(directory, "*.jpg"))
# Sắp xếp các file để đảm bảo chúng theo thứ tự
jpg_files.sort()
# Danh sách các chỉ số cần giữ lại: 5, 25, 45, 55
keep_indices = {5, 25, 45, 55}
# Thêm 16 chỉ số theo cấp số cộng với khoảng cách là 5, bắt đầu từ 1 (1, 6, 11,...)
additional_keeps = 16
start = 1
while len(keep_indices) < (additional_keeps + 4): # Thêm 16 ảnh nữa cùng với 4 ảnh đặc biệt
if start not in keep_indices: # Đảm bảo không trùng với các chỉ số đã có
keep_indices.add(start)
start += 5 # Cấp số cộng với khoảng cách là 5
# Duyệt qua tất cả các ảnh và giữ lại các ảnh có trong danh sách chỉ số cần giữ
for idx, file in enumerate(jpg_files, start=1):
if idx not in keep_indices:
try:
os.remove(file)
print(f"Đã xóa: {file}")
except OSError as e:
print(f"Lỗi khi xóa {file}: {e}")
else:
print(f"Giữ lại: {file}")
if __name__ == "__main__":
# Đường dẫn đến thư mục chứa ảnh .jpg
directory = "./images" # Thay đổi đường dẫn tới thư mục chứa ảnh của bạn
remove_jpg_images(directory)Editor is loading...
Leave a Comment