-
Notifications
You must be signed in to change notification settings - Fork 1
/
format converter.py
110 lines (83 loc) · 3.91 KB
/
format converter.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
import os
import logging
from PIL import Image
input_folder = r"algae"
output_folder = r"insect"
# Create a logger object to log errors and warnings
logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)
handler = logging.FileHandler('image_converter.log')
handler.setLevel(logging.WARNING)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
for filename in os.listdir(input_folder):
if filename.endswith(".jpeg"):
try:
# Open the image using Pillow
image = Image.open(os.path.join(input_folder, filename))
# Convert the image to RGB format
rgb_image = image.convert("RGB")
# Save the JPG image to the output folder
output_filename = filename.replace(".jpeg", ".jpg")
rgb_image.save(os.path.join(output_folder, output_filename), quality=50)
except Exception as e:
print(f"Error converting {filename}: {e}")
elif filename.endswith(".jpg"):
try:
# Open the image using Pillow
image = Image.open(os.path.join(input_folder, filename))
# Convert the image to RGB format
rgb_image = image.convert("RGB")
# Save the JPG image to the output folder
rgb_image.save(os.path.join(output_folder, filename), quality=50)
except Exception as e:
print(f"Error converting {filename}: {e}")
input_folder = r"insect"
output_folder = r"palgae"
target_size = (256, 256)
for filename in os.listdir(input_folder):
if filename.endswith(".jpeg") or filename.endswith(".jpg"):
try:
# Open the image using Pillow
image = Image.open(os.path.join(input_folder, filename))
# Get the original image size
width, height = image.size
# Find the larger dimension
larger_dim = max(width, height)
# Calculate the scaling factor
scale_factor = target_size[0] / larger_dim
# Calculate the new size
new_size = (int(scale_factor * width), int(scale_factor * height))
# Resize the image
image = image.resize(new_size, resample=Image.LANCZOS)
# Create a blank background image of target size
background = Image.new('RGB', target_size, (255, 255, 255))
# Paste the resized image on the center of background image
offset = ((target_size[0] - new_size[0]) // 2, (target_size[1] - new_size[1]) // 2)
background.paste(image, offset)
# Save the image to the output folder
background.save(os.path.join(output_folder, filename), optimize=True, quality=50)
except FileNotFoundError:
print(f"File {filename} not found in input folder")
except Exception as e:
print(f"Error converting {filename}: {e}")
input_folder = r"palgae"
output_folder = r"png_allok"
for filename in os.listdir(input_folder):
if filename.endswith(".jpeg") or filename.endswith(".jpg"):
try:
# Open the image using Pillow
image = Image.open(os.path.join(input_folder, filename))
# Convert the image to RGBA format
rgba_image = image.convert("RGBA")
# Reduce the size of the PNG image
png_image = rgba_image.save(os.path.join(output_folder, filename.replace(".jpg" or ".jpeg", ".png")), optimize=True, quality=50)
# Check if the save() method succeeded
if png_image:
# Save the PNG image to the output folder
png_image.save(os.path.join(output_folder, filename.replace(".jpg" or ".jpeg", ".png")), optimize=True, quality=50)
except FileNotFoundError:
logger.warning(f"File {filename} not found in input folder")
except Exception as e:
logger.error(f"Error converting {filename}: {e}")