python3 ip:port格式转化成http、https格式文件

新手学习python第一个程序:

此脚本可以将ip:port格式的ip.txt文件转化成http\https格式的url.txt文件

如:

ip.txt:

1.1.1.1:80

1.1.1.1:8080

转化后:

url.txt

http://1.1.1.1:80

https://1.1.1.1:8080

http://1.1.1.1:80

https://1.1.1.1:8080

脚本如下:

#ip.txt中ip:port格式转换成http、https格式,保存到url.txt中
with open("ip.txt","r") as f:
    line = f.readlines()

with open("url.txt","w") as f2:
    for i in line:
        i=i.strip('\n')
        if 'http://' not in i and 'https://' not in i:       
            f2.write('http://'+i+'\n')
            f2.write('https://'+i+'\n')
        else:
            f2.write(i+'\n')

出来。 以下是示例代码实现: ```python import cv2 import numpy as np import time # 读取图像 img = cv2.imread('image.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 提取轮廓 _, contours, _ = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 构建轮廓掩模 mask = np.zeros(gray.shape, np.uint8) cv2.drawContours(mask, contours, -1, 255, -1) # 循环读取实时图像 cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() if not ret: break # 转换为灰度图像 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 通过掩模提取轮廓 masked_gray = cv2.bitwise_and(gray, mask) _, thresh = cv2.threshold(masked_gray, 10, 255, cv2.THRESH_BINARY) _, contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 绘制轮廓 cv2.drawContours(frame, contours, -1, (0, 0, 255), 2) # 记录313列的y值 y_values = [] for cnt in contours: for pt in cnt: x, y = pt[0] if x == 313: y_values.append(y) # 记录3秒内的y值 if len(y_values) > 0: start_time = time.time() while time.time() - start_time < 3: ret, frame = cap.read() if not ret: break gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) masked_gray = cv2.bitwise_and(gray, mask) _, thresh = cv2.threshold(masked_gray, 10, 255, cv2.THRESH_BINARY) _, contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(frame, contours, -1, (0, 0, 255), 2) for cnt in contours: for pt in cnt: x, y = pt[0] if x == 313: y_values.append(y) # 计算最大值和最小值的差值 if len(y_values) > 0: ymin = min(y_values) ymax = max(y_values) diff = ymax - ymin cv2.putText(frame, f'Diff: {diff}', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) # 显示图像 cv2.imshow('frame', frame) if cv2.waitKey(1) == ord('q'): break cap.release() cv2.destroyAllWindows() ``` 其中,首先读取图像并提取轮廓,然后进入实时读取图像的循环中。在每一帧图像中,通过轮廓掩模提取轮廓,并记录313列的y值。如果存在y值,则记录接下来3秒内的y值,并计算最大值和最小值的差值。最后在图像中显示差值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值