Skip to content

Commit

Permalink
Merge pull request #41 from leslievan/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
leslievan authored Apr 25, 2023
2 parents 9accff7 + ec78892 commit 3c11143
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 31 deletions.
34 changes: 24 additions & 10 deletions entity/image_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
from enum import Enum
from pathlib import Path

import piexif
from PIL import Image
from PIL.Image import Transpose
from dateutil import parser

from entity.config import ElementConfig
from enums.constant import CAMERA_MAKE_CAMERA_MODEL_VALUE
from enums.constant import CAMERA_MODEL_LENS_MODEL_VALUE
from enums.constant import CUSTOM_VALUE
from enums.constant import DATETIME_VALUE
from enums.constant import DATE_VALUE
from enums.constant import LENS_MAKE_LENS_MODEL_VALUE
from enums.constant import LENS_VALUE
from enums.constant import MAKE_VALUE
from enums.constant import CAMERA_MODEL_LENS_MODEL_VALUE
from enums.constant import MODEL_VALUE
from enums.constant import PARAM_VALUE
from enums.constant import TOTAL_PIXEL_VALUE
Expand Down Expand Up @@ -92,16 +93,18 @@ def __init__(self, path: Path):
if ExifId.EXPOSURE_TIME.value in self.exif else '0s'
# 感光度
self.iso: str = self.exif[ExifId.ISO.value] if ExifId.ISO.value in self.exif else '0'

# 修正图像方向
self.orientation = self.exif[ExifId.ORIENTATION.value] if ExifId.ORIENTATION.value in self.exif else 0
if self.orientation == 3:
self.img = self.img.transpose(Transpose.ROTATE_180)
elif self.orientation == 6:
self.orientation = self.exif[ExifId.ORIENTATION.value] if ExifId.ORIENTATION.value in self.exif else 1
if self.orientation == "Rotate 0":
pass
elif self.orientation == "Rotate 90 CW":
self.img = self.img.transpose(Transpose.ROTATE_270)
elif self.orientation == 8:
elif self.orientation == "Rotate 180":
self.img = self.img.transpose(Transpose.ROTATE_180)
elif self.orientation == "Rotate 270 CW":
self.img = self.img.transpose(Transpose.ROTATE_90)
self.exif[ExifId.ORIENTATION.value] = 1
else:
pass

# 水印设置
self.custom = '无'
Expand Down Expand Up @@ -220,5 +223,16 @@ def close(self):
self.img.close()
self.watermark_img.close()

def get_total_pixel(self):
return self._total_pixel
def save(self, target_path, quality=100):
if self.orientation == "Rotate 0":
pass
elif self.orientation == "Rotate 90 CW":
self.watermark_img = self.watermark_img.transpose(Transpose.ROTATE_90)
elif self.orientation == "Rotate 180":
self.watermark_img = self.watermark_img.transpose(Transpose.ROTATE_180)
elif self.orientation == "Rotate 270 CW":
self.watermark_img = self.watermark_img.transpose(Transpose.ROTATE_270)
else:
pass

self.watermark_img.save(target_path, quality=quality, encoding='utf-8', exif=self.img.info['exif'])
8 changes: 3 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
from init import WATERMARK_PROCESSOR
from init import config
from init import root_menu
from utils import ENCODING
from utils import get_file_list
from utils import insert_exif


def processing():
Expand Down Expand Up @@ -67,12 +67,10 @@ def processing():
processor_chain.process(container)

# 保存图片
target_path = Path(config.get_output_dir()).joinpath(source_path.name)
target_path = Path(config.get_output_dir(), encoding=ENCODING).joinpath(source_path.name)

with container.get_watermark_img() as watermark:
watermark.save(target_path, quality=config.get_quality())
container.save(target_path, quality=config.get_quality())
container.close()
insert_exif(container.exif, target_path)
print('处理完成,文件已输出至 output 文件夹中,请点击任意键退出或直接关闭'.format(len(file_list)))
input()
state = -1
Expand Down
2 changes: 1 addition & 1 deletion main.spec
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ coll = COLLECT(
strip=False,
upx=True,
upx_exclude=[],
name='semi-utils-1_5_4',
name='semi-utils-1_5_5',
)
26 changes: 11 additions & 15 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
import subprocess
from pathlib import Path

import piexif
from PIL import Image
from PIL import ImageDraw
from PIL import ImageOps
from PIL.ExifTags import TAGS

if platform.system() == 'Windows':
exiftool_path = './exiftool/exiftool.exe'
encoding = 'gbk'
EXIFTOOL_PATH = './exiftool/exiftool.exe'
ENCODING = 'gbk'
else:
exiftool_path = './exiftool/exiftool'
encoding = 'utf-8'
EXIFTOOL_PATH = './exiftool/exiftool'
ENCODING = 'utf-8'


def get_file_list(path):
Expand All @@ -23,7 +22,7 @@ def get_file_list(path):
:param path: 路径
:return: 文件名
"""
path = Path(path)
path = Path(path, encoding=ENCODING)
return [file_path for file_path in path.iterdir()
if file_path.is_file() and file_path.suffix in ['.jpg', '.jpeg', '.JPG', '.JPEG']]

Expand All @@ -38,9 +37,9 @@ def get_exif(path) -> dict:
try:
# 如果 exif 中不存在镜头信息,用 exiftool 读取
# if 'LensModel' not in _exif:
output = subprocess.check_output([exiftool_path, '-charset', 'UTF8', path])
output = subprocess.check_output([EXIFTOOL_PATH, '-charset', 'UTF8', path])

output = output.decode(encoding)
output = output.decode(ENCODING)
lines = output.splitlines()
utf8_lines = [line for line in lines]

Expand All @@ -58,6 +57,7 @@ def get_exif(path) -> dict:
exif_dict[key] = value
except UnicodeDecodeError as e:
# 如果 exiftool 无法读取,用 piexif 读取
# TODO 用 piexif 读取时处理 Orientation
with Image.open(path) as image:
info = image._getexif()
if info:
Expand All @@ -70,19 +70,15 @@ def get_exif(path) -> dict:
return exif_dict


def insert_exif(exif, target_path) -> None:
def insert_exif(source_path, target_path) -> None:
"""
复制照片的 exif 信息
:param exif: exif 信息
:param source_path: 源照片路径
:param target_path: 目的照片路径
"""
try:
# 将 exif 信息转换为字节串
src_exif_bytes = piexif.dump(exif)

# 将源照片的 exif 信息写入 target_path
piexif.insert(src_exif_bytes, str(target_path))

subprocess.check_output([EXIFTOOL_PATH, '-tagsfromfile', source_path, '-overwrite_original', target_path])
except ValueError:
pass

Expand Down

0 comments on commit 3c11143

Please sign in to comment.