Commit a92c991d by linjinhong

测试添加shap

parent 79f3b0d1
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -37,8 +37,9 @@
"multiparty": "^4.2.3",
"nodemon": "^3.1.4",
"normalize.css": "^8.0.1",
"sharp": "^0.34.1",
"vue": "^2.6.11",
"vue-draggable-resizable": "^3.0.0",
"vue-draggable-resizable": "^2.0.0",
"vue-i18n": "^8.16.0",
"vue-moveable": "^2.0.0-beta.75",
"vue-router": "^3.1.5",
......
// 裁切最大像素点
var inputFolder = "D:\\python\\code\\PsProduct\\PsProduct\\input";
var outputFolder = "D:\\python\\code\\PsProduct\\PsProduct\\output";
// 从参数文件读取配置
var paramFile = new File(Folder.userData + "/ps_params.json");
// 自定义JSON解析函数(替代标准JSON.parse)
function parseJSON(jsonString) {
var obj = {};
try {
eval("obj = " + jsonString);
} catch(e) {
$.writeln("JSON解析错误: " + e.message);
}
return obj;
}
var params; // 在外部定义 params 变量
// 通过参数文件读取配置
if (paramFile.exists) {
paramFile.open("r");
var jsonString = paramFile.read();
paramFile.close();
paramFile.remove();
// 使用自定义解析代替JSON.parse
var params = parseJSON(jsonString);
inputFolder = params.input || inputFolder;
outputFolder = params.output || outputFolder;
}
// 主处理逻辑
processFolder(new Folder(inputFolder), new Folder(outputFolder));
function processFolder(inputDir, outputDir) {
if (!outputDir.exists) outputDir.create();
var files = inputDir.getFiles(/\.(psd|jpg|png|tiff?)$/i);
for (var i = 0; i < files.length; i++) {
processFile(files[i], outputDir);
}
}
function processFile(file, outputDir) {
try {
var doc = app.open(file);
// 自动解锁背景层
if (doc.artLayers.length > 0 && doc.artLayers[0].isBackgroundLayer) {
doc.artLayers[0].isBackgroundLayer = false;
}
// 透明裁剪逻辑
var originalBounds = [0, 0, doc.width.as("px"), doc.height.as("px")];
var newBounds = doc.trim(TrimType.TRANSPARENT, true, true, true, true);
if (newBounds && !arraysEqual(originalBounds, newBounds)) {
doc.crop([newBounds[0], newBounds[1], newBounds[2]+1, newBounds[3]+1]);
}
// 保存文件
saveAs(doc, new File(outputDir + "/" + file.name));
doc.close(SaveOptions.DONOTSAVECHANGES);
} catch(e) { /* 静默处理错误 */ }
}
function saveAs(doc, targetFile) {
var pngOpt = new PNGSaveOptions();
pngOpt.interlaced = false;
pngOpt.compression = 3;
doc.saveAs(targetFile, pngOpt, true);
}
function arraysEqual(a, b) {
for (var i = 0; i < a.length; i++) {
if (Math.round(a[i]) != Math.round(b[i])) return false;
}
return true;
}
\ No newline at end of file
export function pxToUnit(px, unit) {
const setting = this.$dataStore.get("setting");
if (!unit) {
......@@ -6,6 +5,8 @@ export function pxToUnit(px, unit) {
unit = setting.unit;
}
}
// console.log("mm", px);
// console.log("inch", Number((Number(px) / 0.84183).toFixed(1)));
if (unit === "mm") {
px = Number((px * 0.84183).toFixed(1));
} else if (unit === "inch") {
......@@ -14,12 +15,12 @@ export function pxToUnit(px, unit) {
return px;
}
export function mmToPx(mm) {
return Number((Number(mm) / 0.84183).toFixed(1))
return Number((Number(mm) / 0.84183).toFixed(1));
}
export function unitToPx(px,unit) {
export function unitToPx(px, unit) {
const setting = this.$dataStore.get("setting");
if(!unit){
if (!unit) {
unit = setting.unit;
}
if (unit === "mm") {
......
const fs = require("fs");
const path = require("path");
// const sharp = require("sharp");
const sharp = 1;
/**
* 裁切单个图片的透明白边
* @param {string} inputPath - 输入图片路径
* @param {string} outputPath - 输出图片路径
* @param {Object} options - 配置选项
* @param {number} options.threshold - 透明度阈值,0-255之间,默认为10
* @returns {Promise<Object>} - 返回处理结果
*/
async function cropImageTransparentEdges(inputPath, outputPath, options = {}) {
const threshold = options.threshold || 10;
// 确保输出目录存在
const outputDir = path.dirname(outputPath);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
try {
// 读取图片信息
const metadata = await sharp(inputPath).metadata();
// 提取图片的透明通道信息
const { data, info } = await sharp(inputPath)
.raw()
.ensureAlpha()
.toBuffer({ resolveWithObject: true });
const { width, height, channels } = info;
// 找到图片的边界
let topmost = height;
let bottommost = 0;
let leftmost = width;
let rightmost = 0;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const alphaIdx = (y * width + x) * channels + channels - 1;
const alpha = data[alphaIdx];
// 如果像素不是透明的(根据阈值)
if (alpha > threshold) {
topmost = Math.min(topmost, y);
bottommost = Math.max(bottommost, y);
leftmost = Math.min(leftmost, x);
rightmost = Math.max(rightmost, x);
}
}
}
// 如果图片全透明,保持原样
if (topmost > bottommost || leftmost > rightmost) {
await sharp(inputPath).toFile(outputPath);
return {
status: "unchanged",
reason: "全透明图片",
inputPath,
outputPath,
};
}
// 计算裁切区域
const cropWidth = rightmost - leftmost + 1;
const cropHeight = bottommost - topmost + 1;
// 裁切并保存图片
await sharp(inputPath)
.extract({
left: leftmost,
top: topmost,
width: cropWidth,
height: cropHeight,
})
.toFile(outputPath);
return {
status: "cropped",
inputPath,
outputPath,
originalSize: { width: metadata.width, height: metadata.height },
newSize: { width: cropWidth, height: cropHeight },
};
} catch (error) {
return {
status: "error",
inputPath,
outputPath,
error: error.message,
};
}
}
/**
* 裁切图片目录中的所有图片的透明白边
* @param {string} inputDir - 输入图片目录
* @param {string} outputDir - 输出图片目录
* @param {Object} options - 配置选项
* @param {number} options.threshold - 透明度阈值,0-255之间,默认为10
* @returns {Promise<Array>} - 返回处理结果数组
*/
async function cropTransparentEdges(inputDir, outputDir, options = {}) {
// 确保输出目录存在
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
// 获取输入目录中的所有图片文件
const files = fs.readdirSync(inputDir).filter((file) => {
const ext = path.extname(file).toLowerCase();
return [".png", ".jpg", ".jpeg", ".webp"].includes(ext);
});
const results = [];
// 处理每个图片
for (const file of files) {
const inputPath = path.join(inputDir, file);
const outputPath = path.join(outputDir, file);
const result = await cropImageTransparentEdges(
inputPath,
outputPath,
options
);
results.push({ file, ...result });
}
return results;
}
/**
* 处理图片并输出结果
* @param {string} inputPath - 输入图片路径或目录
* @param {string} outputPath - 输出图片路径或目录
* @param {Object} options - 配置选项
* @param {number} options.threshold - 透明度阈值,0-255之间,默认为10
*/
async function processImages(inputPath, outputPath, options = {}) {
console.log(`开始处理图片,从 ${inputPath}${outputPath}`);
try {
let results = [];
// 检查输入是文件还是目录
const inputStat = fs.statSync(inputPath);
if (inputStat.isDirectory()) {
// 如果输入是目录,检查输出是否有扩展名
const outputExt = path.extname(outputPath);
if (outputExt) {
throw new Error("如果输入是目录,输出也必须是目录");
}
// 处理目录下的所有图片
results = await cropTransparentEdges(inputPath, outputPath, options);
} else {
// 处理单个图片
const result = await cropImageTransparentEdges(
inputPath,
outputPath,
options
);
results = [{ file: path.basename(inputPath), ...result }];
}
console.log("处理完成!");
console.log(
`成功处理: ${results.filter((r) => r.status === "cropped").length} 张图片`
);
console.log(
`保持不变: ${
results.filter((r) => r.status === "unchanged").length
} 张图片`
);
console.log(
`处理失败: ${results.filter((r) => r.status === "error").length} 张图片`
);
return results;
} catch (error) {
console.error("处理图片过程中出错:", error);
throw error;
}
}
module.exports = {
cropTransparentEdges,
cropImageTransparentEdges,
processImages,
};
processImages(
"C:\\Users\\lm121\\Desktop\\text\\1744786196514f6f7a856378.png",
"C:\\Users\\lm121\\Desktop\\text\\1744786196514f6f7a856378_1.png"
);
......@@ -257,6 +257,8 @@ export default {
}, 500);
}
} else {
console.log(260, res.data);
bus.$emit("busEmit", {
type: "sendFile",
value: res.data,
......
......@@ -6,7 +6,7 @@ import html2canvas from "html2canvas";
import moment from "moment";
import pkg from "../../../../package.json";
import { pxToUnit } from "../../../utils/index";
import { cropImageTransparentEdges } from "@/utils/setImage.js";
export default {
data() {
return {
......@@ -23,17 +23,17 @@ export default {
settingName: "",
printInkList: [
{
label: "仅彩色油墨"
label: "仅彩色油墨",
},
{
label: "仅白色油墨"
label: "仅白色油墨",
},
{
label: "彩色 + 白色油墨"
label: "彩色 + 白色油墨",
},
{
label: "仅黑色油墨"
}
label: "仅黑色油墨",
},
],
detailShow: false,
showPopover: false,
......@@ -63,23 +63,23 @@ export default {
iBlackBalance: 0,
iYellowBalance: 0,
byMinWhite: 1,
bMultiple: false
}
bMultiple: false,
},
};
},
props: {
visible: {
default: false,
type: Boolean
type: Boolean,
},
byPlatenSize: {
default: 0,
type: Number
type: Number,
},
imgList: {
default: () => [],
type: Array
}
type: Array,
},
},
mounted() {
this.$dataStore.set("default-print-setting", this.printSetting);
......@@ -88,7 +88,9 @@ export default {
console.log(this.$dataStore, "select");
if (select) {
this.printSettingSelectLabel = select;
let index = this.printSettingList.findIndex(el => el.label === select);
let index = this.printSettingList.findIndex(
(el) => el.label === select
);
if (index >= 0) {
this.printSettingVal = index;
this.printSetting = JSON.parse(
......@@ -120,7 +122,7 @@ export default {
} else {
return "1.5";
}
}
},
},
watch: {
printSetting: {
......@@ -128,11 +130,11 @@ export default {
this.$dataStore.set("print-setting", this.printSetting);
},
deep: true,
immediate: true
immediate: true,
},
visible() {
this.dialogShow = this.visible;
}
},
},
created() {
this.getPrinter();
......@@ -143,7 +145,7 @@ export default {
this.$confirm(`确定删除该预设?`, "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
type: "warning",
}).then(() => {
this.$dataStore.delete(
this.printSettingList[this.printSettingVal].label + "-print-setting"
......@@ -158,7 +160,7 @@ export default {
if (k.includes("-print-setting")) {
arr.push({
label: k.replace("-print-setting", ""),
value: this.$dataStore.get(k)
value: this.$dataStore.get(k),
});
}
}
......@@ -176,7 +178,7 @@ export default {
if (this.settingName.trim() === "") {
return this.$message.warning("预设名称不能为空");
}
if (this.printSettingList.find(el => el.label === this.settingName)) {
if (this.printSettingList.find((el) => el.label === this.settingName)) {
return this.$message.warning("预设名称不能重复");
}
this.$dataStore.set(
......@@ -237,7 +239,7 @@ export default {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], {
type: mime
type: mime,
});
},
......@@ -260,7 +262,7 @@ export default {
canvas: canvas1,
backgroundColor: null,
useCORS: true,
allowTaint: true
allowTaint: true,
}).then(async function(canvas) {
let dataURL = canvas.toDataURL("image/png");
let pageBlob = that.dataURLtoBlob(dataURL);
......@@ -270,8 +272,8 @@ export default {
let { data } = await that.$api.post("/uploadImage", params, {
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
"Content-Type": "application/x-www-form-urlencoded",
},
});
let w_mm = Number((canvas1.width * 0.84183).toFixed(1));
let h_mm = Number((canvas1.height * 0.84183).toFixed(1));
......@@ -285,6 +287,10 @@ export default {
},
return_data() {
let dom_id = `${this.imgList[0].fileName}_0`;
const data = cropImageTransparentEdges(this.imgList[0].url);
console.log(290, data);
return;
const image = document.getElementById(dom_id);
const line = document.getElementById("line");
......@@ -320,7 +326,7 @@ export default {
y: y_mm,
w: w_mm,
h: h_mm,
r
r,
};
// 返回计算结果
// return {
......@@ -367,10 +373,12 @@ export default {
y = this.numberToStr4(y);
w = this.numberToStr4(w);
h = this.numberToStr4(h);
console.log(x, "x");
console.log(y, "y");
console.log(w, "w");
console.log(h, "h");
console.log("print", x, y, w, h);
// console.log(x, "x");
// console.log(y, "y");
// console.log(w, "w");
// console.log(h, "h");
r = Number(r).toFixed(0);
const imgFileName = this.imgList[0].fileName;
let position = ""; // 位置
......@@ -405,8 +413,8 @@ export default {
byPlatenSize: this.byPlatenSize,
cmd,
fileName: imgFileName,
print_cmd
}
print_cmd,
},
};
await this.toWritePrintLog(data);
......@@ -431,7 +439,7 @@ export default {
position_unit,
position_after_px,
position_before_px,
send_api_data
send_api_data,
};
await this.$api.post("/writePrintLog", data);
},
......@@ -448,8 +456,8 @@ export default {
setDetailShow() {
this.detailShow = !this.detailShow;
}
}
},
},
};
</script>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment