您好,我有以下代码:
login=os.getlogin()
if not os.path.exist("C:\Users\login\Documents")
如您所见,我有一个登录变量来获取计算机的用户,但是我正在努力在“ if not os.path.exist”语句中使用它,我想要的输出是让python获取用户的用户名,并在目录中使用它。
谢谢!
答案 0 :(得分:0)
您可以使用字符串格式:
import cv2
import numpy as np
winName = "ForeGround Segment"
def foreground_segment(im, brightness, useEqualize=1, blurSize=21, th1=None):
winName = "ForeGround Segment"
tmp = brightness
if (blurSize >= 3):
blurSize += (1 - blurSize % 2)
tmp = cv2.GaussianBlur(tmp, (blurSize, blurSize), 0)
if (useEqualize):
tmp = cv2.equalizeHist(tmp)
cv2.imshow('brightness preprocess', tmp)
#cv2.imwrite('../BrightnessPreprocess.png', tmp)
ret, tmp = cv2.threshold(tmp, th1, 255, cv2.THRESH_BINARY_INV)
cv2.imshow('threshold', tmp)
im2, contours, hierarchy = cv2.findContours(tmp, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
dst = im.copy()
#print(contours)
maxDim = 0
largest = -1
for i in range(len(contours)):
cv2.drawContours(dst, contours, largest, (0, 0, 255), 1)
dim = len(contours[i])
if (dim > maxDim):
maxDim = dim
largest = i
img_mask = np.zeros(src.shape, np.uint8)
if (largest >= 0):
theImg = contours[largest]
cv2.polylines(dst, theImg, True, (0, 255,0), 2)
cv2.drawContours(img_mask, contours, largest, 255, -1)
cv2.imshow("Result Mask", img_mask)
cv2.imshow("Result Contour", dst)
#cv2.imwrite("../img_mask.png", img_mask)
#cv2.imwrite("../result.png", dst)
if __name__ == '__main__':
src = cv2.imread('frontal.png')
src = cv2.resize(src, (int(src.shape[0]/3), int(src.shape[1]/3)), interpolation = cv2.INTER_AREA)
cv2.imshow(winName, src)
dst = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)
hsv_planes = cv2.split(dst)
brightness = hsv_planes[2]
useEqualize = 1
blurSize = 21
th1 = int(33.0 * 255 / 100)
cv2.createTrackbar("Equalize", winName, useEqualize, 1, foreground_segment)
cv2.createTrackbar("Blur Sigma", winName, blurSize, 100, foreground_segment)
cv2.createTrackbar("Threshold", winName, th1, 255, foreground_segment)
while(1):
#--- Using cv2.getTrackbarPos() to get values from the slider ---
useEqualize = cv2.getTrackbarPos('Equalize', winName)
blurSize = cv2.getTrackbarPos('Blur Sigma', winName)
th1 = cv2.getTrackbarPos('Threshold', winName)
foreground_segment(src, brightness, useEqualize, blurSize, th1)
#--- Press Q to quit ---
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
或者您可以自己构建字符串:
if not os.path.exists("C:\Users\{0}\Documents".format(login)):
或者如果使用join:
if not os.path.exists("C:\Users\\" + login + "\Documents"):
正如评论所指出的,您还应该查看 question。这显示了如何正确获取登录名 名称。