Skip to content

Commit fe2285f

Browse files
committed
fix: fix the output file size
Signed-off-by: Nascs Fang <[email protected]>
1 parent be2ec35 commit fe2285f

File tree

2 files changed

+66
-38
lines changed

2 files changed

+66
-38
lines changed

docs/common/dev/_custom_logo.mdx

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,46 @@ pip install Pillow
2121
```py
2222

2323
from PIL import Image
24+
import os
2425
import sys
2526

26-
class BMPImageInfo:
27-
def __init__(self, image_path):
28-
self.image_path = image_path
29-
30-
def get_image_info(self):
31-
with Image.open(self.image_path) as img:
32-
27+
class ImageConverter:
28+
def __init__(self, input_path, output_path):
29+
self.input_path = input_path
30+
self.output_path = output_path
31+
self.target_size = 0.5 * 1024 * 1024 # 0.5 MB in bytes (512 KB)
32+
33+
def convert_to_bmp(self):
34+
with Image.open(self.input_path) as img:
35+
# Convert to RGB since BMP doesn't support transparency
36+
img = img.convert("RGB")
37+
38+
# Resize the image if the file size exceeds 0.5 MB
39+
img = self.resize_to_fit(img)
40+
41+
# Save as BMP
42+
img.save(self.output_path, format='BMP')
43+
44+
def resize_to_fit(self, img):
45+
# Gradually reduce the size of the image until it fits within the target size
46+
while True:
47+
img.save(self.output_path, format='BMP')
48+
if os.path.getsize(self.output_path) <= self.target_size:
49+
break
50+
# Reduce the size by 10%
3351
width, height = img.size
52+
img = img.resize((int(width * 0.9), int(height * 0.9)), Image.ANTIALIAS)
53+
return img
3454

35-
total_pixels = width * height
36-
return width, height, total_pixels
37-
38-
def main(image_file):
39-
image_info = BMPImageInfo(image_file)
40-
width, height, total_pixels = image_info.get_image_info()
41-
print(f"Image Width: {width} pixels")
42-
print(f"Image Height: {height} pixels")
43-
print(f"Total Pixels: {total_pixels}")
55+
def main(input_file, output_file):
56+
converter = ImageConverter(input_file, output_file)
57+
converter.convert_to_bmp()
4458

4559
if __name__ == "__main__":
46-
if len(sys.argv) != 2:
47-
print("Usage: python3 get_bmp_info.py <image.bmp>")
60+
if len(sys.argv) != 3:
61+
print("Usage: python3 convert_logo.py <input.jpg> <output.bmp>")
4862
else:
49-
main(sys.argv[1])
63+
main(sys.argv[1], sys.argv[2])
5064

5165
```
5266

i18n/en/docusaurus-plugin-content-docs/current/common/dev/_custom_logo.mdx

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,46 @@ pip install Pillow
2121
```py
2222

2323
from PIL import Image
24+
import os
2425
import sys
2526

26-
class BMPImageInfo:
27-
def __init__(self, image_path):
28-
self.image_path = image_path
29-
30-
def get_image_info(self):
31-
with Image.open(self.image_path) as img:
32-
27+
class ImageConverter:
28+
def __init__(self, input_path, output_path):
29+
self.input_path = input_path
30+
self.output_path = output_path
31+
self.target_size = 0.5 * 1024 * 1024 # 0.5 MB in bytes (512 KB)
32+
33+
def convert_to_bmp(self):
34+
with Image.open(self.input_path) as img:
35+
# Convert to RGB since BMP doesn't support transparency
36+
img = img.convert("RGB")
37+
38+
# Resize the image if the file size exceeds 0.5 MB
39+
img = self.resize_to_fit(img)
40+
41+
# Save as BMP
42+
img.save(self.output_path, format='BMP')
43+
44+
def resize_to_fit(self, img):
45+
# Gradually reduce the size of the image until it fits within the target size
46+
while True:
47+
img.save(self.output_path, format='BMP')
48+
if os.path.getsize(self.output_path) <= self.target_size:
49+
break
50+
# Reduce the size by 10%
3351
width, height = img.size
52+
img = img.resize((int(width * 0.9), int(height * 0.9)), Image.ANTIALIAS)
53+
return img
3454

35-
total_pixels = width * height
36-
return width, height, total_pixels
37-
38-
def main(image_file):
39-
image_info = BMPImageInfo(image_file)
40-
width, height, total_pixels = image_info.get_image_info()
41-
print(f"Image Width: {width} pixels")
42-
print(f"Image Height: {height} pixels")
43-
print(f"Total Pixels: {total_pixels}")
55+
def main(input_file, output_file):
56+
converter = ImageConverter(input_file, output_file)
57+
converter.convert_to_bmp()
4458

4559
if __name__ == "__main__":
46-
if len(sys.argv) != 2:
47-
print("Usage: python3 get_bmp_info.py <image.bmp>")
60+
if len(sys.argv) != 3:
61+
print("Usage: python3 convert_logo.py <input.jpg> <output.bmp>")
4862
else:
49-
main(sys.argv[1])
63+
main(sys.argv[1], sys.argv[2])
5064

5165
```
5266

0 commit comments

Comments
 (0)