Skip to content

Commit

Permalink
Port generate_test_mask_image.py to python3
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 3, 2016
1 parent 19f6b62 commit 377de52
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions scripts/generate_test_mask_image.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Expand Down Expand Up @@ -35,12 +35,12 @@
import argparse
from PyQt5.QtGui import QImage, QColor, qRed, qBlue, qGreen, qAlpha, qRgb
import struct
import urllib2
import urllib.request, urllib.error, urllib.parse
import glob


def error(msg):
print msg
print(msg)
sys.exit(1)


Expand All @@ -53,9 +53,9 @@ def colorDiff(c1, c2):


def imageFromPath(path):
if (path[:7] == 'http://' or path[:7] == 'file://'):
if (path[:7] == 'http://' or path[:7] == 'file://' or path[:8] == 'https://'):
#fetch remote image
data = urllib2.urlopen(path).read()
data = urllib.request.urlopen(path).read()
image = QImage()
image.loadFromData(data)
else:
Expand Down Expand Up @@ -88,7 +88,7 @@ def getControlImagePath(path):
error('No matching control images found for {}'.format(path))

found_image = filtered_images[0]
print 'Found matching control image: {}'.format(found_image)
print('Found matching control image: {}'.format(found_image))
return found_image


Expand All @@ -101,30 +101,30 @@ def updateMask(control_image_path, rendered_image_path, mask_image_path):
if not rendered_image:
error('Could not read rendered image {}'.format(rendered_image_path))
if not rendered_image.width() == control_image.width() or not rendered_image.height() == control_image.height():
print ('Size mismatch - control image is {}x{}, rendered image is {}x{}'.format(control_image.width(),
print(('Size mismatch - control image is {}x{}, rendered image is {}x{}'.format(control_image.width(),
control_image.height(),
rendered_image.width(),
rendered_image.height()))
rendered_image.height())))

max_width = min(rendered_image.width(), control_image.width())
max_height = min(rendered_image.height(), control_image.height())

#read current mask, if it exist
mask_image = imageFromPath(mask_image_path)
if mask_image.isNull():
print 'Mask image does not exist, creating {}'.format(mask_image_path)
print('Mask image does not exist, creating {}'.format(mask_image_path))
mask_image = QImage(control_image.width(), control_image.height(), QImage.Format_ARGB32)
mask_image.fill(QColor(0, 0, 0))

#loop through pixels in rendered image and compare
mismatch_count = 0
linebytes = max_width * 4
for y in xrange(max_height):
for y in range(max_height):
control_scanline = control_image.constScanLine(y).asstring(linebytes)
rendered_scanline = rendered_image.constScanLine(y).asstring(linebytes)
mask_scanline = mask_image.scanLine(y).asstring(linebytes)

for x in xrange(max_width):
for x in range(max_width):
currentTolerance = qRed(struct.unpack('I', mask_scanline[x * 4:x * 4 + 4])[0])

if currentTolerance == 255:
Expand All @@ -143,9 +143,9 @@ def updateMask(control_image_path, rendered_image_path, mask_image_path):
if mismatch_count:
#update mask
mask_image.save(mask_image_path, "png")
print 'Updated {} pixels in {}'.format(mismatch_count, mask_image_path)
print('Updated {} pixels in {}'.format(mismatch_count, mask_image_path))
else:
print 'No mismatches in {}'.format(mask_image_path)
print('No mismatches in {}'.format(mask_image_path))

parser = argparse.ArgumentParser() # OptionParser("usage: %prog control_image rendered_image mask_image")
parser.add_argument('control_image')
Expand Down

0 comments on commit 377de52

Please sign in to comment.