def _scale_image(file_name, size_w, size_h):
''' This function calculates ratio and scales an image for comparison by _pyautogui() '''
sModuleInfo = inspect.stack()[0][3] + " : " + inspect.getmoduleinfo(__file__).name
# Only used by desktop, so only import here
import pyautogui
from PIL import Image
from decimal import Decimal
try:
# Open image file
file_name = open(file_name, 'rb') # Read file into memory
file_name = Image.open(file_name) # Convert to PIL format
# Read sizes
screen_w, screen_h = pyautogui.size() # Read screen resolution
image_w, image_h = file_name.size # Read the image element's actual size
# Calculate new image size
if size_w > screen_w: # Make sure we create the scaling ratio in the proper direction
ratio = Decimal(size_w) / Decimal(screen_w) # Get ratio (assume same for height)
else:
ratio = Decimal(screen_w) / Decimal(size_w) # Get ratio (assume same for height)
CommonUtil.ExecLog(sModuleInfo, "Scaling ratio %s" %ratio, 0)
size = (int(image_w * ratio), int(image_h * ratio)) # Calculate new resolution of image element
# Scale image
file_name.thumbnail(size, Image.ANTIALIAS) # Resize image per calculation above
return file_name # Return the scaled image object
except:
return CommonUtil.Exception_Handler(sys.exc_info(), None, "Error scaling image")
LocateElement.py 文件源码
python
阅读 21
收藏 0
点赞 0
评论 0
评论列表
文章目录