Skip to content

Commit

Permalink
Merge pull request #21 from Daethyra/2.0.1
Browse files Browse the repository at this point in the history
fix(jpg-conversion): add logic specific to handling JPG conversions
  • Loading branch information
Daethyra authored Dec 16, 2024
2 parents f990ac8 + d0ac1d0 commit 1b3ecdb
Showing 1 changed file with 22 additions and 1 deletion.
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

0 comments on commit 1b3ecdb

Please sign in to comment.