如何使用c#编辑python脚本的某个部分 [英] How do I edit a certain part of a python script using c#

查看:137
本文介绍了如何使用c#编辑python脚本的某个部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在visual studio中有一个项目,可以在鼠标点击时检索2个不同的x,y坐标。然后坐标将显示在文本框中。我还有一个python脚本,可以使用鼠标单击时选取的x,y坐标裁剪面部。我现在面临的问题是,我不知道如何从文本框中获取坐标并将其直接放在python脚本中,而无需打开python脚本并手动更改它。



  !/ usr / bin / env python  
软件许可协议(BSD许可证)

版权所有(c)2012,Philipp Wagner
保留所有权利。

在源代码和二进制形式中重新分发和使用,有或没有
如果满足以下条件,则允许修改
符合:

*源代码的重新分发必须保留上述版权
通知,此条件列表和以下免责声明。
*二进制形式的再分发必须重现以上
版权声明,此条件列表和以下
文件中的免责声明和/或提供的其他材料
with the distribution。
*这两个名字都没有作者的名称或其名称
贡献者可用于支持或宣传产品未经事先书面许可,从此软件派生


< span class =code-comment>此软件由版权所有者和贡献者提供

按原样以及任何明示或暗示的保证,包括但不
限于,适销性和适用性的暗示保证
对于特定目的,我们不承担任何责任。在任何情况下
版权所有者或贡献者对任何直接的,间接的,
偶发,特殊,惩戒或间接损害(包括,
但不限于购买替代商品或服务;
损失使用,数据或利润;或业务中断)但
引起并承担任何责任理论,无论是合同还是严格
引起责任或侵权(包括疏忽或其他)
任何使用本软件的方式,即使是ADVI SED OF THE
此类损害的可能性。

import sys,math,Image

def 距离(p1, p2):
dx = p2 [ 0 ] - p1 [ 0 ]
dy = p2 [ 1 ] - p1 [ 1 ]
返回 math.sqrt(dx * dx + dy * dy)

def ScaleRotateTranslate(图像,角度,中心) = ,new_center = ,scale = ,resample = Image.BICUBIC):
if (scale (中心 ):
return image.rotate(angle = angle,resample = resample)
nx,ny = x ,y = center
sx = sy = 1. 0
如果 new_center:
(nx,ny)= new_center
如果比例:
(sx,sy)=(比例,比例)
cosine = math.cos(angle)
sine = math.sin(angle)
a = cosine / sx
b = sine / sx
c = x-nx * a-ny * b
d = -sine / sy
e = cosine / sy
f = y-nx * d-ny * e
return image.transform(image.size,Image.AFFINE,(a,b,c,d,e,f),resample = resample)

def CropFace(image,eye_left =( 0 0 ),eye_right =( 0 0 ),offset_pct =( 0 。< span class =code-digit> 2
0 2 ),dest_sz =( 70 70 )):
计算原始图像的偏移量
offset_h = math.floor(float(offset_pct [ 0 ])* dest_sz [ 0 ])
offset_v = math.floor(float(offset_pct [ 1 ])* dest_sz [ 1 ])
获取方向
eye_direction =(eye_right [ 0 ] - eye_left [ 0 ],eye_right [ 1 ] - eye_left [ 1 ])
以弧度为单位的calc旋转角度
rotation = -math.atan2(float(eye_direction [ 1 ]),floa t(eye_direction [ 0 ]))
它们之间的距离
dist =距离(eye_left,eye_right)
计算参考眼宽
reference = dest_sz [ 0 ] - 2 0 * offset_h
比例因子
scale = float(dist)/ float(reference)
旋转原始的左眼
image = ScaleRotateTranslate(image,center = eye_left,angle = rotation)
裁剪旋转的图像
crop_xy =(eye_left [ 0 ] - 缩放* offset_h,eye_left [ 1 ] - sca le * offset_v)
crop_size =(dest_sz [ 0 ] * scale,dest_sz [ 1 ] * scale)
image = image.crop((int(crop_xy [ 0 ]),int(crop_xy [ 1 ]),int(crop_xy [ 0 ] + crop_size [ 0 ]),int (crop_xy [ 1 ] + crop_size [ 1 ])))
调整大小
image = image.resize(dest_sz,Image.ANTIALIAS)
return image

if __name__ == __ main __
image = Image.open( C:\\right135 \\right135(1).jpg
CropFace(image,eye_left =( 270 260 ),eye_right =( 342 256 ) ,offset_pct =( 0 3 0 3 ),dest_sz =( 92 112 )).save( C:\\right135 \\g.jpg







基本上我现在想要实现的是第二个最后一行代码CropFace位于,eye_left和eye_right坐标可以直接从我的c#窗体中的文本框中读取并放置在那里。有谁有想法吗?



请帮助,谢谢。

解决方案

输入类似%的代码python脚本中的eye_l%%eye_r%并执行以下操作:

  string  s = File.ReadAllText(  python文件名这里)。替换( %eye_l% 你的新左值。)替换( %eye_r% 您的新权利值 ); 
File.WriteAllText( python file name here,s);


I have a project in visual studio that can retrieve 2 different x,y coordinates on mouse click now. The coordinates would then be displayed in a textbox. I also have a python script that can crop faces using the x,y coordinates that are picked out on mouse click. The problem I am facing now is that I do not have any idea how to take the coordinates from the textboxes and directly place it in the python script without having to open the python script and change it manually.

#!/usr/bin/env python
# Software License Agreement (BSD License)
#
# Copyright (c) 2012, Philipp Wagner
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above
#    copyright notice, this list of conditions and the following
#    disclaimer in the documentation and/or other materials provided
#    with the distribution.
#  * Neither the name of the author nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import sys, math, Image

def Distance(p1,p2):
  dx = p2[0] - p1[0]
  dy = p2[1] - p1[1]
  return math.sqrt(dx*dx+dy*dy)

def ScaleRotateTranslate(image, angle, center = None, new_center = None, scale = None, resample=Image.BICUBIC):
  if (scale is None) and (center is None):
    return image.rotate(angle=angle, resample=resample)
  nx,ny = x,y = center
  sx=sy=1.0
  if new_center:
    (nx,ny) = new_center
  if scale:
    (sx,sy) = (scale, scale)
  cosine = math.cos(angle)
  sine = math.sin(angle)
  a = cosine/sx
  b = sine/sx
  c = x-nx*a-ny*b
  d = -sine/sy
  e = cosine/sy
  f = y-nx*d-ny*e
  return image.transform(image.size, Image.AFFINE, (a,b,c,d,e,f), resample=resample)

def CropFace(image, eye_left=(0,0), eye_right=(0,0), offset_pct=(0.2,0.2), dest_sz = (70,70)):
  # calculate offsets in original image
  offset_h = math.floor(float(offset_pct[0])*dest_sz[0])
  offset_v = math.floor(float(offset_pct[1])*dest_sz[1])
  # get the direction
  eye_direction = (eye_right[0] - eye_left[0], eye_right[1] - eye_left[1])
  # calc rotation angle in radians
  rotation = -math.atan2(float(eye_direction[1]),float(eye_direction[0]))
  # distance between them
  dist = Distance(eye_left, eye_right)
  # calculate the reference eye-width
  reference = dest_sz[0] - 2.0*offset_h
  # scale factor
  scale = float(dist)/float(reference)
  # rotate original around the left eye
  image = ScaleRotateTranslate(image, center=eye_left, angle=rotation)
  # crop the rotated image
  crop_xy = (eye_left[0] - scale*offset_h, eye_left[1] - scale*offset_v)
  crop_size = (dest_sz[0]*scale, dest_sz[1]*scale)
  image = image.crop((int(crop_xy[0]), int(crop_xy[1]), int(crop_xy[0]+crop_size[0]), int(crop_xy[1]+crop_size[1])))
  # resize it
  image = image.resize(dest_sz, Image.ANTIALIAS)
  return image

if __name__ == "__main__":
	image =  Image.open("C:\\right135\\right135(1).jpg")
	CropFace(image, eye_left=(270,260), eye_right=(342,256), offset_pct=(0.3,0.3), dest_sz=(92,112)).save("C:\\right135\\g.jpg")




Basically now what I would like to achieve is the SECOND LAST LINE of code where CropFace is at, the eye_left and eye_right coordinates can directly be read off the textboxes in my c# windows form and placed in there. Does anyone have any idea?

Please help, thank you.

解决方案

Put a code like %eye_l% and %eye_r% in the python script and do the following:

string s= File.ReadAllText("python file name here").Replace("%eye_l%", "your new left value").Replace("%eye_r%", "your new right value");
File.WriteAllText("python file name here", s); 


这篇关于如何使用c#编辑python脚本的某个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆