Skip to content

Commit

Permalink
Add config for image module
Browse files Browse the repository at this point in the history
Signed-off-by: xcgspring <[email protected]>
  • Loading branch information
xcgspring committed Dec 15, 2014
1 parent 57aa565 commit f9b4226
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 5 deletions.
2 changes: 2 additions & 0 deletions AXUI/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logger
import driver
import XML
import image

def Config(config_file=""):
'''config the system
Expand All @@ -22,6 +23,7 @@ def Config(config_file=""):
config.config(logger)
config.config(XML)
config.config(driver)
config.config(image)

#port AppMap here
AppMap=XML.AppMap
Expand Down
11 changes: 10 additions & 1 deletion AXUI/global.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@ screenshot_location = abspath
#enable screenshot when fail happens
#can only set to True or False, other value will be ignore
screenshot_on_failure = False


[image]
#if generate diff image
#can only set to True or False, other value will be ignore
gen_diff_image = True

#diff image location
#need abspath
diff_image_location = abspath

[driver]
#driver used in your UI automation
driver_used = windows
Expand Down
4 changes: 3 additions & 1 deletion AXUI/image/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@

#used by config module
from image_config import *
#other interface
from image_compare import image_compare
18 changes: 15 additions & 3 deletions AXUI/image/image_compare.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@

import os
from itertools import izip
from PIL import Image
from PIL import Image, ImageChops

import image_config
from AXUI.logger import LOGGER

def image_compare(image1, image2):
def image_compare(image1, image2, diff_image_name="diff.bmp"):
'''compare two images, return difference percentage
#code from http://rosettacode.org/wiki/Percentage_difference_between_images#Python
'''
gen_diff_image = image_config.query_gen_diff_image()
diff_image_location = image_config.query_diff_image_location()

i1 = Image.open(image1)
i2 = Image.open(image2)
assert i1.mode == i2.mode, "Different kinds of images: %s VS %s" % (i1.mode, i2.mode)
assert i1.size == i2.size, "Different sizes: %s" % (i1.size, i2.size)


#generate diff bitmap
if gen_diff_image:
diff = ImageChops.difference(i1, i2)
diff_image_path = os.path.join(diff_image_location, diff_image_name)
diff.save(diff_image_path)
LOGGER().debug("Diff image save to: %s" % diff_image_path)

#caculate the diff percentage
pairs = izip(i1.getdata(), i2.getdata())
if len(i1.getbands()) == 1:
# for gray-scale jpegs
Expand Down
42 changes: 42 additions & 0 deletions AXUI/image/image_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

import os

config_section="image"
default_configs={ "gen_diff_image" : "True",
"diff_image_location" : os.path.dirname(os.path.abspath(__file__)),
}

GenDiffImage=default_configs["gen_diff_image"]
DiffImageLocation=default_configs["diff_image_location"]

def config(configs=default_configs):
'''call back function used by config module
set the global variables according to configuration
'''
global GenDiffImage
global DiffImageLocation

GenDiffImage=configs["gen_diff_image"]
DiffImageLocation=configs["diff_image_location"]

#used by config module
__all__=["config_section", "default_configs", "config"]

def query_diff_image_location():
'''query diff_image_location from config
'''
if not os.path.isdir(DiffImageLocation):
os.makedirs(DiffImageLocation)

return DiffImageLocation

def query_gen_diff_image():
'''query gen_diff_image from config
'''
results = {"True":True, "False":False}

if GenDiffImage in results:
return results[GenDiffImage]
else:
#use default
return results[default_configs["gen_diff_image"]]
9 changes: 9 additions & 0 deletions example/windows/windows.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ screenshot_location = c:\screenshot
#enable screenshot when fail happens
#can only set to True or False, other value will be ignore
screenshot_on_failure = False

[image]
#if generate diff image
#can only set to True or False, other value will be ignore
gen_diff_image = True

#diff image location
#need abspath
diff_image_location = c:\screenshot\diff

[driver]
#driver used in your UI automation
Expand Down

0 comments on commit f9b4226

Please sign in to comment.