def demo_normalize(): img = Image.open('data/laser-off.jpg').convert('RGBA') img01 = img arr = np.array(img) new_img = Image.fromarray(normalize(arr).astype('uint8'), 'RGBA') new_img = only_red(new_img) new_img.save('data/laser-off-normalized.jpg') new_img1 = new_img img = Image.open('data/laser-on.jpg').convert('RGBA') img02 = img arr = np.array(img) new_img = Image.fromarray(normalize(arr).astype('uint8'), 'RGBA') new_img = only_red(new_img) new_img.save('data/laser-on-normalized.jpg') new_img2 = new_img laser_diff0 = difference(img01, img02) laser_diff0.save('data/laser-diff0.jpg') laser_diff1 = difference(new_img1, new_img2) laser_diff1.save('data/laser-diff1.jpg') find_line(laser_diff0, 'data/laser-line0.jpg', filter_outliers=0) find_line(laser_diff1, 'data/laser-line1.jpg')
def equal(self, img1, img2, skip_area=None): """Compares two screenshots using Root-Mean-Square Difference (RMS). @param img1: screenshot to compare. @param img2: screenshot to compare. @return: equal status. """ if not HAVE_PIL: return None # Trick to avoid getting a lot of screen shots only because the time in the windows # clock is changed. # We draw a black rectangle on the coordinates where the clock is locates, and then # run the comparison. # NOTE: the coordinates are changing with VM screen resolution. if skip_area: # Copying objects to draw in another object. img1 = img1.copy() img2 = img2.copy() # Draw a rectangle to cover windows clock. for img in (img1, img2): self._draw_rectangle(img, skip_area) # To get a measure of how similar two images are, we use # root-mean-square (RMS). If the images are exactly identical, # this value is zero. # diff = ImageChops.difference(img1, img2) diff = difference(img1, img2) h = diff.histogram() sq = (value * ((idx % 256) ** 2) for idx, value in enumerate(h)) sum_of_squares = sum(sq) rms = math.sqrt(sum_of_squares / (img1.size[0] * img1.size[1])) # Might need to tweak the threshold. return rms < 8
def compare_screenshot_to_base(baseline, diff=100): """Calculate the exact difference between two images. :param string baseline: [required] base screenshot to compare :param int diff: value of maximum difference Example:: Compare screenshot to base base_screenshot.jpg """ path = _get_screenshot() current_browser = _get_browser() if hasattr(current_browser, 'get_screenshot_as_file'): current_browser.get_screenshot_as_file(path) else: current_browser.save_screenshot(path) img1 = Iopen(path) img2 = Iopen(baseline) his1 = img1.histogram() his2 = img2.histogram() sqrtdiff = lambda a, b: (a - b) ** 2 rms = sqrt(reduce(add, imap(sqrtdiff, his1, his2)) / len(his1)) logger.info("RMS diff: %s" % rms) if rms > 0: idiff = difference(img1, img2) path = path.replace(".png", ".jpg") idiff.save(path) logger.info("diff image: %s" % path) if rms > diff: raise AssertionError( "Image: %s is different from baseline: %s" % (path, baseline))
def compare_screenshot_to_base(baseline, diff=100): """Calculate the exact difference between two images. :param string baseline: [required] base screenshot to compare :param int diff: value of maximum difference Example:: Compare screenshot to base base_screenshot.jpg """ path = _get_screenshot() current_browser = _get_browser() if hasattr(current_browser, 'get_screenshot_as_file'): current_browser.get_screenshot_as_file(path) else: current_browser.save_screenshot(path) img1 = Iopen(path) img2 = Iopen(baseline) his1 = img1.histogram() his2 = img2.histogram() sqrtdiff = lambda a, b: (a - b)**2 rms = sqrt(reduce(add, imap(sqrtdiff, his1, his2)) / len(his1)) logger.info("RMS diff: %s" % rms) if rms > 0: idiff = difference(img1, img2) path = path.replace(".png", ".jpg") idiff.save(path) logger.info("diff image: %s" % path) if rms > diff: raise AssertionError("Image: %s is different from baseline: %s" % (path, baseline))
def search_with_value(page: page_dat, chars: [char_dat], mult: float, max_x: int, max_y: int) -> wobble: histodiff_sum = 0 best_diff_sum = 9999999999999999999999999999999999999999999999999999999999999 max_wobble = mult * 1.1 wobinc = (max_wobble - mult) / 10 curx = mult cury = mult bestx = None besty = None while(curx < max_wobble and int(max_x * (curx + wobinc)) < page.x): curx += wobinc while(cury < max_wobble and int(max_y * (cury + wobinc)) < page.y): cury += wobinc histodiff_sum = 0 for char in chars: x1 = int(curx * char.x1) y1 = int(cury * char.y1) x2 = int(curx * char.x2) y2 = int(cury * char.y2) page_char = page.img.crop([y1, x1, y2, x2]) page_charr = autocontrast(page_char.resize([char.img.width, char.img.height], Image.BOX)) page_diff = difference(page_charr, char.img).histogram() histo_sum = 0 inc = 0 for diff in page_diff: inc += 1 histo_sum += diff * inc histodiff_sum += histo_sum if histodiff_sum < best_diff_sum: best_diff_sum = histodiff_sum bestx = curx besty = cury return wobble(bestx, besty, best_diff_sum)
def processImage(path): ''' Iterate the GIF, extracting each frame. ''' from PIL import Image from PIL.ImageChops import difference mode = analyseImage(path)['mode'] im = Image.open(path) i = 0 p = im.getpalette() last_frame = im.convert('RGBA') try: while True: print("saving %s (%s) frame %d, %s %s" % (path, mode, i, im.size, im.tile)) ''' If the GIF uses local colour tables, each frame will have its own palette. If not, we need to apply the global palette to the new frame. ''' if not im.getpalette(): im.putpalette(p) new_frame = Image.new('RGBA', im.size) ''' Is this file a "partial"-mode GIF where frames update a region of a different size to the entire image? If so, we need to construct the new frame by pasting it on top of the preceding frames. ''' if mode == 'partial': new_frame.paste(last_frame) new_frame.paste(im, (0,0), im.convert('RGBA')) new_frame.save('%s-%d.png' % (''.join(os.path.basename(path).split('.')[:-1]), i), 'PNG') diff_frame = difference(last_frame, new_frame) a = np.array(diff_frame) a[:,:,3] = 255 diff_frame = Image.fromarray(a) diff_frame.save('%s-diff-%d.png' % (''.join(os.path.basename(path).split('.')[:-1]), i), 'PNG') i += 1 last_frame = new_frame im.seek(im.tell() + 1) if i == 10: break except EOFError: pass
def search_with_value(page: page_dat, chars: [char_dat], mult: float) -> int: histodiff_sum = 0 for char in chars: x1 = int(mult * char.x1) y1 = int(mult * char.y1) x2 = int(mult * char.x2) y2 = int(mult * char.y2) page_char = page.img.crop([y1, x1, y2, x2]) page_charr = page_char.resize([char.img.width, char.img.height], Image.BOX) page_diff = difference(page_charr, char.img).histogram() histo_sum = 0 inc = 0 for diff in page_diff: inc += 1 histo_sum += diff * inc histodiff_sum += histo_sum return histodiff_sum
def diff(a, b): diff = difference(a.image, b.image) diffNormalized = autocontrast(diff) return (VideoFrame(diff), VideoFrame(diffNormalized))
def diff(a, b): diff = difference(a.image, b.image) diff_normalized = autocontrast(diff) return (VideoFrame(diff), VideoFrame(diff_normalized))
def get_distance(self, off_img, on_img, save_images_dir=None, as_pfc=False, **kwargs): """ Calculates distance using two images. Keyword arguments: off_img -- a stream or filename of an image assumed to have no laser projection on_img -- a stream of filename of an image assumed to have a laser projection """ if save_images_dir: save_images_dir = os.path.expanduser(save_images_dir) assert os.path.isdir( save_images_dir), 'Invalid directory: %s' % save_images_dir if isinstance(off_img, basestring): off_img = Image.open(os.path.expanduser(off_img)).convert('RGB') if isinstance(on_img, basestring): on_img = Image.open(os.path.expanduser(on_img)).convert('RGB') # Normalize image brightness. if self.normalize_brightness: off_img = Image.fromarray( utils.normalize(np.array(off_img)).astype('uint8'), 'RGB') if save_images_dir: off_img.save( os.path.join( save_images_dir, kwargs.pop('off_img_norm_fn', 'off_img_norm.jpg'))) on_img = Image.fromarray( utils.normalize(np.array(on_img)).astype('uint8'), 'RGB') if save_images_dir: on_img.save( os.path.join( save_images_dir, kwargs.pop('on_img_norm_fn', 'on_img_norm.jpg'))) # Strip out non-red channels. off_img = utils.only_red(off_img) if save_images_dir: off_img.save( os.path.join( save_images_dir, kwargs.pop('off_img_norm_red_fn', 'off_img_norm_red.jpg'))) on_img = utils.only_red(on_img) if save_images_dir: on_img.save( os.path.join( save_images_dir, kwargs.pop('on_img_norm_red_fn', 'on_img_norm_red.jpg'))) # Calculate difference. # The laser line should now be the brightest pixels. diff_img = difference(off_img, on_img) if save_images_dir: diff_img.save( os.path.join(save_images_dir, kwargs.pop('diff_img_fn', 'diff_img.jpg'))) if self.blur_radius: diff_img = diff_img.filter( ImageFilter.GaussianBlur(radius=self.blur_radius)) if save_images_dir: diff_img.save( os.path.join( save_images_dir, kwargs.pop('diff_blur_img_fn', 'diff2_img.jpg'))) # Estimate the pixels that are the laser by # finding the row in each column with maximum brightness. width, height = size = diff_img.size x = diff_img.convert('L') if save_images_dir: # If saving a result image, create an empty black image which we'll later map # the laser line into. out1 = Image.new("L", x.size, "black") pix1 = out1.load() out2 = Image.new("L", x.size, "black") pix2 = out2.load() out3 = Image.new("L", x.size, "black") pix3 = out3.load() y = np.asarray(x.getdata(), dtype=np.float64).reshape( (x.size[1], x.size[0])) laser_measurements = [0] * width # [row] laser_brightness = [0] * width # [brightness] for col_i in xrange(y.shape[1]): col_max = max([(y[row_i][col_i], row_i) for row_i in xrange(y.shape[0])]) col_max_brightness, col_max_row = col_max laser_measurements[col_i] = col_max_row laser_brightness[col_i] = col_max_brightness # Ignore all columns with dim brightness outliers. # These usually indicate a region where the laser is absorbed or otherwise scattered # too much to see. if self.filter_outliers: brightness_std = np.std(laser_brightness) brightness_mean = np.mean(laser_brightness) outlier_level = brightness_mean - brightness_std * self.outlier_filter_threshold final_measurements = [-1] * width # [brightest row] for col_i, col_max_row in enumerate(laser_measurements): if save_images_dir: pix1[col_i, col_max_row] = 255 if not self.filter_outliers \ or (self.filter_outliers and laser_brightness[col_i] > outlier_level): if save_images_dir: pix2[col_i, col_max_row] = 255 # Assuming the laser is mounted below the camera, # we can assume all points above the centerline are noise. if self.laser_position == BOTTOM and col_max_row < height / 2: continue elif self.laser_position == TOP and col_max_row > height / 2: continue if not self.filter_outliers \ or (self.filter_outliers and laser_brightness[col_i] > outlier_level): if save_images_dir: pix3[col_i, col_max_row] = 255 final_measurements[col_i] = col_max_row if save_images_dir: out1.save( os.path.join(save_images_dir, kwargs.pop('line_img1_fn', 'line1.jpg'))) out2.save( os.path.join(save_images_dir, kwargs.pop('line_img2_fn', 'line2.jpg'))) out3.save( os.path.join(save_images_dir, kwargs.pop('line_img3_fn', 'line3.jpg'))) # If directed, return raw pixels from center instead of distance calculation. if as_pfc: return final_measurements # Convert the pixel measurements to distance. D_lst = pixels_to_distance( pixel_rows=final_measurements, rpc=self.rpc, ro=self.ro, h=self.h, max_height=height, max_width=width, ) return D_lst
def get_distance(self, off_img, on_img, save_images_dir=None, as_pfc=False, **kwargs): """ Calculates distance using two images. Keyword arguments: off_img -- a stream or filename of an image assumed to have no laser projection on_img -- a stream of filename of an image assumed to have a laser projection """ if save_images_dir: save_images_dir = os.path.expanduser(save_images_dir) assert os.path.isdir(save_images_dir), 'Invalid directory: %s' % save_images_dir if isinstance(off_img, basestring): off_img = Image.open(os.path.expanduser(off_img)).convert('RGB') if isinstance(on_img, basestring): on_img = Image.open(os.path.expanduser(on_img)).convert('RGB') # Normalize image brightness. if self.normalize_brightness: off_img = Image.fromarray(utils.normalize(np.array(off_img)).astype('uint8'), 'RGB') if save_images_dir: off_img.save(os.path.join(save_images_dir, kwargs.pop('off_img_norm_fn', 'off_img_norm.jpg'))) on_img = Image.fromarray(utils.normalize(np.array(on_img)).astype('uint8'), 'RGB') if save_images_dir: on_img.save(os.path.join(save_images_dir, kwargs.pop('on_img_norm_fn', 'on_img_norm.jpg'))) # Strip out non-red channels. off_img = utils.only_red(off_img) if save_images_dir: off_img.save(os.path.join(save_images_dir, kwargs.pop('off_img_norm_red_fn', 'off_img_norm_red.jpg'))) on_img = utils.only_red(on_img) if save_images_dir: on_img.save(os.path.join(save_images_dir, kwargs.pop('on_img_norm_red_fn', 'on_img_norm_red.jpg'))) # Calculate difference. # The laser line should now be the brightest pixels. diff_img = difference(off_img, on_img) if save_images_dir: diff_img.save(os.path.join(save_images_dir, kwargs.pop('diff_img_fn', 'diff_img.jpg'))) if self.blur_radius: diff_img = diff_img.filter(ImageFilter.GaussianBlur(radius=self.blur_radius)) if save_images_dir: diff_img.save(os.path.join(save_images_dir, kwargs.pop('diff_blur_img_fn', 'diff2_img.jpg'))) # Estimate the pixels that are the laser by # finding the row in each column with maximum brightness. width, height = size = diff_img.size x = diff_img.convert('L') if save_images_dir: # If saving a result image, create an empty black image which we'll later map # the laser line into. out1 = Image.new("L", x.size, "black") pix1 = out1.load() out2 = Image.new("L", x.size, "black") pix2 = out2.load() out3 = Image.new("L", x.size, "black") pix3 = out3.load() y = np.asarray(x.getdata(), dtype=np.float64).reshape((x.size[1], x.size[0])) laser_measurements = [0]*width # [row] laser_brightness = [0]*width # [brightness] for col_i in xrange(y.shape[1]): col_max = max([(y[row_i][col_i], row_i) for row_i in xrange(y.shape[0])]) col_max_brightness, col_max_row = col_max laser_measurements[col_i] = col_max_row laser_brightness[col_i] = col_max_brightness # Ignore all columns with dim brightness outliers. # These usually indicate a region where the laser is absorbed or otherwise scattered # too much to see. if self.filter_outliers: brightness_std = np.std(laser_brightness) brightness_mean = np.mean(laser_brightness) outlier_level = brightness_mean - brightness_std * self.outlier_filter_threshold final_measurements = [-1]*width # [brightest row] for col_i, col_max_row in enumerate(laser_measurements): if save_images_dir: pix1[col_i, col_max_row] = 255 if not self.filter_outliers \ or (self.filter_outliers and laser_brightness[col_i] > outlier_level): if save_images_dir: pix2[col_i, col_max_row] = 255 # Assuming the laser is mounted below the camera, # we can assume all points above the centerline are noise. if self.laser_position == BOTTOM and col_max_row < height/2: continue elif self.laser_position == TOP and col_max_row > height/2: continue if not self.filter_outliers \ or (self.filter_outliers and laser_brightness[col_i] > outlier_level): if save_images_dir: pix3[col_i, col_max_row] = 255 final_measurements[col_i] = col_max_row if save_images_dir: out1.save(os.path.join(save_images_dir, kwargs.pop('line_img1_fn', 'line1.jpg'))) out2.save(os.path.join(save_images_dir, kwargs.pop('line_img2_fn', 'line2.jpg'))) out3.save(os.path.join(save_images_dir, kwargs.pop('line_img3_fn', 'line3.jpg'))) # If directed, return raw pixels from center instead of distance calculation. if as_pfc: return final_measurements # Convert the pixel measurements to distance. D_lst = pixels_to_distance( pixel_rows=final_measurements, rpc=self.rpc, ro=self.ro, h=self.h, max_height=height, max_width=width, ) return D_lst
im1 = Image.open( "/Users/jonas.palacionis/Desktop/Image Processing/Images/parrot.png") im2 = Image.open( "/Users/jonas.palacionis/Desktop/Image Processing/Images/hill.png" ).convert('RGB').resize((im1.width, im1.height)) multiply(im1, im2).show() #adding two images add(im1, im2).show() #computing the difference between two images im1 = Image.open( "/Users/jonas.palacionis/Desktop/Image Processing/Images/goal1.png") im2 = Image.open( "/Users/jonas.palacionis/Desktop/Image Processing/Images/goal2.png") im = difference(im1, im2) im.show() im.save( "/Users/jonas.palacionis/Desktop/Image Processing/Images/difference_goal.png" ) plt.subplot(311) plt.imshow(im1) plt.axis('off') plt.subplot(312) plt.imshow(im2) plt.axis('off') plt.subplot(313) plt.imshow(im), plt.axis('off') plt.show() im.show()
""" im = im.convert('RGBA') #im = im.convert('RGB') data = np.array(im) # "data" is a height x width x 4 numpy array red, green, blue, alpha = data.T # Temporarily unpack the bands for readability #red, green, blue = data.T # Temporarily unpack the bands for readability # Replace all non-red areas with black #red_areas = red#(red < blue) & (red < green) #data[..., :-1][red_areas.T] = (255, 0, 0) # Transpose back needed #data[..., :-1][red_areas.T] = (0, 0, 0) # Transpose back needed im2 = Image.fromarray(red.T) return im2 laser_off = Image.open(os.path.expanduser('~/Desktop/laser-off.jpg')) laser_on = Image.open(os.path.expanduser('~/Desktop/laser-on.jpg')) laser_off_red = only_red(laser_off) laser_off_red.save(os.path.expanduser('~/Desktop/laser-off-red.jpg')) laser_on_red = only_red(laser_on) laser_on_red.save(os.path.expanduser('~/Desktop/laser-on-red.jpg')) laser_diff = difference(laser_off, laser_on) laser_diff.save(os.path.expanduser('~/Desktop/laser-diff.jpg')) laser_diff_red = difference(laser_off_red, laser_on_red) laser_diff_red.save(os.path.expanduser('~/Desktop/laser-diff-red.jpg'))