-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
153 lines (119 loc) · 4.54 KB
/
main.py
File metadata and controls
153 lines (119 loc) · 4.54 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
__project__ = "Image To Webp"
__version__ = "1.0.0"
__author__ = "Mefamex"
__email__ = "info@mefamex.com"
__url__ = "https://mefamex.com/projects/image_to_webp/"
__license__ = "MIT"
__description__ = "Image To Webp, çalıştığı klasordeki tüm resimleri webp formatına dönüştürmenizi sağlayan bir Python projesidir."
__url_github__ = "https://github.com/mefamex/image_to_webp"
__status__ = "Production"
__date__ = "2025-02-09"
__date_modify__ = "2025-02-09"
__python_version__ = ">=3.13"
__dependencies__ = {
"python": ">=3.13",
"pillow": ">=11.1.0" ,
"roboflow": ">=1.1.53"
}
___doc___ = """
Proje Adi: Image To Webp
Yazar: Mefamex (info@mefamex.com) (https://mefamex.com)
Lisans: MIT
Aciklama:
Image To Webp, çalıştığı klasordeki tüm resimleri webp formatına dönüştürmenizi sağlayan bir Python projesidir.
Ozellikler:
- Resimleri webp formatına dönüştürme
- Alt dizinleri kontrol etme
- Dosya isimlerini kontrol etme (. ile basliyanlar alinmaz)
- Resimleri dönüştürürken kalite ayarlaması
- Resimleri dönüştürürken dosya uzantısı kontrolü
Kurulum:
- 1-) dizinde image_to_webp.exe calistirma
- 2-) Proje klonlama: git clone https://github.com/mefamex/image_to_webp.git
- Gerekli bağimliliklari kurma: pip install -r requirements.txt
- Proje calistirma: python main.py
"""
import os, sys
from time import sleep
from PIL import Image
"""
dont forget to update pillow and roboflow
"""
def png_to_webp(input_path:str, quality:int=100):
"""
Converts PNG image to WEBP format.
:param input_path: Path to the input image file (PNG, JPG, etc.)
:type input_path: str
:param quality: Quality of WEBP output (0-100), defaults to 100
:type quality: int
:return: saved path
:type return: str
"""
try:
img = Image.open(input_path)
image_dir = os.path.dirname(input_path)
saving_dir = os.path.join(image_dir, "converted")
new_name = os.path.basename(input_path)
new_name = new_name[:new_name.rfind('.')] + ".webp"
new_path = os.path.join(saving_dir, new_name)
if not os.path.exists(saving_dir): os.mkdir(saving_dir)
img.save(new_path, "webp", quality=quality)
print("+ DONE :",os.path.basename(input_path),new_path)
return new_path
except Exception as e: print(f"Hata oluştu {input_path} , {new_path} : {e}")
# Kullanım örneği:
#input_image = "input.png"
#png_to_webp(input_image)
converted={}
passed={}
NotImage = {}
for path, folder, files in os.walk(os.getcwd()):
isPass = False
for folder in path.split(os.getcwd())[1].split(os.sep):
if folder.startswith('.'):
passed[folder] = path
isPass = True
break
if isPass: continue
print("")
isExistImageFiles = False
for file in files:
combined_path= os.path.join(path,file)
if os.path.splitext(combined_path)[1].lower() in Image.registered_extensions() and not file.startswith("."):
try:
with Image.open(combined_path) as img:
saved_path = png_to_webp(combined_path)
converted[os.path.basename(combined_path)] = saved_path
except Exception as e:print(f"Hata oluştu {combined_path} : {e}")
else:
print("- PASS :", combined_path)
NotImage[file] = combined_path
sleep(1)
print("\n\n\n=============================================================\n CONVERTING DONE \n=============================================================\n\n")
sleep(1)
if len(NotImage)>0:
print(" PASSED FILES (not image or starts with '.'):\n")
for q in NotImage:
print(f"{q} -> {NotImage[q].split(os.getcwd())[1]}")
sleep(0.1)
else: print("No files were found without the image extension.")
print("\n=============================================================\n")
sleep(1)
if len(passed)>0:
print("\n\n PASSED FOLDERS: (starts with '.')\n")
for q in passed:
print(f"{q} -> {passed[q].split(os.getcwd())[1]}")
sleep(0.1)
else: print("No folders were skipped")
print("\n=============================================================\n")
sleep(1)
if len(converted)>0:
print("\n\n CONVERTED FILES:\n")
for q in converted:
print(f"{q} -> {converted[q].split(os.getcwd())[1]}")
sleep(0.1)
else: print("No Files converted")
print("\n=============================================================\n")
print("\n exit program...\n")
sleep(100000)