[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, ...
随机推荐
- H5页面制作和CSS3动画的结合
现在很火的是h5页面的开发,更多的视觉冲击,带来更多的关注度,更多的眼球,想要别人看你的东西,不在是以前的一段文字,或是一个图片的时代了,现在h5把一张张图片,一段段文字动起来,更有与客户的交互,而且 ...
- UE4 InstancedStaticMesh使用
在绘制大批量近似模型时,Unity有GPU Instancing(https://www.cnblogs.com/hont/p/7143626.html),而UE中有 HISM和ISM(Instanc ...
- C# winfrom 局域网版多人成语接龙(二)
功能基本上是完成了,要两个人完才好玩,目前 倒计时,每组游戏玩家数量这些控制变量,都是写死再代码里的,等以后想改的时候再改,这个项目核心的功能算是实现了,但还可以扩展,比如记录一下用户的游戏数据,答对 ...
- cmder右键打开方式
第一步: 新打开一个cmder窗口 第二步: 输入: Cmder.exe /register user 或 Cmder.exe /register all 第三步: 回车执行命令
- 行列式求值,从 $n!$ 优化到 $n^3$
前置知识 \(\sum\) 为累加符号,\(\prod\) 为累乘符号. 上三角矩阵指只有对角线及其右上方有数值其余都是 \(0\) 的矩阵. 如果一个矩阵的对角线全部为 \(1\) 那么这个矩阵为单 ...
- (性能测试)--记录一次高可用场景导致CPU资源升高
测试场景:高可用场景--限流测试: 被测交易:查询类交易,HTTP协议: 交易链路:jmeter - web - coimpre(前置服务) -- coimbp -- cobp (coimbp .co ...
- java中以字符分隔的字符串与字符串数组的相互转换
1.字符串数组拼接成一个以指定字符(包括空字符)分隔的字符串--String.join(),JDK8的新特性 String[] strArray = {"aaa","bb ...
- 自用电脑+外网开放+SSL认证(纯免费)
背景: 本文的目的主要是为了方便大家测试,不过有条件的情况下没必要学习了.主要是给那些没有服务器,公司也不给ssl认证的开发测试人员的一种方案:就像题目所说的那样. 纯免费,纯免费的话是有学习成本的, ...
- react祖先与子孙多层传值
先做数据源store.js文件 // 状态 store 统一数据源 import React, { createContext } from 'react' // Provider 发布消息 // C ...
- vue计算属性computed
模板中放入太多的逻辑会让模板过重且难以维护,使用计算属性可以让模板变得简洁易于维护.计算属性是基于它们的响应式依赖进行缓存的,计算属性比较适合对多个变量或者对象进行处理后返回一个结果值,也就是数多个变 ...