-
Notifications
You must be signed in to change notification settings - Fork 0
/
jpg_to_jxl_legacy.py
136 lines (108 loc) · 5.97 KB
/
jpg_to_jxl_legacy.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# import os
# import subprocess
# import sys
# import time
# try:
# from colorama import Fore, Style
# except ImportError:
# subprocess.check_call([sys.executable, "-m", "pip", "install", "colorama"])
# from colorama import Fore, Style
# def check_cjxl_installed():
# try:
# subprocess.run(["cjxl", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# return True
# except (subprocess.CalledProcessError, FileNotFoundError):
# return False
# def install_cjxl():
# if os.name == "nt": # Windows
# print("cjxl not found. Please install cjxl manually.")
# # Добавьте инструкции для установки cjxl на Windows
# else: # Linux / MacOS
# print("cjxl not found. Please install cjxl manually.")
# # Добавьте инструкции для установки cjxl на Linux / MacOS
# def format_time(seconds):
# mins, secs = divmod(seconds, 60)
# hours, mins = divmod(mins, 60)
# return f"{int(hours)}h:{int(mins)}m:{int(secs)}s"
# def apply_cjxl_command(directory_path, quality):
# total_start_time = time.time() # Start time of the script execution
# for root, dirs, files in os.walk(directory_path):
# for file in files:
# if file.lower().endswith('.jpg'):
# input_file = os.path.join(root, file)
# output_folder = os.path.join(root, os.path.basename(root) + '_jxl')
# os.makedirs(output_folder, exist_ok=True)
# output_file = os.path.join(output_folder, '.'.join(file.split('.')[:-1]) + f'.jxl')
# command = f'cjxl "{input_file}" "{output_file}" --lossless_jpeg 0 -q {quality} -v'
# start_time = time.time() # Start time of file encoding
# os.system(command)
# end_time = time.time() # End time of file encoding
# input_size = os.path.getsize(input_file)
# output_size = os.path.getsize(output_file)
# file_time = end_time - start_time
# print(f"{Fore.RED}Input file: {input_file}, Size: {int(input_size / 1048576)} MB{Style.RESET_ALL}")
# print(f"{Fore.RED}Output file: {output_file}, Size: {int(output_size / 1048576)} MB{Style.RESET_ALL}")
# print(f"{Fore.YELLOW}File encoding time: {file_time:.2f} seconds ({format_time(file_time)}){Style.RESET_ALL}")
# total_end_time = time.time() # End time of the script execution
# total_time = total_end_time - total_start_time
# print(f"{Fore.GREEN}Total script execution time: {total_time:.2f} seconds ({format_time(total_time)}){Style.RESET_ALL}")
# if not check_cjxl_installed():
# install_cjxl()
# directory_path = input("Enter the directory path: ")
# quality = input("Enter the quality (0-100): ")
# apply_cjxl_command(directory_path, quality)
#-----------------------------------------
# import os
# import subprocess
# import sys
# import time
# try:
# from colorama import Fore, Style
# except ImportError:
# subprocess.check_call([sys.executable, "-m", "pip", "install", "colorama"])
# from colorama import Fore, Style
# def check_cjxl_installed():
# try:
# subprocess.run(["cjxl", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# return True
# except FileNotFoundError:
# print("Warning: cjxl not found. Please install cjxl manually.")
# return False
# def format_time(seconds):
# mins, secs = divmod(seconds, 60)
# hours, mins = divmod(mins, 60)
# return f"{int(hours)}h:{int(mins)}m:{int(secs)}s"
# def apply_cjxl_command(directory_path, quality, replace_files):
# total_start_time = time.time() # Start time of the script execution
# for root, dirs, files in os.walk(directory_path):
# for file in files:
# if file.lower().endswith('.jpg'):
# input_file = os.path.join(root, file)
# if replace_files:
# output_folder = root
# else:
# output_folder = os.path.join(root, os.path.basename(root) + '_jxl')
# os.makedirs(output_folder, exist_ok=True)
# output_file = os.path.join(output_folder, '.'.join(file.split('.')[:-1]) + f'.jxl')
# command = f'cjxl "{input_file}" "{output_file}" --lossless_jpeg 0 -q {quality} -v'
# start_time = time.time() # Start time of file encoding
# os.system(command)
# end_time = time.time() # End time of file encoding
# input_size = os.path.getsize(input_file)
# output_size = os.path.getsize(output_file)
# file_time = end_time - start_time
# print(f"{Fore.RED}Input file: {input_file}, Size: {int(input_size / 1048576)} MB{Style.RESET_ALL}")
# print(f"{Fore.RED}Output file: {output_file}, Size: {int(output_size / 1048576)} MB{Style.RESET_ALL}")
# print(f"{Fore.YELLOW}File encoding time: {file_time:.2f} seconds ({format_time(file_time)}){Style.RESET_ALL}")
# # Удаление оригинального файла
# os.remove(input_file)
# print(f"{Fore.GREEN}Original file {input_file} has been removed.{Style.RESET_ALL}")
# total_end_time = time.time() # End time of the script execution
# total_time = total_end_time - total_start_time
# print(f"{Fore.GREEN}Total script execution time: {total_time:.2f} seconds ({format_time(total_time)}){Style.RESET_ALL}")
# if not check_cjxl_installed():
# sys.exit()
# directory_path = input("Enter the directory path: ")
# quality = input("Enter the quality (0-100): ")
# replace_files = input("Do you want to replace files in destination folder? (Y/N): ").lower().strip() == 'y'
# apply_cjxl_command(directory_path, quality, replace_files)