保存到零件同名的文件夹下,名称相同,类型是 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] 生成略缩图并保存的更多相关文章

  1. java自动生成略缩图

    当你要做一个图库的项目时,对图片大小.像素的控制是首先需要解决的难题. 本篇文章,在前辈的经验基础上,分别对单图生成略缩图和批量生成略缩图做个小结. 一.单图生成略缩图 单图经过重新绘制,生成新的图片 ...

  2. php 制作略缩图

    一.需求 最近公司的项目中有个需求,就是用户上传自己的微信二维码,然后系统会自动将用户的微信二维码合并到产品中 二.分析 因为该系统是手机端的,所以从用户端的体验出发,用户当然是直接在微信上保存二维码 ...

  3. Bootstrap-CL:略缩图

    ylbtech-Bootstrap-CL:略缩图 1.返回顶部 1. Bootstrap 缩略图 本章将讲解 Bootstrap 缩略图.大多数站点都需要在网格中布局图像.视频.文本等.Bootstr ...

  4. appium-desktop录制脚本二次开发,生成我司自动化脚本

    目的 通过对appium-desktop脚本录制功能进行二次开发,使录制的java脚本符合我司自动化框架要求. 实现步骤 1.增加元素名称的输入框 由于ATK(我司自动化测试框架)脚本中元素是以“ap ...

  5. 【转】Jenkins 二次开发 - Python

    马克,备用: Jenkins 二次开发 https://testerhome.com/topics/14988?locale=zh-TW python-jenkins api 文档:https://p ...

  6. python生成组织架构图(网络拓扑图、graph.editor拓扑图编辑器)

    Graph.Editor是一款基于HTML5技术的拓补图编辑器,采用jquery插件的形式,是Qunee图形组件的扩展项目,旨在提供可供扩展的拓扑图编辑工具, 拓扑图展示.编辑.导出.保存等功能,此外 ...

  7. Python生成GIF动态图

    python生成摸头GIF 本篇教程演示了如何使用python的PIL库生成GIF图片 源码已经贴在文中,自行取用 效果演示 运行代码,会让你选择要制作的图片 运行完成后,会在同路径下生成dem.gi ...

  8. UG二次开发-CAM-获取修改路径参数

    项目中要获取路径参数,网上大多是C++的例子,而本项目是用C#写的,探索了下,记录下. 以获取某条路径的刀具号为例,其他参数依此类推. using System; using System.Colle ...

  9. sitemap.xml 静态和动态生成页面 shopnc二次开发 动态生成sitemap.xml

    Sitemap 可方便网站管理员通知搜索引擎他们网站上有哪些可供抓取的网页.最简单的 Sitemap 形式,就是XML 文件,在其中列出网站中的网址以及关于每个网址的其他元数据(上次更新的时间.更改的 ...

  10. 【UG二次开发】获取系统信息UF_ask_system_info

    获取系统信息可以使用这个函数UF_ask_system_info 下面是例子: 1 extern DllExport void ufsta(char *param, int *returnCode, ...

随机推荐

  1. Surge多配置文件聚合配置方法

    目录 摘要 1. Surge配置原理 2. Surge托管配置 3. Surge多配置文件聚合配置 (1)找到配置文件 (2)编辑配置文件 参考 摘要 Surge 是一个在 macOS 和iOS 平台 ...

  2. Linux下vim的常用命令总结

    vim按d表示剪切 按dd剪切一行 vim命令:命令模式 /关键字 n继续向下查找 vim的多行注释: 1.按ctrl + v进入 visual block模式 2.按上下选中要注释的行 3.按大写字 ...

  3. 06. C语言指针

    [指针] C语言使用数据名调用数据,数据名相当于C语言的直接寻址,直接寻址只能调用固定数据,而指针是间接寻址,指针存储了另一个数据的地址,使用指针调用数据时首先取指针存储的内存地址,之后使用此地址调用 ...

  4. 海康iSC综合安防平台-视频web插件调试

    综合安防管理平台 视频WEB插件 1.demo_window_simple_playback.html.demo_window_simple_preview.html为简化版demo,可在此基础上开发 ...

  5. java后台@RequestBody和@RequestParam

    RequestBody 接收的是请求体里面(body)的数据 RequestParam接收的是key-value里面的参数,所以它会被切割进行处理从而可以是普通元素.数组.集合.对象等接收 get-- ...

  6. grads 同时读取多个ctl文件方法

    1.不同的文件进行不同的设置:'set dfile 2' 2.读取不同文件的变量:qv.2 实例如下:'reinit''open e:\tskt.CTL''open e:\uwnd.CTL''open ...

  7. Splashtop Enterprise提供全面的远程访问和远程支持解决方案

    ​ 全球领先的远程访问和远程支持解决方案领导者 Splashtop Inc. 发布了全新的 Splashtop Enterprise ,这是一个全面的远程访问和远程支持解决方案,满足企业的IT人员,服 ...

  8. C 语言编程 — 堆栈与内存管理

    目录 文章目录 目录 前文列表 栈(Stack)和堆(Heap) 栈 堆 内存管理 动态分配内存 重新调整内存的大小和释放内存 前文列表 <程序编译流程与 GCC 编译器> <C 语 ...

  9. 预见预判_AIRIOT智慧交通管理解决方案

    随着机动车保有量的逐步增加,城市交通压力日益增大.同时,新能源车辆的快速发展虽然带来了环保效益,但也因不限号政策而进一步加剧了道路拥堵问题.此外,各类赛事和重大活动的交通管制措施也时常导致交通状况复杂 ...

  10. WPF开发快速入门【0】前言与目录

    前言 WPF是一个生不逢时的技术,刚推出的时候由于是XP时代,WPF技术有两个不方便的地方: 1.由于操作系统没有自带Framework,需要另外安装,比较麻烦: 2.程序第一次启动时,由于要加载Fr ...