Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 726395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:26:49+00:00 2026-05-14T06:26:49+00:00

Firstly sorry for the long piece of code pasted below. This is my first

  • 0

Firstly sorry for the long piece of code pasted below.
This is my first time actually having to worry about performance of an application so I haven’t really ever worried about performance.
This piece of code pretty much searches for an image inside another image, it takes 30 seconds to run on my computer, converting the images to greyscale and other changes shaved of 15 seconds, I need another 15 shaved off. I did read a bunch of pages and looked at examples but I couldn’t find the same problems in my code. So any help would be greatly appreciated.

From the looks of it (cProfile) 25 seconds is spent within the Image module, and only 5 seconds in my code.

from PIL import Image
import os, ImageGrab, pdb, time, win32api, win32con
import cProfile

def GetImage(name):
    name = name + '.bmp'
    try:
        print(os.path.join(os.getcwd(),"Images",name))
        image = Image.open(os.path.join(os.getcwd(),"Images",name))
    except:
        print('error opening image;', name)
    return image

def Find(name):
    image = GetImage(name)
    imagebbox = image.getbbox()
    screen = ImageGrab.grab()
    #screen = Image.open(os.path.join(os.getcwd(),"Images","Untitled.bmp"))
    YLimit =  screen.getbbox()[3] - imagebbox[3]
    XLimit = screen.getbbox()[2] - imagebbox[2]
    image = image.convert("L")
    Screen = screen.convert("L")
    Screen.load()
    image.load()
    #print(XLimit, YLimit)
    Found = False
    image = image.getdata()
    for y in range(0,YLimit):
        for x in range(0,XLimit):
            BoxCoordinates = x, y, x+imagebbox[2], y+imagebbox[3]
            ScreenGrab = screen.crop(BoxCoordinates)
            ScreenGrab = ScreenGrab.getdata()
            if image == ScreenGrab:
                Found = True
                #print("woop")
                return x,y
    if Found == False:
        return "Not Found"
cProfile.run('print(Find("Login"))')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:26:50+00:00Added an answer on May 14, 2026 at 6:26 am

    while not directly performance related you could do some things to improve your code:

    if not Found:
        return "Not Found"
    

    is idiomatic way to write condition in Python. You don’t need this clause, however, since this return statement could be reached only if image wasn’t found.

    in GetImage you should create filename once with os.path.join(os.getcwd(),"Images",name) to minimize errors and not to repeat yourself. Also it wouldn’t normally work if you don’t have the image file. Since you’re not handling error in the Find, I’d suggest the following:

    def Find(name):
        fname = os.path.join(os.getcwd(), "Images", name + '.bmp')
        image = Image.open(fname)
        imagebbox = image.getbbox()
        screen = ImageGrab.grab()
        YLimit =  screen.getbbox()[3] - imagebbox[3]
        XLimit = screen.getbbox()[2] - imagebbox[2]
        image = image.convert("L")
        Screen = screen.convert("L")
        Screen.load()
        image.load()
        image = image.getdata()
        for y in range(0, YLimit):
            for x in range(0, XLimit):
                BoxCoordinates = x, y, x+imagebbox[2], y+imagebbox[3]
                ScreenGrab = screen.crop(BoxCoordinates)
                ScreenGrab = ScreenGrab.getdata()
                if image == ScreenGrab:
                    return x, y
        # returns None implicitly
    

    Your major problem is that you’re doing pixel by pixel search, it’s going to be slow on any meaningful size image.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.