def background(image, fill, mark, color, horizontal_offset=None, vertical_offset=None, horizontal_justification=None, vertical_justification=None, orientation=None, method=None, opacity=100): """color is RGB""" if not has_transparency(image): return image if image.mode == 'P': image = image.convert('RGBA') if fill == FILL_CHOICES[0]: opacity = (255 * opacity) / 100 r, g, b = color return fill_background_color(image, (r, g, b, opacity)) elif fill == FILL_CHOICES[1]: layer = generate_layer(image.size, mark, method, horizontal_offset, vertical_offset, horizontal_justification, vertical_justification, orientation, opacity) paste(layer, image, mask=image) return layer
def AddMask(im, mask, color=(255, 255, 255, 255)): """ ͼƬÃɰ棨mask)Ч¹û ±³¾°É«È±Ê¡ÊÇ°×É« """ im = im.convert("RGBA") background = Image.new('RGBA', im.size, color) # convert mask mode and resize it mask = mask.convert('L') mask = mask.resize(im.size, Image.ANTIALIAS) paste(im, background, (0, 0), ImageChops.invert(mask)) return im
def round_image(image, cache={}, radius = 5, pos=ROUNDED_POS, back_color='#FFFFFF'): # 计算圆角半径 radius = (min(image.size) * radius)/100 if image.mode != 'RGBA': image = image.convert('RGBA') pos = 4 * (ROUNDED, ) mask = create_rounded_rectangle(image.size, cache, radius, pos) paste(image, Image.new('RGB', image.size, back_color), (0, 0), ImageChops.invert(mask)) image.putalpha(mask) return image
def tile(image, direction): """镜像效果""" if image.mode == 'P': image = convert_safe_mode(image) result = Image.new(image.mode, get_dimensions(image, direction)) paste(result, image, (0, 0)) if direction == u'两者皆选': x_mirror(image, result) y_mirror(image, result) xy_mirror(image, result) if direction == u'左右镜像': x_mirror(image, result) if direction == u'上下镜像': y_mirror(image, result) return result
def create_rounded_rectangle(size=(600, 400), cache={}, radius=100, pos=ROUNDED_POS): #rounded_rectangle im_x, im_y = size rounded_rectangle_id = ROUNDED_RECTANGLE_ID % (radius, 255, size, pos) if rounded_rectangle_id in cache: return cache[rounded_rectangle_id] else: #cross cross_id = ROUNDED_RECTANGLE_ID % (radius, 255, size, ROUNDED_POS) if cross_id in cache: cross = cache[cross_id] else: cross = cache[cross_id] = Image.new('L', size, 0) draw = ImageDraw.Draw(cross) draw.rectangle((radius, 0, im_x - radius, im_y), fill=255) draw.rectangle((0, radius, im_x, im_y - radius), fill=255) #corner corner_id = CORNER_ID % (radius, 255) if corner_id in cache: corner = cache[corner_id] else: corner = cache[corner_id] = create_corner(radius, 4) #255) #rounded rectangle rectangle = Image.new('L', (radius, radius), 255) rounded_rectangle = cross.copy() for index, angle in enumerate(pos): if angle == ROUNDED: element = corner else: element = rectangle if index % 2: x = im_x - radius element = element.transpose(Image.FLIP_LEFT_RIGHT) else: x = 0 if index < 2: y = 0 else: y = im_y - radius element = element.transpose(Image.FLIP_TOP_BOTTOM) paste(rounded_rectangle, element, (x, y)) cache[rounded_rectangle_id] = rounded_rectangle return rounded_rectangle
img1 = Image.new('RGBA', img2.size, color='white') mask = Image.open(r'D:\windows\Desktop\Folder keep\FSViewer50\Mask\mask76.jpg') #img1.show() def blend(image1, image2, alpha): image1, image2 = [image.convert('RGBA') for image in [image1, image2]] return Image.blend(image1, image2, alpha) def composite(image1, image2, mask): image1, image2, mask = [ image.convert('RGBA') for image in [image1, image2, mask] ] im_mask = mask.resize(image1.size, Image.ANTIALIAS) #im_mask = Replace() return Image.composite(image1, image2, im_mask) #im = blend(img1, img2, 0.5) #im = composite(img1, img2, mask) img1, img2 = [image.convert('RGBA') for image in [img1, img2]] mask = mask.convert('L') mask = mask.resize(img1.size, Image.ANTIALIAS) paste(img2, Image.new('RGB', img2.size, 'blue'), (0, 0), ImageChops.invert(mask)) #img2.putalpha(mask) img2.show() #im.show()
def xy_mirror(image, result): paste(result, image.transpose(Image.ROTATE_180), image.size)
def y_mirror(image, result): width, height = image.size paste(result, image.transpose(Image.FLIP_TOP_BOTTOM), (0, height))
def x_mirror(image, result): width, height = image.size paste(result, image.transpose(Image.FLIP_LEFT_RIGHT), (width, 0))