[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, ...
随机推荐
- 开源电子邮件营销平台 listmonk 使用教程
做产品肯定要做电子邮件营销,特别是面向海外的产品,电子邮件营销已成为企业与客户沟通.建立品牌忠诚度和推动销售的重要工具,可以直接接触到目标受众,提供个性化内容,并以相对较低的成本获得可观的投资回报.你 ...
- Steam中将XBox手柄默认布局改为任天堂手柄布局的方法
1. 在Steam菜单栏找到"查看",选择大屏幕模式. 2. 进入大屏幕模式后,在菜单界面找到"设置". 3. 在设置界面找到"控制器",选 ...
- MacOS M1芯片openmp库出现mach-o file, but is an incompatible architecture (have ‘arm64‘, need ‘x86_64‘问题解决
目录 1. 问题描述 2. 问题出现原因 3. 解决方案 编译安装 使用Homebrew安装 Reference 1. 问题描述 报错如下所示: ImportError: dlopen(/Users/ ...
- rubymineIDE配置启动
debug启动 debug启动 安装插件 报错: 网络问题 下载 ruby-debug-ide-2.3.1 解压到 /home/haima/.rvm/gems/ruby-2.3.8/gems/
- 🔥httpsok彻底告别SSL证书续期烦扰
httpsok彻底告别SSL证书续期烦扰 介绍 httpsok 是一个便捷的 HTTPS 证书自动续签工具,专为 Nginx .OpenResty 服务器设计.已服务众多中小企业,稳定.安全.可靠. ...
- C语言:约瑟夫问题——使用循环链表解决
传说有30个乘客同乘一条船,因为严重超载,加上风浪水作,危险万分.船长告诉乘客,只有将全船一半的乘客投入海 中,其余人才能幸免于难.他们约定了一个规则:30个人围成一圈,由第一个人数起,依次报数,数到 ...
- T2T-ViT:更多的局部结构信息,更高效的主干网络 | ICCV 2021
论文提出了T2T-ViT模型,引入tokens-to-token(T2T)模块有效地融合图像的结构信息,同时借鉴CNN结果设计了deep-narrow的ViT主干网络,增强特征的丰富性.在ImageN ...
- 数据分析---numpy模块
前戏 NumPy(Numerical Python) 是 Python 语言中做科学计算的基础库.重在于数值计算,也是大部分Python科学计算库的基础,多用于在大型.多维数组上执行的数值运算. 快捷 ...
- Cmockery学习
什么是cmockery? 是一个轻量级的C语言单元测试框架 什么是单元测试? 单元测试就是测试一个系统的最小实现单元,往往是函数 示例解析 #include <stdarg.h> #inc ...
- 面试必问:MySQL死锁 是什么,如何解决?(史上最全)
MySQL死锁接触少,但面试又经常被问到怎么办? 最近有小伙伴在面试的时候,被问了MySQL死锁,如何解决? 虽然也回答出来了,但是不够全面体系化, 所以,小北给大家做一下系统化.体系化的梳理,帮助大 ...