Skip to content

Commit

Permalink
feat: 添加TIFF处理类
Browse files Browse the repository at this point in the history
  • Loading branch information
CheaterScript committed Jun 8, 2022
1 parent 6ff942f commit c27c95e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
7 changes: 5 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@

from PyQt6 import QtWidgets, QtCore
from PyQt6.QtGui import QGuiApplication
from tools.tiff_processer import TIFF
from ui.Ui_MainWindow import Ui_MainWindow

from tools.raw_processer import Raw
from tools.png_processer import PNG
from tools.jpg_processer import JPG

# 支持的格式, 不在其中的会跳过处理
FILE_TYPES = ('.jpg', '.jpge', '.png', '.raw')
FILE_TYPES = ('.jpg', '.jpge', '.png', '.raw', '.tif', '.tiff')


class ToolWindow(QtWidgets.QMainWindow, Ui_MainWindow):
Expand All @@ -24,7 +25,7 @@ def __init__(self, parent=None):

def openFile(self):
fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self, "打开文件", os.getcwd(
), "All Images(*.jpg;*jpge;*.png;*.raw);;PNG(*.png);;RAW(*.raw);;JEPE(*.jpg;*jpge)")
), "All Images(*.jpg;*jpge;*.png;*.raw;*.tiff;*.tif);;PNG(*.png);;Photoshop RAW(*.raw);;JEPE(*.jpg;*jpge);;TIFF(*.tif;*.tiff)")

self.lineEdit_input.setText(fileName)

Expand Down Expand Up @@ -152,6 +153,8 @@ def handle(self):
processer = PNG(inputPath, outputPath, row, col)
if extension_name == '.jpg' or extension_name == '.jpge':
processer = JPG(inputPath, outputPath, row, col)
if extension_name == '.tif' or extension_name == '.tiff':
processer = TIFF(inputPath, outputPath, row, col)

if not processer:
self.complete()
Expand Down
1 change: 1 addition & 0 deletions test.raw

Large diffs are not rendered by default.

Binary file added test.tif
Binary file not shown.
1 change: 0 additions & 1 deletion tools/png_processer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def slice(self) -> None:
print("正在处理%d/%d张……" % (i*self.rows+j, total))

def save(self, img, index) -> None:
print(img.shape)
cv2.imencode(self.inputPath.suffix, img, [int(cv2.IMWRITE_PNG_COMPRESSION), 0])[
1].tofile("%s/%s_%03d%s" %
(self.output, self.inputPath.stem, index, self.inputPath.suffix))
42 changes: 42 additions & 0 deletions tools/tiff_processer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import cv2
from tools.processer import Processer
from pathlib import Path
import math
from PyQt6 import QtWidgets
import numpy as np


class TIFF(Processer):
def load(self) -> None:
self.img = cv2.imdecode(np.fromfile(
self.input, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
self.inputPath = Path(self.input)

def slice(self) -> None:
self.load()
total = self.rows * self.cols
shape = self.img.shape
singleWidth = math.ceil(shape[1] / self.cols)
singleHeight = math.ceil(shape[0] / self.rows)
print(shape)
if singleWidth * self.cols != shape[0] or singleHeight * self.rows != shape[1]:
self.img = cv2.resize(
self.img, (singleWidth * self.cols, singleHeight * self.rows))

for i in range(self.rows):
for j in range(self.cols):
percent = int((i*self.rows+j + 1) / total * 100)
self.update.emit(percent)

x = j * singleWidth
y = i * singleHeight

new_img = self.img[y:y+singleHeight, x: x+singleWidth]

self.save(new_img, i*self.rows+j)
print("正在处理%d/%d张……" % (i*self.rows+j, total))

def save(self, img, index) -> None:
cv2.imencode(self.inputPath.suffix, img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])[
1].tofile("%s/%s_%03d%s" %
(self.output, self.inputPath.stem, index, '.jpg'))

0 comments on commit c27c95e

Please sign in to comment.