Skip to content

Commit

Permalink
fix: 修复Raw无法获取宽高的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
CheaterScript committed Jun 8, 2022
1 parent c27c95e commit 01bd3f9
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 10 deletions.
71 changes: 66 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from unittest import result
import cv2
import os
import numpy as np
Expand All @@ -10,6 +11,7 @@
from PyQt6.QtGui import QGuiApplication
from tools.tiff_processer import TIFF
from ui.Ui_MainWindow import Ui_MainWindow
from ui.inputDialog import Ui_Dialog

from tools.raw_processer import Raw
from tools.png_processer import PNG
Expand All @@ -34,6 +36,39 @@ def ouputDir(self):
self, "选择输出路径", os.getcwd())
self.lineEdit_output.setText(dirPath)

def showInputDialog(self):
if self.inputDialog.exec() == 1:
return True
else:
return False

def checkRawInput(self):
# width = int(self.inputDialogUI.lineEdit_width.text())
# height = int(self.inputDialogUI.lineEdit_height.text())
# channels = int(self.inputDialogUI.lineEdit_channels.text())
try:
width = int(self.inputDialogUI.lineEdit_width.text())
except ValueError as e:
QtWidgets.QMessageBox.warning(
self, "提示", "Raw宽度不是一个整数!")
return False

try:
width = int(self.inputDialogUI.lineEdit_width.text())
except ValueError as e:
QtWidgets.QMessageBox.warning(
self, "提示", "Raw宽度不是一个整数!")
return False

try:
width = int(self.inputDialogUI.lineEdit_width.text())
except ValueError as e:
QtWidgets.QMessageBox.warning(
self, "提示", "Raw通道不是一个整数!")
return False

return True

def center(self):
screen = QGuiApplication.screenAt(QtCore.QPoint(
self.frameGeometry().x(), self.frameGeometry().y()))
Expand All @@ -47,6 +82,11 @@ def initUI(self, MainWindow):
self.pushButton_start = self.pushButton_3
self.center()

self.inputDialog = QtWidgets.QDialog(self)
self.inputDialogUI = Ui_Dialog()
self.inputDialogUI.setupUi(self.inputDialog)


# 打开文件
self.pushButton_input.clicked.connect(self.openFile)
# self.pushButton_input.clicked.connect(self.test)
Expand All @@ -68,7 +108,21 @@ def setIsEnabled(self, isEnabled):
def start(self):
if(self.checkInput() == False):
return

inputPath = self.lineEdit_input.text()
_, extension_name = os.path.splitext(inputPath)
extension_name = extension_name.lower()

if extension_name == '.raw':
result = self.showInputDialog()
if not result:
return
if self.checkRawInput() == False:
return

self.handle()

def handle(self):
# 禁用所有控件
self.setIsEnabled(False)
# 隐藏开始按钮
Expand Down Expand Up @@ -148,7 +202,10 @@ def handle(self):
if extension_name not in FILE_TYPES:
raise Exception("文件打开失败")
if extension_name == '.raw':
processer = Raw(inputPath, outputPath, row, col)
width = int(self.inputDialogUI.lineEdit_width.text())
height = int(self.inputDialogUI.lineEdit_height.text())
channels = int(self.inputDialogUI.lineEdit_channels.text())
processer = Raw(inputPath, outputPath, row, col,(height, width, channels))
if extension_name == '.png':
processer = PNG(inputPath, outputPath, row, col)
if extension_name == '.jpg' or extension_name == '.jpge':
Expand All @@ -164,12 +221,16 @@ def handle(self):

time.sleep(0.5)

processer.slice()
result = processer.slice()

time.sleep(0.5)
if isinstance(result, str):
QtWidgets.QMessageBox.information(
self, "提示", result)
else:
time.sleep(0.5)

QtWidgets.QMessageBox.information(
self, "处理完成提示", "恭喜,处理完成!")
QtWidgets.QMessageBox.information(
self, "处理完成提示", "恭喜,处理完成!")

self.complete()

Expand Down
2 changes: 1 addition & 1 deletion test.raw

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions tools/raw_processer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@


class Raw(Processer):
def __init__(self, input, output, rows, cols, shape) -> None:
super().__init__(input, output, rows, cols)
self.shape = shape

def load(self) -> None:
self.img = np.fromfile(self.input, dtype=np.uint8)
self.img = self.img.reshape((65536, 65536, 3))
self.inputPath = Path(self.input)

def slice(self) -> None:
self.load()
try:
self.img = self.img.reshape(self.shape)
except ValueError as e:
return "宽高或通道数与Raw文件大小不匹配!"
total = self.rows * self.cols
shape = self.img.shape
singleWidth = math.ceil(shape[0] / self.cols)
Expand All @@ -37,7 +44,8 @@ def slice(self) -> None:
self.save(new_img, i*self.rows+j)
print("正在处理%d/%d张……" % (i*self.rows+j, total))

return False
def save(self, img, index) -> None:
cv2.imencode(self.inputPath.suffix, img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])[
cv2.imencode('.jpg', img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])[
1].tofile("%s/%s_%03d%s" %
(self.output, self.inputPath.stem, index, '.jpg'))
2 changes: 1 addition & 1 deletion tools/tiff_processer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ def slice(self) -> None:
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])[
cv2.imencode('.jpg', img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])[
1].tofile("%s/%s_%03d%s" %
(self.output, self.inputPath.stem, index, '.jpg'))
2 changes: 1 addition & 1 deletion ui/Ui_MainWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def retranslateUi(self, MainWindow):
self.label.setText(_translate("MainWindow", "输出目录:"))
self.pushButton_output.setText(_translate("MainWindow", "浏览"))
self.pushButton_3.setText(_translate("MainWindow", "开始处理"))
self.groupBox_2.setTitle(_translate("MainWindow", "参数配置"))
self.groupBox_2.setTitle(_translate("MainWindow", "切割参数配置"))
self.label_3.setText(_translate("MainWindow", "行数:"))
self.label_5.setText(_translate("MainWindow", "列数:"))
self.label_6.setText(_translate("MainWindow", "×"))
Expand Down
79 changes: 79 additions & 0 deletions ui/inputDialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Form implementation generated from reading ui file 'inputDialog.ui'
#
# Created by: PyQt6 UI code generator 6.3.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.


from PyQt6 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(419, 208)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(50, 160, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Orientation.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.StandardButton.Cancel|QtWidgets.QDialogButtonBox.StandardButton.Ok)
self.buttonBox.setObjectName("buttonBox")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(20, 30, 55, 16))
font = QtGui.QFont()
font.setPointSize(12)
self.label.setFont(font)
self.label.setObjectName("label")
self.lineEdit_width = QtWidgets.QLineEdit(Dialog)
self.lineEdit_width.setGeometry(QtCore.QRect(20, 60, 113, 30))
font = QtGui.QFont()
font.setPointSize(12)
self.lineEdit_width.setFont(font)
self.lineEdit_width.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.lineEdit_width.setObjectName("lineEdit_width")
self.label_2 = QtWidgets.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(150, 30, 55, 16))
font = QtGui.QFont()
font.setPointSize(12)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.lineEdit_height = QtWidgets.QLineEdit(Dialog)
self.lineEdit_height.setGeometry(QtCore.QRect(150, 60, 113, 30))
font = QtGui.QFont()
font.setPointSize(12)
self.lineEdit_height.setFont(font)
self.lineEdit_height.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.lineEdit_height.setObjectName("lineEdit_height")
self.lineEdit_channels = QtWidgets.QLineEdit(Dialog)
self.lineEdit_channels.setGeometry(QtCore.QRect(280, 60, 113, 30))
font = QtGui.QFont()
font.setPointSize(12)
self.lineEdit_channels.setFont(font)
self.lineEdit_channels.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.lineEdit_channels.setObjectName("lineEdit_channels")
self.label_3 = QtWidgets.QLabel(Dialog)
self.label_3.setGeometry(QtCore.QRect(280, 30, 55, 16))
font = QtGui.QFont()
font.setPointSize(12)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(Dialog)
self.label_4.setGeometry(QtCore.QRect(20, 110, 311, 16))
self.label_4.setStyleSheet("*{\n"
"color:rgb(255, 0, 0)\n"
"}")
self.label_4.setObjectName("label_4")

self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept) # type: ignore
self.buttonBox.rejected.connect(Dialog.reject) # type: ignore
QtCore.QMetaObject.connectSlotsByName(Dialog)

def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "输入RAW图片信息"))
self.label.setText(_translate("Dialog", "宽度:"))
self.label_2.setText(_translate("Dialog", "高度:"))
self.lineEdit_channels.setText(_translate("Dialog", "3"))
self.label_3.setText(_translate("Dialog", "通道数:"))
self.label_4.setText(_translate("Dialog", "*Photoshop Raw格式不包含宽高信息,需要手动输入。"))
Loading

0 comments on commit 01bd3f9

Please sign in to comment.