Skip to content
Open
Show file tree
Hide file tree
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
24 changes: 23 additions & 1 deletion _ePubMaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import threading
import traceback
import uuid
import piexif
from datetime import datetime, timedelta
from pathlib import Path
from typing import Optional, List
Expand Down Expand Up @@ -101,6 +102,7 @@ def __init__(self, master, input_dir, file, name, wrap_pages, grayscale, max_wid
self.max_width = max_width
self.max_height = max_height
self.wrap_pages = wrap_pages
self.orientation_lut = {3: 180, 6: 270, 8: 90,}

def run(self):
try:
Expand Down Expand Up @@ -187,6 +189,16 @@ def assign_image_ids(self):
image["id"] = f"image_{count:0{padding_width}}"
image["filename"] = image["id"] + image["extension"]

def pull_exif_orientation(self, image_data):
try:
exif_dict = piexif.load(image_data.info['exif'])
orientation = exif_dict['0th'][piexif.ImageIFD.Orientation]
if orientation in [3, 6, 8]:
return orientation
except (KeyError, AttributeError, IndexError):
pass
return 0

def write_images(self):
if self.progress:
self.progress.progress_set_maximum(len(self.images))
Expand All @@ -199,13 +211,23 @@ def write_images(self):
image_data: PIL.Image.Image = PIL.Image.open(image["source"])
image["width"], image["height"] = image_data.size
image["type"] = image_data.get_format_mimetype()
should_rotate = self.pull_exif_orientation(image_data)
should_resize = (self.max_width and self.max_width < image["width"]) or (
self.max_height and self.max_height < image["height"])
should_grayscale = self.grayscale and image_data.mode != "L"
if not should_grayscale and not should_resize:
if not should_grayscale and not should_resize and not should_rotate:
self.zip.write(image["source"], output)
else:
image_format = image_data.format
if should_rotate:
image_data = image_data.rotate(self.orientation_lut[should_rotate], expand=True)
# if should_rotate == 3:
# image_data = image_data.rotate(180, expand=True)
# elif should_rotate == 6:
# image_data = image_data.rotate(270, expand=True)
# elif should_rotate == 8:
# image_data = image_data.rotate(90, expand=True)
image["width"], image["height"] = image_data.size
if should_resize:
width_scale = image["width"] / self.max_width if self.max_width else 1.0
height_scale = image["height"] / self.max_height if self.max_height else 1.0
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
jinja2~=2.11.3
Pillow~=8.1.0
Pillow~=11.1.0
piexif~=1.1.3