Skip to content

Commit c27c95e

Browse files
committed
feat: 添加TIFF处理类
1 parent 6ff942f commit c27c95e

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

main.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88

99
from PyQt6 import QtWidgets, QtCore
1010
from PyQt6.QtGui import QGuiApplication
11+
from tools.tiff_processer import TIFF
1112
from ui.Ui_MainWindow import Ui_MainWindow
1213

1314
from tools.raw_processer import Raw
1415
from tools.png_processer import PNG
1516
from tools.jpg_processer import JPG
1617

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

2021

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

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

2930
self.lineEdit_input.setText(fileName)
3031

@@ -152,6 +153,8 @@ def handle(self):
152153
processer = PNG(inputPath, outputPath, row, col)
153154
if extension_name == '.jpg' or extension_name == '.jpge':
154155
processer = JPG(inputPath, outputPath, row, col)
156+
if extension_name == '.tif' or extension_name == '.tiff':
157+
processer = TIFF(inputPath, outputPath, row, col)
155158

156159
if not processer:
157160
self.complete()

test.raw

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

test.tif

172 KB
Binary file not shown.

tools/png_processer.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def slice(self) -> None:
3737
print("正在处理%d/%d张……" % (i*self.rows+j, total))
3838

3939
def save(self, img, index) -> None:
40-
print(img.shape)
4140
cv2.imencode(self.inputPath.suffix, img, [int(cv2.IMWRITE_PNG_COMPRESSION), 0])[
4241
1].tofile("%s/%s_%03d%s" %
4342
(self.output, self.inputPath.stem, index, self.inputPath.suffix))

tools/tiff_processer.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import cv2
2+
from tools.processer import Processer
3+
from pathlib import Path
4+
import math
5+
from PyQt6 import QtWidgets
6+
import numpy as np
7+
8+
9+
class TIFF(Processer):
10+
def load(self) -> None:
11+
self.img = cv2.imdecode(np.fromfile(
12+
self.input, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
13+
self.inputPath = Path(self.input)
14+
15+
def slice(self) -> None:
16+
self.load()
17+
total = self.rows * self.cols
18+
shape = self.img.shape
19+
singleWidth = math.ceil(shape[1] / self.cols)
20+
singleHeight = math.ceil(shape[0] / self.rows)
21+
print(shape)
22+
if singleWidth * self.cols != shape[0] or singleHeight * self.rows != shape[1]:
23+
self.img = cv2.resize(
24+
self.img, (singleWidth * self.cols, singleHeight * self.rows))
25+
26+
for i in range(self.rows):
27+
for j in range(self.cols):
28+
percent = int((i*self.rows+j + 1) / total * 100)
29+
self.update.emit(percent)
30+
31+
x = j * singleWidth
32+
y = i * singleHeight
33+
34+
new_img = self.img[y:y+singleHeight, x: x+singleWidth]
35+
36+
self.save(new_img, i*self.rows+j)
37+
print("正在处理%d/%d张……" % (i*self.rows+j, total))
38+
39+
def save(self, img, index) -> None:
40+
cv2.imencode(self.inputPath.suffix, img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])[
41+
1].tofile("%s/%s_%03d%s" %
42+
(self.output, self.inputPath.stem, index, '.jpg'))

0 commit comments

Comments
 (0)