[UG 二次开发 python] 生成略缩图并保存
保存到零件同名的文件夹下,名称相同,类型是 jpg
用到 numpy,PIL,cv2
blockstyler 文件略
# nx: threaded
__version__ = "0.0.1"
__author__ = "unmht"
__blog__ = "https://www.cnblogs.com/unm001/"
from dlx.截图制作 import 截图制作
import os
import NXOpen
import NXOpen.BlockStyler
import NXOpen.UF
import cv2
import numpy as np
from PIL import Image
from support import info
os.chdir(os.path.dirname(os.path.abspath(__file__)))
class myclass(截图制作):
dwa: NXOpen.BlockStyler.DrawingArea
bt0: NXOpen.BlockStyler.Button
bt1: NXOpen.BlockStyler.Button
def __init__(self):
super().__init__()
self.work = self.theSession.Parts.Work
self.partpath = self.work.FullPath
self.dirpath = os.path.dirname(self.partpath)
self.imgpath = os.path.join(self.dirpath, f"{self.work.Name}.jpg")
self.readimgok = False
self.tmpimg = os.path.join(self.dirpath, "_tmp.jpg")
self.imgdata = None
if os.path.exists(self.tmpimg):
os.remove(self.tmpimg)
def dialogShown_cb(self):
try:
if os.path.exists(self.imgpath):
self.dwa.Image = self.imgpath
self.readimgok = True
except Exception as ex:
self.theUI.NXMessageBox.Show(
"Block Styler", NXOpen.NXMessageBox.DialogType.Error, str(ex)
)
def update_cb(self, block):
try:
if block == self.dwa:
# ---- Enter your code here -----
pass
elif block == self.bt0:
if self.imgdata is not None:
cv2.imencode(".jpg", self.imgdata)[1].tofile(self.imgpath)
elif block == self.bt1:
r = False
try:
r = self.capture()
except Exception as e:
info(e)
if r:
self.dwa.Image = self.tmpimg
except Exception as ex:
# ---- Enter your exception handling code here -----
self.theUI.NXMessageBox.Show(
"Block Styler", NXOpen.NXMessageBox.DialogType.Error, str(ex)
)
return 0
def capture(self):
self.imgdata = None
imgbuilder = self.theUI.CreateImageExportBuilder()
imgbuilder.RegionMode = False
imgbuilder.FileFormat = NXOpen.Gateway.ImageExportBuilder.FileFormats.Jpg
imgbuilder.FileName = self.tmpimg
imgbuilder.BackgroundOption = (
NXOpen.Gateway.ImageExportBuilder.BackgroundOptions.CustomColor
)
imgbuilder.SetCustomBackgroundColor([1.0, 1.0, 1.0])
imgbuilder.EnhanceEdges = True
imgbuilder.Commit()
imgbuilder.Destroy()
if os.path.exists(self.tmpimg):
img = Image.open(self.tmpimg)
img.load()
imgtmp = np.asanyarray(img)
img.close()
os.remove(self.tmpimg)
imgtmp = cv2.cvtColor(imgtmp, cv2.COLOR_RGB2BGR)
imggray = cv2.cvtColor(imgtmp, cv2.COLOR_BGR2GRAY)
imggray = 255 - imggray
cts, hirs = cv2.findContours(
imggray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
)
ct = max(cts, key=cv2.contourArea)
rct = cv2.boundingRect(ct)
x, y, width, height = rct
imgCut = imgtmp[y : y + height, x : x + width]
h2 = int(max(width * 2 / 3, height) + 0.1) + 4
if h2 % 2 == 1:
h2 = h2 + 1
if h2 % 4 != 0:
h2 = h2 + 2
w2 = int(h2 * 1.5)
dw = int((w2 - width) / 2)
dh = int((h2 - height) / 2)
imgret = np.full((h2, w2, 3), 255).astype(np.uint8)
# info([imgCut.shape, imgret.shape])
imgret[dh : dh + height, dw : dw + width] = imgCut
# info([width, height, w2, h2, dw, dh])
imgret2 = cv2.resize(imgret, (320, 240))
cv2.imencode(".jpg", imgret2)[1].tofile(self.tmpimg)
self.imgdata = imgret2
return True
def main():
thecls: myclass
try:
thecls = myclass()
# The following method shows the dialog immediately
thecls.Show()
except Exception as ex:
# ---- Enter your exception handling code here -----
NXOpen.UI.GetUI().NXMessageBox.Show(
"Block Styler", NXOpen.NXMessageBox.DialogType.Error, str(ex)
)
finally:
try:
if thecls != None:
if os.path.exists(thecls.tmpimg):
os.remove(thecls.tmpimg)
if thecls != None:
thecls.Dispose()
thecls = None
except Exception as e:
pass
if __name__ == "__main__":
main()


[UG 二次开发 python] 生成略缩图并保存的更多相关文章
- java自动生成略缩图
当你要做一个图库的项目时,对图片大小.像素的控制是首先需要解决的难题. 本篇文章,在前辈的经验基础上,分别对单图生成略缩图和批量生成略缩图做个小结. 一.单图生成略缩图 单图经过重新绘制,生成新的图片 ...
- php 制作略缩图
一.需求 最近公司的项目中有个需求,就是用户上传自己的微信二维码,然后系统会自动将用户的微信二维码合并到产品中 二.分析 因为该系统是手机端的,所以从用户端的体验出发,用户当然是直接在微信上保存二维码 ...
- Bootstrap-CL:略缩图
ylbtech-Bootstrap-CL:略缩图 1.返回顶部 1. Bootstrap 缩略图 本章将讲解 Bootstrap 缩略图.大多数站点都需要在网格中布局图像.视频.文本等.Bootstr ...
- appium-desktop录制脚本二次开发,生成我司自动化脚本
目的 通过对appium-desktop脚本录制功能进行二次开发,使录制的java脚本符合我司自动化框架要求. 实现步骤 1.增加元素名称的输入框 由于ATK(我司自动化测试框架)脚本中元素是以“ap ...
- 【转】Jenkins 二次开发 - Python
马克,备用: Jenkins 二次开发 https://testerhome.com/topics/14988?locale=zh-TW python-jenkins api 文档:https://p ...
- python生成组织架构图(网络拓扑图、graph.editor拓扑图编辑器)
Graph.Editor是一款基于HTML5技术的拓补图编辑器,采用jquery插件的形式,是Qunee图形组件的扩展项目,旨在提供可供扩展的拓扑图编辑工具, 拓扑图展示.编辑.导出.保存等功能,此外 ...
- Python生成GIF动态图
python生成摸头GIF 本篇教程演示了如何使用python的PIL库生成GIF图片 源码已经贴在文中,自行取用 效果演示 运行代码,会让你选择要制作的图片 运行完成后,会在同路径下生成dem.gi ...
- UG二次开发-CAM-获取修改路径参数
项目中要获取路径参数,网上大多是C++的例子,而本项目是用C#写的,探索了下,记录下. 以获取某条路径的刀具号为例,其他参数依此类推. using System; using System.Colle ...
- sitemap.xml 静态和动态生成页面 shopnc二次开发 动态生成sitemap.xml
Sitemap 可方便网站管理员通知搜索引擎他们网站上有哪些可供抓取的网页.最简单的 Sitemap 形式,就是XML 文件,在其中列出网站中的网址以及关于每个网址的其他元数据(上次更新的时间.更改的 ...
- 【UG二次开发】获取系统信息UF_ask_system_info
获取系统信息可以使用这个函数UF_ask_system_info 下面是例子: 1 extern DllExport void ufsta(char *param, int *returnCode, ...
随机推荐
- protobuf 文档
文档地址: https://golang-tech-stack.com/tutorial/pb 学习视频: https://www.bilibili.com/video/BV1Y3411j7EM?p= ...
- spring-boot集成Quartz-job存储方式二RAM,改从json配置文件读取job配置
前面第二种RAM方法已经可以满足单机使用需求了,但是本地调试和服务器应用会有冲突,因此将定时任务保存到本地json配置文件中,这样更灵活. 1.ApplicationInit类 package org ...
- Linux(三):Linux的目录及相关作用
使用 Linux,不仅限于学习各种命令,了解整个 Linux 文件系统的目录结构以及各个目录的功能同样至关重要.使用 Linux 时,通过命令行输入 ls -l / 可以看到,在 Linux 根目录( ...
- 原生微信小程序
new Date 跨平台兼容性问题 在 Andriod 使用 new Date("2018-05-30 00:00:00")木有问题,但是在ios 下面识别不出来.因为 IOS 下 ...
- Win10-常用cmd命令与快捷键
以下全部是本人私认为平时经常用到的指令,持续更行中- 常用快捷键 win + R : 输入cmd回车,打开命令提示符界面 win + E : 打开文件资源管理器(俗称:文件夹) win + S : 搜 ...
- C 语言编程 — 编程实践
目录 文章目录 目录 前文列表 程序示例 前文列表 <程序编译流程与 GCC 编译器> <C 语言编程 - 基本语法> <C 语言编程 - 基本数据类型> < ...
- 用pageOffice文档控件实现 office文档在线编辑
第三方文档控件,pageOffice 系统开发中经常要处理办公文档,如果word,excel,ppt,编辑整理,保存,归档. 开发市场上也有很多第三文文档控件,多年的总结,还是认为pageOffice ...
- T2T-ViT:更多的局部结构信息,更高效的主干网络 | ICCV 2021
论文提出了T2T-ViT模型,引入tokens-to-token(T2T)模块有效地融合图像的结构信息,同时借鉴CNN结果设计了deep-narrow的ViT主干网络,增强特征的丰富性.在ImageN ...
- PHP 有哪些常用的魔术变量 ?
引言 PHP 向它运行的任何脚本提供了大量的预定义常量. 魔术变量 __LINE__ 文件中的当前行号. __FILE__ 文件的完整路径和文件名. 如果用在被包含文件中,则返回被包含的文件名. __ ...
- cmd /c和cmd /k 以及CMD命令
[转]cmd /c和cmd /kjava的Runtime.getRuntime().exec(commandStr)可以调用执行cmd指令. cmd /c dir 是执行完dir命令后关闭命令窗口. ...