Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jpg-conversion): add logic specific to handling JPG conversions #21

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/formaverter/image_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,28 @@ def convert(self) -> None:
)
return
else:
self._convert_image(input_ext, output_ext)
# Handle JPG conversion separately
if self.output_format.lower() == 'jpg':
self._convert_to_jpg(input_ext, output_ext)
else:
self._convert_image(input_ext, output_ext)

def _convert_to_jpg(self, input_ext: str, output_ext: str) -> None:
img = Image.open(self.input_path)

# Ensure the output format is actually JPG
if output_ext.lower() != 'jpg':
output_ext = 'jpg'

# Save transparency metadata for palette images w/ transparency
if img.mode == 'P' and img.info.get('transparency'):
img = img.convert('RGBA')

# Convert to RGB
rgb_img = img.convert('RGB')

# Save as JPG with quality 95 (adjust as needed)
rgb_img.save(self.output_path, 'JPEG', quality=95)

def _convert_image(self, input_ext: str, output_ext: str) -> None:
"""
Expand Down
Loading