首页 > 编程语言 >使用Jupyter(python+opencv)实现特别难的脚本-Day2

使用Jupyter(python+opencv)实现特别难的脚本-Day2

时间:2024-06-17 16:30:29浏览次数:11  
标签:Jupyter python 0.5 image random Day2 delay time click

Day2

那昨天实现了这个自动挖土,我发现这个yb也是很扯0的东西,所以今天简单优化优化,完了再简单优化一下双手,写个yb吧。

首先依旧是库一小堆儿

from PIL import Image
import pyautogui
import random
import pygetwindow as gw
import time

然后那既然是优化那肯定是面向对象优化了,对吧,那就简单封装一下吧。

那窗口函数会吧?

def get_window(title):
    return gw.getWindowsWithTitle(title)[0]

那输出新button尺寸也会吧?

def resize_image(original_path, scale_x, scale_y, save_path):
    image = Image.open(original_path)
    new_size = (int(image.width * scale_x), int(image.height * scale_y))
    resized_image = image.resize(new_size, Image.ANTIALIAS)
    resized_image.save(save_path)
    return save_path

再写个小小的随机时延也会吧?

def random_delay(min_delay, max_delay):
    delay = random.uniform(min_delay, max_delay)
    time.sleep(delay)
    return delay

最后一个简单的click函数也会吧?

当然后期还需要增加双击的操作。(如果有人帮我写那就更好了

def click_target(image_path, offset_x_ratio, offset_y_ratio, randomSacle_x, randomSacle_y, clickTimes,  confidenceThreshold, duration = 5):
    start_time = time.time()
    end_time = start_time + duration
    click_count = 0
    while time.time() < end_time and click_count < clickTimes:
        try:
            match = pyautogui.locateOnScreen(image_path, confidence=confidenceThreshold)
            if match:
                print(f"找到一个匹配位置:{match}")
                random_x = 2 * random.random() - 1
                random_y = 2 * random.random() - 1
                clickPos_x = match.left + match.width * offset_x_ratio + random_x * randomSacle_x
                clickPos_y = match.top + match.height * offset_y_ratio + random_y * randomSacle_y
                pyautogui.click(clickPos_x, clickPos_y, button='left')
                click_count += 1
        except Exception as e:
            print("Error:", e)
            time.sleep(1)
    print("已经完成")

那这边我是考虑到了以后可能会有更复杂的这个任务所以集成了一个多顺序按钮的这么一个函数,当然g了一下

def click_sequence(click_operations: List[Tuple[str, float, float, float, float, int, float, int]], max_successes: int):
    success_count = 0
    while success_count < max_successes:
        for operation in click_operations:
            image_path, offset_x_ratio, offset_y_ratio, randomScale_x, randomScale_y, clickTimes, confidenceThreshold, duration = operation
            click_target(image_path, offset_x_ratio, offset_y_ratio, randomScale_x, randomScale_y, clickTimes, confidenceThreshold, duration)
            time.sleep(random_delay(0.5, 3))
        success_count += 1
    print(f"已完成{max_successes}次数的循环点击")

然后最简单的main函数会写吧?

def main():
    window = get_window('叉叉xy')
    eventButtonPath = resize_image(r"E:\footbook\eventButton1.png", window.width/1132, window.height/667, 'E:\\footbook\\eventButton_newtest1.png')
    yunbiaoButton1Path = resize_image(r"E:\footbook\yunbiaoButton1.png", window.width/1095, window.height/646, 'E:\\footbook\\yunbiaoButton_newtest1.png')
    yunbiaoButton2Path = resize_imagexx(r"E:\footbook\yunbiaoButton2.png", window.width/1094, window.height/645, 'E:\\footbook\\yunbiaoButton_newtest2.png')
    yunbiaoButton3Path = resize_image(r"E:\footbook\yunbiaoButton3.png", window.width/1094, window.height/645, 'E:\\footbook\\yunbiaoButton_newtest3.png')
    
    
    click_target(eventButtonPath, 0.5, 0.8, 5, 2, 1, 0.7)
    time.sleep(random_delay(0.5, 3))
    click_target(yunbiaoButton1Path, 0.8, 0.5, 5, 2, 1, 0.85)

#     time.sleep(random_delay(0.5, 3))
#     click_target(yunbiaoButton2Path, 0.5, 0.5, 30, 5, 1, 0.85, 300)  # 设置了时间限制为300秒
#     time.sleep(random_delay(0.5, 3))
#     click_target(yunbiaoButton3Path, 0.5, 0.5, 20, 5, 1, 0.85, 300)  # 设置了时间限制为300秒
    
    click_operations = [
        (yunbiaoButton2Path, 0.5, 0.5, 30, 5, 1, 0.85, 300),
        (yunbiaoButton3Path, 0.5, 0.5, 20, 5, 1, 0.85, 100)
    ]
    
    click_sequence(click_operations, 3)

    loop_clicks(yunbiaoButton2Path, yunbiaoButton3Path, 0.5, 0.5, 30, 5, 0.85, 350, 2)
    
if __name__ == "__main__":
    main()

完事儿!!!

那上面其中注释部分就是我一开始没有做这个集成函数直接用这个click写的这么一个过程,但是发现只能yb一次对吧,但是咱们的yb是有三次的嘛,对吧。那今天就搞到这了。下班!!!

标签:Jupyter,python,0.5,image,random,Day2,delay,time,click
From: https://blog.csdn.net/qq_52933098/article/details/139747611

相关文章

  • 使用Jupyter(python+opencv)实现很难的脚本-Day1
    由于xx西游没办法自动挖图,于是懒狗的我只能自己写一段脚本来实现挖土自由。首先介绍几个比较重要的库都需要自行install。fromPILimportImage#用于计算图片大小的库importpyautogui#用于抓取目标位置的库importpygetwindowasgw#用于得到窗口大小的库......
  • 开发一个python工具,pdf转图片,并且截成单个图片,然后修整没用的白边及循环遍历文件夹全
    今天推荐一键款本人开发的pdf转单张图片并截取没有用的白边工具一、开发背景:业务需要将一个pdf文件展示在前端显示,但是基于各种原因,放弃了h5使用插件展示原因有多个,文件资源太大加载太慢、pdf展示兼容性问题、pdf展示效果不好、pdf字体有时缺失等等,所以将项目中的协议等,全部由p......
  • python中常见re正则表达式(整数、小数、邮箱、号码、车牌、x开头y结尾)大合集(值得收
    目录专栏导读库的介绍库的安装1、匹配整数2、匹配某几位整数3、匹配小数4、匹配电话格式1:11位数字格式2:187-12341234或者187-1234-1234格式3:(123)456-7890,或者+86123-456-78905、匹配邮箱6、匹配车牌7、xx为开头yy为结尾9、匹配中文10、匹配非中文总结专栏导读......
  • 交叉编译python第三方库
    这里我们以编译androidpython程序为例工具crossenv名词对于交叉编译的各个部分,没有标准的词汇表,不同的资源经常会使用相互矛盾的术语。为了避免混淆,我们只使用GNU术语,这是Python本身使用的。host就是你编译出来的包要运行的平台,比如这里是Androidbuild进......
  • Python中的迭代器与生成器
    目录一、引言二、迭代器(Iterator)迭代器的概念迭代器的使用三、生成器(Generator)生成器的概念生成器的使用四、迭代器与生成器的性能与内存优化性能优化内存优化五、案例分析六、进阶用法推导式(Comprehensions)生成器表达式(GeneratorExpressions)七、总结一、引......
  • 精选了10个Python实战项目(附源码),拿走即用!
    ① 猜字游戏在这个游戏中,你必须一个字母一个字母的猜出秘密单词。如果你猜错了一个字母,你将丢掉一条命。正如游戏名那样,你需要仔细选择字母,因为你的生命数量非常有限。importrandom#生命次数lives=3#神秘单词,随机选择words=['pizza','fairy','teeth','......
  • 小白的Python+Anaconda+vscode安装教程(win11系统手把手教学)
    python下载安装python下载安装过程下载地址:https://www.python.org/![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/33bd022a0d104a22b9e8caf7abb4e294.png我这里选择了3.7.9版本选中刚才下载的.exe文件,右键-打卡上一个页面点击customizinstallation后......
  • Day28.学校与班级建关联
    1.学校与班级建关联_班级类,将班级和班级对应的课程信息生成对象'''班级'''classClass:#__init__中,初始化单个对象,记录每个班级独有的东西def__init__(self,class_name):self.class_name=class_name#初始班级时,班级没有课程表self......
  • Python遥感影像叠加分析:基于一景数据提取另一数据
      本文介绍基于Python中GDAL模块,实现基于一景栅格影像,对另一景栅格影像的像元数值加以叠加提取的方法。  本文期望实现的需求为:现有一景表示6种不同植被类型的.tif格式栅格数据,以及另一景与前述栅格数据同区域的、表示植被参数的.tif格式栅格数据;我们希望基于前者中的植被类......
  • 盘点一个Python自动化办公的问题
    大家好,我是Python进阶者。一、前言前几天在Python铂金交流群【逆光】问了一个Python自动化办公的问题,问题如下:问题我现在有两个表a、b,for循环a、b,如果a的条件满足b,则把b的值赋给a,目前a有7万条数据,b有300条。我写的代码20分钟都没跑完。这是代码,请问改怎么解决?二、实现过......