-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_analysis.py
More file actions
68 lines (53 loc) · 2.32 KB
/
image_analysis.py
File metadata and controls
68 lines (53 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from selenium import webdriver
from PIL import Image, ImageDraw
class ImageAnalysis:
STAGING_URL = 'https://15min.lt/'
PRODUCTION_URL = 'https://15min.lt/'
driver = None
def __init__(self):
self.set_up()
self.capture_screens()
self.analyze()
self.clean_up()
def set_up(self):
self.driver = webdriver.PhantomJS()
def clean_up(self):
self.driver.close()
def capture_screens(self):
self.screenshot(self.STAGING_URL, 'screen_staging.png')
self.screenshot(self.PRODUCTION_URL, 'screen_production.png')
def screenshot(self, url, file_name):
print ("Capturing", url, "screenshot as", file_name, "...")
self.driver.get(url)
self.driver.set_window_size(1024, 768)
self.driver.save_screenshot(file_name)
self.driver.get_screenshot_as_png()
print ("Done.")
def analyze(self):
screenshot_staging = Image.open("screen_staging.png")
screenshot_production = Image.open("screen_production.png")
columns = 60
rows = 80
screen_width, screen_height = screenshot_staging.size
block_width = ((screen_width - 1) // columns) + 1
block_height = ((screen_height - 1) // rows) + 1
for y in range(0, screen_height, block_height+1):
for x in range(0, screen_width, block_width+1):
region_staging = self.process_region(screenshot_staging, x, y, block_width, block_height)
region_production = self.process_region(screenshot_production, x, y, block_width, block_height)
if region_staging is not None and region_production is not None and region_production != region_staging:
draw = ImageDraw.Draw(screenshot_staging)
draw.rectangle((x, y, x+block_width, y+block_height), outline = "red")
screenshot_staging.save("result.png")
def process_region(self, image, x, y, width, height):
region_total = 0
factor = 100
for coordinateY in range(y, y+height):
for coordinateX in range(x, x+width):
try:
pixel = image.getpixel((coordinateX, coordinateY))
region_total += sum(pixel)/4
except:
return
return region_total/factor
ImageAnalysis()