def test_sanity(self): im = hopper("L") ImageChops.constant(im, 128) ImageChops.duplicate(im) ImageChops.invert(im) ImageChops.lighter(im, im) ImageChops.darker(im, im) ImageChops.difference(im, im) ImageChops.multiply(im, im) ImageChops.screen(im, im) ImageChops.add(im, im) ImageChops.add(im, im, 2.0) ImageChops.add(im, im, 2.0, 128) ImageChops.subtract(im, im) ImageChops.subtract(im, im, 2.0) ImageChops.subtract(im, im, 2.0, 128) ImageChops.add_modulo(im, im) ImageChops.subtract_modulo(im, im) ImageChops.blend(im, im, 0.5) ImageChops.composite(im, im, im) ImageChops.offset(im, 10) ImageChops.offset(im, 10, 20)
def test_add_modulo_no_clip(): # Arrange im = hopper() # Act new = ImageChops.add_modulo(im, im) # Assert assert new.getpixel((50, 50)) == (224, 76, 254)
def test_add_modulo_no_clip(self): # Arrange im = hopper() # Act new = ImageChops.add_modulo(im, im) # Assert self.assertEqual(new.getpixel((50, 50)), (224, 76, 254))
def handle_image_pixel(): img1 = Image.open('avatar.jpeg') img2 = Image.open('scenery.jpeg') # 叠加 add(image1, image2, scale=1.0, offset=0)[source] :: out = ((image1 + image2) / scale + offset) ops_img = ImageChops.add(img1, img2, scale=2, offset=100) ops_img.save('chops_add.jpg') # .add_modulo(image1, image2)[source] #:: out = ((image1 + image2) % MAX) ops_img = ImageChops.add_modulo(img1, img2) ops_img.save('chops_add_modulo.jpg') # blend(image1, image2, alpha)[source] Alias for PIL.Image.Image.blend(). ops_img = ImageChops.blend(img1, img2.resize(img1.size), 0.5) ops_img.save('chops_blend.jpg') # composite(image1, image2, mask)[source] : Alias for PIL.Image.Image.composite(). # A mask image. This image can have mode "1", "L", or "RGBA", and must have the same size as the other two images ops_img = ImageChops.composite(img1, img2, Image.new('L', img1.size)) ops_img.save('chops_composite.jpg') # .constant(image, value)[source] # Fill a channel with a given grey level. ops_img = ImageChops.constant(img1, 200) ops_img.save('chops_constant.jpg') # darker(image1, image2)[source] :: # out = min(image1, image2) ops_img = ImageChops.darker(img1, img2) ops_img.save('chops_darker.jpg') # difference(image1, image2)[source] # out = abs(image1 - image2) ops_img = ImageChops.difference(img1, img2) ops_img.save('chops_difference.jpg') # duplicate(image)[source] Alias for PIL.Image.Image.copy(). # PIL.ImageChops.invert(image)[source] # out = MAX - image # PIL.ImageChops.lighter(image1, image2)[source] out = max(image1, image2) # PIL.ImageChops.logical_and(image1, image2)[source] # Logical AND between two images. # out = ((image1 and image2) % MAX) # PIL.ImageChops.logical_or(image1, image2)[source] # Logical OR between two images. # out = ((image1 or image2) % MAX) # PIL.ImageChops.multiply(image1, image2)[source] # Superimposes two images on top of each other. # out = image1 * image2 / MAX # If you multiply an image with a solid black image, the result is black. If you multiply with a solid white image, the image is unaffected. # PIL.ImageChops.offset(image, xoffset, yoffset=None)[source] # Returns a copy of the image where data has been offset by the given distances. # Data wraps around the edges. If yoffset is omitted, it is assumed to be equal to xoffset. # 参数: # xoffset – The horizontal distance. # yoffset – The vertical distance. If omitted, both distances are set to the same value. # PIL.ImageChops.screen(image1, image2)[source] #:: out = MAX - ((MAX - image1) * (MAX - image2) / MAX) # Superimposes two inverted images on top of each other. ops_img = ImageChops.screen(img1, img2) ops_img.save('chops_screen.jpg') # PIL.ImageChops.subtract(image1, image2, scale=1.0, offset=0)[source] #:: out = ((image1 - image2) / scale + offset) # Subtracts two images, dividing the result by scale and adding the offset. If omitted, scale defaults to 1.0, and offset to 0.0. ops_img = ImageChops.subtract(img1, img2) ops_img.save('chops_subtract.jpg') # PIL.ImageChops.subtract_modulo(image1, image2)[source] # out:: = ((image1 - image2) % MAX) # Subtract two images, without clipping the result. ops_img = ImageChops.subtract_modulo(img1, img2) ops_img.save('chops_subtract_modulo.jpg')
def test_add_modulo(): # Arrange with Image.open("Tests/images/imagedraw_ellipse_RGB.png") as im1: with Image.open("Tests/images/imagedraw_floodfill_RGB.png") as im2: # Act new = ImageChops.add_modulo(im1, im2) # Assert assert new.getbbox() == (25, 25, 76, 76) assert new.getpixel((50, 50)) == ORANGE
def test_add_modulo(self): # Arrange im1 = Image.open("Tests/images/imagedraw_ellipse_RGB.png") im2 = Image.open("Tests/images/imagedraw_floodfill.png") # Act new = ImageChops.add_modulo(im1, im2) # Assert self.assertEqual(new.getbbox(), (25, 25, 76, 76)) self.assertEqual(new.getpixel((50, 50)), ORANGE)
def test_add_modulo(self): # Arrange im1 = Image.open("Tests/images/imagedraw_ellipse_RGB.png") im2 = Image.open("Tests/images/imagedraw_floodfill_RGB.png") # Act new = ImageChops.add_modulo(im1, im2) # Assert self.assertEqual(new.getbbox(), (25, 25, 76, 76)) self.assertEqual(new.getpixel((50, 50)), ORANGE)
def imageadd(): img11 = Image.open("image.jpg") img12 = Image.open("u.jpg") img11.show() img12.show() img111 = ImageChops.add_modulo(img11, img12) img101 = ImageChops.add(img11, img12, 4, 8) img111.show() img101.show() img111.save("clip_image.png") img101.save("wcliping_image.png")
def process_images(img1, img2): """ img1, img2: input images in Image format return: value representing how much the 2 images look similar """ # get the size of original the image img_dim = img1.getbbox() # invert the bit of the pixels img1_inv = ImageChops.invert(img1) img2_inv = ImageChops.invert(img2) # get the object from the 2 images box1 = img1_inv.getbbox() box2 = img2_inv.getbbox() # crop the object from the images img1_obj = img1_inv.crop(box1) img2_obj = img2_inv.crop(box2) # resize the 2nd image according to the object of the 1st image img2_obj = img2_obj.resize((box1[2] - box1[0], box1[3] - box1[1])) # enhance the simplicity of the objects (take an array and return an array) obj1_simple = enhance_contrast_percentile(np.array(img1_obj), disk(3), p0=.8, p1=.9) obj2_simple = enhance_contrast_percentile(np.array(img2_obj), disk(3), p0=.8, p1=.9) # convert the objects from array to image img1_obj = Image.fromarray(obj1_simple) img2_obj = Image.fromarray(obj2_simple) # invert the bit of the pixels of the 2nd image img2_obj = ImageChops.invert(img2_obj) # generate a new image representing the one inside the other img_diff = ImageChops.invert(ImageChops.add_modulo(img1_obj, img2_obj)) # resize the new image into the old sizes img_diff = img_diff.resize( (img_dim[2] - img_dim[0], img_dim[3] - img_dim[1])) return np.count_nonzero(np.array(img_diff))
def NodeEvaluation(self, eval_info): image1 = eval_info.EvaluateParameter('Image') image2 = eval_info.EvaluateParameter('Overlay') blendmode = eval_info.EvaluateProperty('Blend Mode') image = api.RenderImage() main_image = image1.GetImage() layer_image = ImageOps.fit(image2.GetImage(), main_image.size) if blendmode == 'Add': img = ImageChops.add(main_image, layer_image) elif blendmode == 'Add Modulo': img = ImageChops.add_modulo(main_image, layer_image) elif blendmode == 'Subtract': img = ImageChops.subtract(main_image, layer_image) elif blendmode == 'Subtract Modulo': img = ImageChops.subtract_modulo(main_image, layer_image) elif blendmode == 'Multiply': img = ImageChops.multiply(main_image, layer_image) elif blendmode == 'Screen': img = ImageChops.screen(main_image, layer_image) elif blendmode == 'Difference': img = ImageChops.difference(main_image, layer_image) elif blendmode == 'Darker': img = ImageChops.darker(main_image, layer_image) elif blendmode == 'Lighter': img = ImageChops.lighter(main_image, layer_image) elif blendmode == 'Soft Light': img = ImageChops.soft_light(main_image, layer_image) elif blendmode == 'Hard Light': img = ImageChops.hard_light(main_image, layer_image) elif blendmode == 'Overlay': img = ImageChops.overlay(main_image, layer_image) image.SetAsImage(img) self.NodeSetThumb(image.GetImage()) return image
def write_matrix(self, pin_list): if self.update_skip != 0: self.update_skip -= 1 if self.update_skip >= 0: return self.led.all_off() if self.p_type == 'SBARS': for y in range(self.led_config.matrix_width): y_ind = int( ((self.last + 1.0) / self.led_config.matrix_width) * y) for x_cord in range( int(pin_list[y_ind] * float(self.led_config.matrix_height))): rgb = color_map[int(255.0 * float(x_cord) / float(self.led_config.matrix_height))] self.led.set(x_cord, y_ind, rgb) if self.p_type == 'MBARS': norm_arr = [int(x * 255) for x in pin_list] self.drops.append(norm_arr) for y_cord in range(self.led_config.matrix_height): for x in range(self.led_config.matrix_width): x_ind = int( ((self.last + 1.0) / self.led_config.matrix_width) * x) if self.drops[y_cord][x_ind] > 64: rgb = scale(color_map[255 - self.drops[y_cord][x_ind]], int(self.drops[y_cord][x_ind] * 0.5)) self.led.set(x, y_cord, rgb) del self.drops[0] elif self.p_type == 'IMAGE': complete_image = Image.new("RGBA", self.images[0].size) for pin in xrange(len(pin_list)): if pin_list[pin] > 0.25: complete_image = ImageChops.add_modulo( complete_image, ImageEnhance.Brightness(self.images[pin]).enhance( pin_list[pin])) image.showImage( self.led, "", ImageEnhance.Brightness(complete_image).enhance( self.max_brightness * 0.5)) self.led.update() self.update_skip = self.skip
def difference(image1, image2): # features not in loaded diff1 = ImageChops.subtract(image1, image2) diff1 = ImageChops.invert(diff1) diff1 = ImageOps.colorize( diff1.convert("L"), black="green", white="white", ) # elements in loaded, but not in original diff2 = ImageChops.subtract(image2, image1) diff2 = ImageChops.invert(diff2) diff2 = ImageOps.colorize(diff2.convert("L"), black="red", white="white") diff = ImageChops.add_modulo(diff1, diff2) # diff1.show() # diff2.show() # diff.show() return diff
from PIL import ImageChops # Currently supported modes include: # add_modulo, darker, difference, lighter, logical_and, logical_or, multiple, screen, subtract_modulo op = config.get('operation') if op == 'add_modulo': output_image = ImageChops.add_modulo(image1, image2) elif op == 'darker': output_image = ImageChops.darker(image1, image2) elif op == 'difference': output_image = ImageChops.difference(image1, image2) elif op == 'lighter': output_image = ImageChops.lighter(image1, image2) elif op == 'logical_and': output_image = ImageChops.logical_and(image1, image2) elif op == 'logical_or': output_image = ImageChops.logical_or(image1, image2) elif op == 'multiply': output_image = ImageChops.multiply(image1, image2) elif op == 'screen': output_image = ImageChops.screen(image1, image2)
from PIL import Image, ImageChops if __name__ == '__main__': image = Image.open('../lenna.png') image.show('Original') # Array size is inverted image size shape = (image.size[1], image.size[0], 3) # Create an image using numpy # Generate an array of ones and multiply all values by 100 # This creates a grey-coloured image (all pixel values are (100, 100, 100)) second_image_array = np.ones(shape, dtype=np.uint8) * 100 second_image = Image.fromarray(second_image_array) # ImageChops.add clips the pixel intensity if it is above 255 # E.g. image 1 has a pixel with intensity 200 # image 2 has a pixel with intensity 100 # 100 + 200 = 300 # However images are represented by 8-bit unsigned integers with numbers # ranging [0-255], so the values cap at 255 and 100 + 200 = 255, not 300 add_image = ImageChops.add(image, second_image) add_image.show('Add') # ImageChops.add_modulo on the other side does not clip the result # Using the example above it does the following: # (100 + 200) = x mod 256 or (100 + 200) % 256 # So in our case the new value will be: (100 + 200) % 256 = 44 add_modulo_image = ImageChops.add_modulo(image, second_image) add_modulo_image.show('Add modulo')
def pairs(image1, image2, mode, **kwargs): """ la.image.PairsMode.Add Adds two images, dividing the result by scale and adding the offset. If omitted, scale defaults to 1.0, and offset to 0.0. out = ((image1 + image2) / scale + offset) you should append param `scale` and `offset` scale: int or float, defaults to 1.0 offset: int or float, defaults to 0 eg. la.image.pairs(image1, image2, mode=la.image.PairsMode.Add, scale=1.0, offset=0) la.image.PairsMode.Add_modulo Add two images, without clipping the result. out = ((image1 + image2) % 255) eg. la.image.pairs(image1, image2, mode=la.image.PairsMode.Add_modulo) la.image.PairsMode.Blend Creates a new image by interpolating between two input images. using a constant alpha. out = image1 * (1.0 - alpha) + image2 * alpha If alpha is 0.0, a copy of the first image is returned. If alpha is 1.0, a copy of the second image is returned. There are no restrictions on the alpha value. If necessary, the result is clipped to fit into the allowed output range. you should append param `alpha` alpha: The interpolation alpha factor. eg. la.image.pairs(image1, image2, mode=la.image.PairsMode.Blend, alpha=0.2) la.image.PairsMode.Composite Create composite image by blending images using a transparency mask. you should append param `mask` mask: A mask image. This image can have mode “1”, “L”, or “RGBA”, and must have the same size as the other two images. eg. la.image.pairs(image1, image2, la.image.PairsMode.Composite, mask) la.image.PairsMode.Darker Compares the two images, pixel by pixel, and returns a new image containing the darker values. out = min(image1, image2) eg. la.image.pairs(image1, image2, mode=la.image.PairsMode.Darker) la.image.PairsMode.Difference Returns the absolute value of the pixel-by-pixel difference between the two images. out = abs(image1 - image2) eg. la.image.pairs(image1, image2, mode=la.image.PairsMode.Difference) la.image.PairsMode.Lighter Compares the two images, pixel by pixel, and returns a new image containing the lighter values. out = max(image1, image2) eg. la.image.pairs(image1, image2, mode=la.image.PairsMode.Lighter) la.image.PairsMode.Logical_and Logical AND between two images. Both of the images must have mode “1”. If you would like to perform a logical AND on an image with a mode other than “1”, try multiply() instead, using a black-and-white mask as the second image. out = ((image1 and image2) % 255) eg. la.image.pairs(image1, image2, mode=la.image.PairsMode.Logical_and) la.image.PairsMode.Logical_or Logical OR between two images. Both of the images must have mode “1”. out = ((image1 or image2) % 255) eg. la.image.pairs(image1, image2, mode=la.image.PairsMode.Logical_or) la.image.PairsMode.Logical_xor Logical XOR between two images. Both of the images must have mode “1”. out = ((bool(image1) != bool(image2)) % 255) eg. la.image.pairs(image1, image2, mode=la.image.PairsMode.Logical_xor) la.image.PairsMode.multiply Superimposes two images on top of each other. If you multiply an image with a solid black image, the result is black. If you multiply with a solid white image, the image is unaffected. out = image1 * image2 / 255 eg. la.image.pairs(image1, image2, mode=la.image.PairsMode.multiply) la.image.PairsMode.SoftLight Superimposes two images on top of each other using the Soft Light algorithm eg. la.image.pairs(image1, image2, mode=la.image.PairsMode.SoftLight) la.image.PairsMode.HardLight Superimposes two images on top of each other using the Hard Light algorithm eg. la.image.pairs(image1, image2, mode=la.image.PairsMode.HardLight) la.image.PairsMode.Overlay Superimposes two images on top of each other using the Overlay algorithm eg. la.image.pairs(image1, image2, mode=la.image.PairsMode.Overlay) la.image.PairsMode.Screen Superimposes two inverted images on top of each other. out = 255 - ((255 - image1) * (255 - image2) / 255) eg. la.image.pairs(image1, image2, mode=la.image.PairsMode.Screen) la.image.PairsMode.Subtract Subtracts two images, dividing the result by scale and adding the offset. If omitted, scale defaults to 1.0, and offset to 0.0. out = ((image1 - image2) / scale + offset) you should append param `scale` and `offset` scale: int or float, defaults to 1.0 offset: int or float, defaults to 0 eg. la.image.pairs(image1, image2, mode=la.image.PairsMode.Subtract, scale=1.0, offset=0) la.image.PairsMode.Subtract_modulo Subtract two images, without clipping the result. out = ((image1 - image2) % 255) eg. la.image.pairs(image1, image2, mode=la.image.PairsMode.Subtract_modulo) Args: image1: a PIL instance. The first image. image2: a PIL instance. The second image. Must have the same mode and size as the first image. mode: la.image.PairsMode Return: a PIL instance. """ if 'scale' not in kwargs: kwargs['scale'] = 1. if 'offset' not in kwargs: kwargs['offset'] = 0 if mode == 'add': return ImageChops.add(image1, image2, scale=kwargs['scale'], offset=kwargs['offset']) elif mode == 'add_modulo': return ImageChops.add_modulo(image1, image2) elif mode == 'blend': if 'alpha' not in kwargs: raise ValueError("Missing parameter `alpha`") return ImageChops.blend(image1, image2, alpha=kwargs['alpha']) elif mode == 'composite': if 'mask' not in kwargs: raise ValueError("Missing parameter `mask`") return ImageChops.composite(image1, image2, mask=kwargs['mask']) elif mode == 'darker': return ImageChops.darker(image1, image2) elif mode == 'difference': return ImageChops.difference(image1, image2) elif mode == 'lighter': return ImageChops.lighter(image1, image2) elif mode == 'logical_and': return ImageChops.logical_and(image1, image2) elif mode == 'logical_or': return ImageChops.logical_or(image1, image2) elif mode == 'logical_xor': return ImageChops.logical_xor(image1, image2) elif mode == 'multiply': return ImageChops.multiply(image1, image2) elif mode == 'soft_light': return ImageChops.soft_light(image1, image2) elif mode == 'hard_light': return ImageChops.hard_light(image1, image2) elif mode == 'overlay': return ImageChops.overlay(image1, image2) elif mode == 'screen': return ImageChops.screen(image1, image2) elif mode == 'subtract': return ImageChops.subtract(image1, image2, scale=kwargs['scale'], offset=kwargs['offset']) elif mode == 'subtract_modulo': return ImageChops.subtract_modulo(image1, image2) else: raise ValueError("mode must be la.image.PairsMode param")
def blend(image, blendMode, overImage, position=(0, 0), resize=True): """ Blend two images with the given blend mode image - the image to be pasted on top (if None, returns overImage) blendMode - the mode to use when combining images overImage - the image will be pasted over the top of this image (if None, returns image) position - the position to place the new image, relative to overImage (can be negative) resize - allow the resulting image to be resized if overImage extends beyond its bounds Most comprehensive list: https://docs.krita.org/Blending_Modes Excellent description of blend modes: http://emptyeasel.com/2008/10/31/explaining-blending-modes-in-photoshop-and-gimp-multiply-divide-overlay-screen/ How they're implemented in gimp: https://www.linuxtopia.org/online_books/graphics_tools/gimp_advanced_guide/gimp_guide_node55_002.html For another python implementation (not all supported): https://github.com/flrs/blend_modes/blob/master/blend_modes/blend_modes.py#L138 IMPORTANT: the image bits may be altered. To prevent this, set image.immutable=True """ def _normal(bottom, top): return top def _multiply(bottom, top): return bottom * top def _screen(bottom, top): return bottom + top - (bottom * top) def _dissolve(bottom, top): # TODO: there is a bug. instead of randomly merging pixels, it randomly merges color values rand = np.random.random(bottom.shape) return np.where(rand > 0.5, bottom, top) def _darken(bottom, top): return np.minimum(bottom, top) def _colorBurn(bottom, top): # NOTE: gimp "burn" is a colorBurn, not a linearBurn return 1.0 - ((1.0 - top) / bottom) def _linearBurn(bottom, top): return bottom + top - 1.0 def _lighten(bottom, top): return np.maximum(bottom, top) def _colorDodge(bottom, top): # NOTE: gimp "dodge" is a colorDodge, not a linearDodge return top / (1.0 - bottom) def _linearDodge(bottom, top): return top + bottom def _overlay(bottom, top): # TODO: the colors saturation comes out a little higher than gimp, but close return np.where(top <= 0.5, 2.0 * bottom * top, 1.0 - 2.0 * (1.0 - bottom) * (1.0 - top)) def _hardOverlay(bottom, top): # this is a krita thing return np.where(bottom > 0.5, _multiply(top, 2.0 * bottom), _divide(top, 2.0 * bottom - 1.0)) def _hardLight(bottom, top): return np.where(bottom <= 0.5, _multiply(top, 2.0 * bottom), _screen(top, 2.0 * bottom - 1.0)) def _vividLight(bottom, top): return np.where(top > 0.5, _colorDodge(bottom, top), _colorBurn(bottom, top)) def _linearLight(bottom, top): return np.where(top > 0.5, _linearDodge(bottom, top), _linearBurn(bottom, top)) def _pinLight(bottom, top): # NOTE: I think this is right...? return np.where(bottom > 0.5, _darken(bottom, top), _lighten(bottom, top)) def _hardMix(bottom, top): # NOTE: I think this is right...? return np.where(bottom > 0.5, 1.0, 0.0) def _difference(bottom, top): return np.abs(bottom - top) def _softLight(bottom, top): # NOTE: strangely both algos do the same thing, but look different than gimp #\tyet the last algo is deliberately backwards, and that's what gimp does. Is gimp backwards?? #def D(x): #\treturn np.where(x<=0.25,((16*x-12)*x+4)*x,np.sqrt(x)) #return np.where(top<=0.5,bottom-(1.0-2.0*top)*bottom*(1.0-bottom),bottom+(2.0*top-1)*(D(bottom)-bottom)) #return (1.0-bottom)*bottom*top+bottom*(1.0-(1.0-bottom)*(1.0-top)) return (1.0 - top) * top * bottom + top * (1.0 - (1.0 - top) * (1.0 - bottom)) def _exclusion(bottom, top): return bottom + top - 2.0 * bottom * top def _subtract(bottom, top): # NOTE: You'd think the first algo would be correct, but gimp has it the opposite way #return bottom-top return top - bottom def _grainExtract(bottom, top): # NOTE: You'd think the first algo would be correct, but gimp has it the opposite way #return bottom-top+0.5 return top - bottom + 0.5 def _grainMerge(bottom, top): return bottom + top - 0.5 def _divide(bottom, top): # NOTE: You'd think the first algo would be correct, but gimp has it the opposite way #return bottom/top return top / bottom def _hue(bottom, top): bottomH = rgb2hsvArray(bottom) topH = rgb2hsvArray(top) return hsv2rgbArray(np.dstack((bottomH[:, :, 0], topH[:, :, 1:]))) def _saturation(bottom, top): bottomH = rgb2hsvArray(bottom) topH = rgb2hsvArray(top) return hsv2rgbArray( np.dstack((topH[:, :, 0], bottomH[:, :, 1], topH[:, :, 2]))) def _value(bottom, top): bottomH = rgb2hsvArray(bottom) topH = rgb2hsvArray(top) return hsv2rgbArray(np.dstack((topH[:, :, :2], bottomH[:, :, 2]))) def _color(bottom, top): # TODO: Very close, but seems to lose some blue values compared to gimp bottomH = rgb2hsvArray(bottom) topH = rgb2hsvArray(top) return hsv2rgbArray(np.dstack((bottomH[:, :, :2], topH[:, :, 2]))) #------------------------------------ ret = None if blendMode == 'normal': ret = paste(image, overImage, position, resize) #ret=_blendArray(image,overImage,_normal) elif blendMode == 'dissolve': ret = _blendArray(image, overImage, _dissolve) elif blendMode == 'behind': # this is a brush only blend mode, so not implemented # what it does is paint on just alpha raise NotImplementedError() elif blendMode == 'clear': # this is a brush only blend mode, so not implemented # what it does is paint alpha on solid areas (aka, erase) raise NotImplementedError() elif blendMode in ('darken', 'darkenOnly'): ret = _blendArray(image, overImage, _darken) elif blendMode == 'multiply': #ret=ImageChops.multiply(image,overImage) ret = _blendArray(image, overImage, _multiply) elif blendMode in ('colorBurn', 'burn'): ret = _blendArray(image, overImage, _colorBurn) elif blendMode == 'linearBurn': ret = _blendArray(image, overImage, _linearBurn) elif blendMode in ('lighten', 'lightenOnly'): ret = _blendArray(image, overImage, _lighten) elif blendMode == 'screen': #ret=ImageChops.screen(image,overImage) ret = _blendArray(image, overImage, _screen) elif blendMode in ('colorDodge', 'dodge'): ret = _blendArray(image, overImage, _colorDodge) elif blendMode in ('linearDodge', 'add'): #ret=ImageChops.add(image,overImage) ret = _blendArray(image, overImage, _linearDodge) elif blendMode == 'addMod': ret = ImageChops.add_modulo(image, overImage) elif blendMode == 'overlay': ret = _blendArray(image, overImage, _overlay) elif blendMode == 'hardOverlay': ret = _blendArray(image, overImage, _hardOverlay) elif blendMode == 'softLight': ret = _blendArray(image, overImage, _softLight) elif blendMode == 'hardLight': ret = _blendArray(image, overImage, _hardLight) elif blendMode == 'vividLight': ret = _blendArray(image, overImage, _vividLight) elif blendMode == 'linearLight': ret = _blendArray(image, overImage, _linearLight) elif blendMode == 'pinLight': ret = _blendArray(image, overImage, _pinLight) elif blendMode == 'hardMix': ret = _blendArray(image, overImage, _hardMix) elif blendMode == 'difference': #ret=ImageChops.difference(image,overImage) ret = _blendArray(image, overImage, _difference) elif blendMode == 'exclusion': ret = _blendArray(image, overImage, _exclusion) elif blendMode == 'subtract': #ret=ImageChops.subtract(image,overImage) ret = _blendArray(image, overImage, _subtract) elif blendMode == 'subtractMod': ret = ImageChops.subtract_modulo(image, overImage) elif blendMode == 'divide': ret = _blendArray(image, overImage, _divide) elif blendMode == 'hue': ret = _blendArray(image, overImage, _hue) elif blendMode == 'saturation': ret = _blendArray(image, overImage, _saturation) elif blendMode == 'color': ret = _blendArray(image, overImage, _color) elif blendMode in ('luminosity', 'value'): ret = _blendArray(image, overImage, _value) elif blendMode in ('lighterColor', 'lighter'): ret = ImageChops.lighter(image, overImage) elif blendMode in ('darkerColor', 'darker'): ret = ImageChops.darker(image, overImage) elif blendMode == 'and': ret = ImageChops.logical_and(image, overImage) elif blendMode == 'or': ret = ImageChops.logical_or(image, overImage) elif blendMode == 'grainExtract': ret = _blendArray(image, overImage, _grainExtract) elif blendMode == 'grainMerge': ret = _blendArray(image, overImage, _grainMerge) else: raise Exception('ERR: Unknown blend mode "' + blendMode + '"') return ret
def imshow(tensor, imsize=512, title=None): image = tensor.clone().cpu() image = image.view(*tensor.size()) image = transforms.ToPILImage()(image).convert('L') return image image_folder = "blocks/output4" starting_index = 15 starting_stage = starting_index + 1 transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5, ), (0.5, )), ]) # read normalized delta state files delta_s = torch.load(image_folder + '/delta_stage_15').to('cpu').detach() # delta_s = delta_s.numpy() img_delta = imshow(delta_s, imsize=240) # image_delta = dest_img - src_img img_delta.show() input("Press Enter to continue...") src_img = Image.open('./' + image_folder + '/new_' + str(starting_stage) + '.jpg').convert('L') # dest_img = ImageChops.add_modulo(img_delta, src_img) dest_img.show()
def write_matrix(self, pin_list): if self.update_skip != 0: self.update_skip -= 1 if self.update_skip >= 0: return if len(self.led_config.matrix_pattern_type) == 1: self.p_type = self.led_config.matrix_pattern_type[0] else: for pin in xrange(len(pin_list)): self.beats += pin_list[pin] * (len(pin_list) / (pin + 1)) * 0.002 if self.beats > self.led_config.beats: self.beats = 0 self.p_num += 1 if self.p_num >= len(self.led_config.matrix_pattern_type): self.p_num = 0 self.p_type = self.led_config.matrix_pattern_type[self.p_num] self.led.all_off() h = self.led_config.matrix_height w = self.led_config.matrix_width if self.p_type == 'SBARS': for y in range(h): y_ind = int((float(len(pin_list)) / h) * y) for x_cord in range(int(pin_list[y_ind] * float(w))): rgb = color_map[int(255.0 * float(x_cord) / float(w))] self.led.set(y_ind, x_cord, rgb) if self.p_type == 'MBARS': norm_arr = [int(x * 255) for x in pin_list] self.drops.append(norm_arr) for y_cord in range(w): for x in range(h): x_ind = int((float(len(pin_list)) / h) * x) if self.drops[y_cord][x_ind] > 64: rgb = scale(color_map[255 - self.drops[y_cord][x_ind]], int(self.drops[y_cord][x_ind] * 0.5)) self.led.set(x, w - 1 - y_cord, rgb) del self.drops[0] elif self.p_type == 'IMAGE': complete_image = self.base_image for pin in xrange(len(pin_list)): if pin_list[pin] > 0.55: complete_image = ImageChops.add_modulo( complete_image, ImageEnhance.Brightness(self.images[pin]).enhance( pin_list[pin])) image.showImage( self.led, "", ImageEnhance.Brightness(complete_image).enhance( self.max_brightness * 0.5)) elif self.p_type == 'PINWHEEL': amt = 0 for pin in xrange(len(pin_list)): amt += pin_list[pin] * (len(pin_list) / (pin + 1)) * 0.25 amt = int(amt) pos = 0 for x in range(h): c = colors.hue_helper(pos, self._len, self._step) self.led.drawLine(self._cX, self._cY, x, 0, c) pos += 1 for y in range(w): c = colors.hue_helper(pos, self._len, self._step) self.led.drawLine(self._cX, self._cY, h - 1, y, c) pos += 1 for x in range(h - 1, -1, -1): c = colors.hue_helper(pos, self._len, self._step) self.led.drawLine(self._cX, self._cY, x, w - 1, c) pos += 1 for y in range(w - 1, -1, -1): c = colors.hue_helper(pos, self._len, self._step) self.led.drawLine(self._cX, self._cY, 0, y, c) pos += 1 self._step += amt if (self._step >= 255): self._step = 0 elif self.p_type == 'CBARS': midl = int(h / 2) for y in range(w): level = pin_list[int( (y / float(w)) * float(self.led_config.led_channel_count))] brightness = int(255 * level) rgb = scale(color_map[brightness], brightness) mlvl = int(level * midl) self.led.drawLine(midl - mlvl, y, midl + mlvl, y, rgb) elif self.p_type == 'CIRCLES': for pin in xrange(len(pin_list)): rgb = self.rgb[pin] c = scale(rgb, int((pin_list[pin]) * 255)) self.led.drawCircle(self._cX, self._cY, pin, c) self.led.update() self.update_skip = self.skip
imgtaken.set_alpha(100) gameDisplay.blit(imgtaken, (0,0), special_flags=pygame.BLEND_MAX) if pil == True: # made file name for edited image output timenow = time.time() pil_blend = str(timenow)[0:10] pil_blend= "blend_"+str(timenow)+".jpg" # set current and prior photo as loaded pil images pil_p_photo = pil_c_photo pil_c_photo = photo() pil_c_photo = Image.open(pil_c_photo) # set current and prior dif images as loaded pil images pil_p_dif = pil_c_dif pil_c_dif = ImageChops.difference(pil_c_photo, pil_p_photo) # combine prior and current difference images pil_a = ImageChops.add_modulo(pil_p_dif, pil_c_dif) #pil_a = Image.blend(pil_p_dif, pil_c_dif, 0.9) pil_a.save(pil_blend) # open image to display on screen onscreen = pygame.image.load(pil_blend) gameDisplay.blit(onscreen, (0,0)) pygame.display.update() else: imgtaken = photo() imgtaken = pygame.image.load(imgtaken) gameDisplay.blit(imgtaken, (0,0)) #gameDisplay.fill(white) #gameDisplay.blit(imgtaken, (0,0), special_flags=pygame.BLEND_ADD) #special_flags= # BLEND_ADD, BLEND_SUB, BLEND_MULT, BLEND_MIN, BLEND_MAX
def selfmodulo(image): # Testing Imagechops module return ImageChops.add_modulo(image, image)
def write_matrix(self, pin_list): if self.update_skip != 0: self.update_skip -= 1 if self.update_skip >= 0: return if len(self.led_config.matrix_pattern_type) == 1: self.p_type = self.led_config.matrix_pattern_type[0] else: for pin in range(len(pin_list)): self.beats += pin_list[pin] * (len(pin_list) / (pin + 1)) * 0.002 if self.beats > self.led_config.beats and self._bstep == 0: self._bstep = 0 self.beats = 0 self.p_num += 1 if self.p_num >= len(self.led_config.matrix_pattern_type): self.p_num = 0 self.p_type = self.led_config.matrix_pattern_type[self.p_num] self.mmcm(self.p_type) self.led.all_off() h = self.led_config.matrix_height w = self.led_config.matrix_width if self.p_type == 'SBARS': for y in range(h): y_ind = int((float(len(pin_list)) / h) * y) for x_cord in range(int(pin_list[y_ind] * float(w))): rgb = color_map[int(255.0 * float(x_cord) / float(w))] self.led.set(y_ind, x_cord, rgb) if self.p_type == 'MBARS': norm_arr = [int(x * 255) for x in pin_list] self.drops.append(norm_arr) for y_cord in range(w): for x in range(h): x_ind = int((float(len(pin_list)) / h) * x) if self.drops[y_cord][x_ind] > 64: rgb = scale(color_map[255 - self.drops[y_cord][x_ind]], int(self.drops[y_cord][x_ind] * 0.5)) self.led.set(x, w - 1 - y_cord, rgb) del self.drops[0] elif self.p_type == 'IMAGE': complete_image = self.base_image for pin in range(len(pin_list)): if pin_list[pin] > 0.55: complete_image = ImageChops.add_modulo( complete_image, ImageEnhance.Brightness(self.images[pin]).enhance( pin_list[pin])) image.showImage( self.led, "", ImageEnhance.Brightness(complete_image).enhance( self.max_brightness * 0.5)) elif self.p_type == 'PINWHEEL': amt = 0 for pin in range(len(pin_list)): amt += pin_list[pin] * (len(pin_list) / (pin + 1)) * 0.25 amt = int(amt) pos = 0 for x in range(h): c = colors.hue_helper(pos, self._len, self._step) self.led.drawLine(self.midxa, self.midya, x, 0, c) pos += 1 for y in range(w): c = colors.hue_helper(pos, self._len, self._step) self.led.drawLine(self.midxb, self.midyb, h - 1, y, c) pos += 1 for x in range(h - 1, -1, -1): c = colors.hue_helper(pos, self._len, self._step) self.led.drawLine(self.midxb, self.midyb, x, w - 1, c) pos += 1 for y in range(w - 1, -1, -1): c = colors.hue_helper(pos, self._len, self._step) self.led.drawLine(self.midxa, self.midya, 0, y, c) pos += 1 self._step += amt if (self._step >= 255): self._step = 0 elif self.p_type == 'CBARS': for y in range(w): level = pin_list[int( (y / float(w)) * float(self.led_config.led_channel_count))] brightness = int(255 * level) rgb = scale(color_map[brightness], brightness) mlvl = int(level * self.midxa) self.led.drawLine(self.midxa - mlvl, y, self.midxb + mlvl, y, rgb) elif self.p_type == 'CIRCLES': for pin in range(self.led_config.led_channel_count): rgb = self.rgb[pin] c = scale(rgb, int((pin_list[pin]) * 255)) self.led.drawCircle( self.midxa, self.midyb, int(pin * ((w / 2) / self.led_config.led_channel_count)), c) elif self.p_type == 'BANNER': rgb = self.rgb[list(pin_list).index(max(list(pin_list)))] # fit 4 characters into width text = self.led_config.banner_text[int(self._bstep ):int(self._bstep) + 4] # fonts : 6x4 8x6 16x8 self.led.drawText(text, x=1, y=1, color=rgb, bg=colors.Off, font='6x4', font_scale=1) # scroll speed 0.1 self._bstep += 0.1 # scroll with beats # self._bstep += (pin_list[0] * 0.2) + (pin_list[1] * 0.1) if (self._bstep >= len(self.led_config.banner_text)): self._bstep = 0 self.led.push_to_driver() self.update_skip = self.skip
from PIL import Image, ImageChops im1 = Image.open("/home/pi/DIP/Dataset/4.1.03.tiff") im2 = Image.open("/home/pi/DIP/Dataset/4.1.04.tiff") im3 = ImageChops.add_modulo(im1, im2) im3.show()
imgtaken.set_alpha(100) gameDisplay.blit(imgtaken, (0, 0), special_flags=pygame.BLEND_MAX) if pil == True: # made file name for edited image output timenow = time.time() pil_blend = str(timenow)[0:10] pil_blend = "blend_" + str(timenow) + ".jpg" # set current and prior photo as loaded pil images pil_p_photo = pil_c_photo pil_c_photo = photo() pil_c_photo = Image.open(pil_c_photo) # set current and prior dif images as loaded pil images pil_p_dif = pil_c_dif pil_c_dif = ImageChops.difference(pil_c_photo, pil_p_photo) # combine prior and current difference images pil_a = ImageChops.add_modulo(pil_p_dif, pil_c_dif) #pil_a = Image.blend(pil_p_dif, pil_c_dif, 0.9) pil_a.save(pil_blend) # open image to display on screen onscreen = pygame.image.load(pil_blend) gameDisplay.blit(onscreen, (0, 0)) pygame.display.update() else: imgtaken = photo() imgtaken = pygame.image.load(imgtaken) gameDisplay.blit(imgtaken, (0, 0)) #gameDisplay.fill(white) #gameDisplay.blit(imgtaken, (0,0), special_flags=pygame.BLEND_ADD) #special_flags= # BLEND_ADD, BLEND_SUB, BLEND_MULT, BLEND_MIN, BLEND_MAX