Python Image 模块,composite() 实例源码
我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用Image.composite()。
def apply_watermark(im, mark, position, opacity=1):
"""Adds a watermark to an image."""
if opacity < 1:
mark = reduce_opacity(mark, opacity)
if im.mode != 'RGBA':
im = im.convert('RGBA')
# create a transparent layer the size of the image and draw the
# watermark in that layer.
layer = Image.new('RGBA', im.size, (0, 0, 0))
if position == 'tile':
for y in range(0, im.size[1], mark.size[1]):
for x in range(0, im.size[0], mark.size[0]):
layer.paste(mark, (x, y))
elif position == 'scale':
# scale,but preserve the aspect ratio
ratio = min(
float(im.size[0]) / mark.size[0], float(im.size[1]) / mark.size[1])
w = int(mark.size[0] * ratio)
h = int(mark.size[1] * ratio)
mark = mark.resize((w, h))
layer.paste(mark, (round((im.size[0] - w) / 2), round((im.size[1] - h) / 2)))
else:
layer.paste(mark, position)
# composite the watermark with the layer
return Image.composite(layer, im, layer)
def mark(self, path, position=POSITION_BottOM_RIGHT):
'''????????'''
try:
img = Image.open(path)
except IOError:
return None
if img.size[0] < self._mosaic.size[0]:
print 'width', img.size[0], self._mosaic.size[0]
return None
if img.size[1] < self._mosaic.size[1]:
print 'height', img.size[1], self._mosaic.size[1]
return None
img_area = img.size[0] * img.size[1]
mosaic_area = self._mosaic.size[0] * self._mosaic.size[1]
ratio = 4
if img_area < mosaic_area * ratio:
return None
self._locate(img, position)
layer = Image.new('RGBA', img.size, 0))
layer.paste(self._mosaic, self.Box)
return Image.composite(layer, img, layer)
def composite(image1, image2, mask):
"Create composite image by blending images using a transparency mask"
return Image.composite(image1, mask)
##
# Offset image data.
# <p>
# 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.
#
# @param image Source image.
# @param xoffset The horizontal distance.
# @param yoffset The vertical distance. If omitted,both
# distances are set to the same value.
# @return An Image object.
def composite(image1,both
# distances are set to the same value.
# @return An Image object.
def composite(image1,both
# distances are set to the same value.
# @return An Image object.
def draw_text_with_halo(img, text, font, col, halo_col):
halo = Image.new('RGBA', 0))
ImageDraw.Draw(halo).text(position, font = font, fill = halo_col)
blurred_halo = halo.filter(ImageFilter.BLUR)
ImageDraw.Draw(blurred_halo).text(position, fill = col)
return Image.composite(img, blurred_halo, ImageChops.invert(blurred_halo))
def render(self, stream, value):
im = self.im.copy()
im2 = self.im.copy()
x = 0
r_i = sum(ord(c) for c in value) # ????????????????
for c in value:
fgimg = Image.new('RGBA', self.size, self.font_color)
charimg = Image.new('L', self.font.getsize(c), '#000000')
draw = ImageDraw.Draw(charimg)
draw.text((0, 0), c, font=self.font, fill='#ffffff')
r = (int(time()) / 1000 + ord(c) + r_i) % 40 - 20 # ???????????????
charimg = charimg.rotate(r, expand=1, resample=Image.BICUBIC)
charimg = charimg.crop(charimg.getbBox())
maskimg = Image.new('L', self.size)
y = (im2.size[1] - charimg.size[1]) / 2
maskimg.paste(charimg, y, charimg.size[0] + x, charimg.size[1] + y))
im2 = Image.composite(fgimg, im2, maskimg)
x += charimg.size[0] - 5 # - X???
# ??????? x ??
center = (im.size[0] - x) / 2
im.paste(im2, (center, im2.size[0]+center, im2.size[1]))
im.save(stream, self.image_type)
def blend(image1, alpha):
"Blend two images using a constant transparency weight"
return Image.blend(image1, alpha)
##
# Create composite using transparency mask.
# <p>
# Same as the <b>composite</b> function in the <b>Image</b> module.
def blend(image1, alpha)
##
# Create composite using transparency mask.
# <p>
# Same as the <b>composite</b> function in the <b>Image</b> module.
def blend(image1, alpha)
##
# Create composite using transparency mask.
# <p>
# Same as the <b>composite</b> function in the <b>Image</b> module.
def do_composite(self):
"""usage: composite <image:pic1> <image:pic2> <image:mask>
Replace two images and a mask with their composite.
"""
image1 = self.do_pop()
image2 = self.do_pop()
mask = self.do_pop()
self.push(Image.composite(image1, mask))
def add_reflection(im, bgcolor="#00000", amount=0.4, opacity=0.6):
""" Returns the supplied PIL Image (im) with a reflection effect
bgcolor The background color of the reflection gradient
amount The height of the reflection as a percentage of the orignal image
opacity The initial opacity of the reflection gradient
Originally written for the Photologue image management system for Django
and Based on the original concept by Bernd Schlapsi
"""
# convert bgcolor string to rgb value
background_color = ImageColor.getrgb(bgcolor)
# copy orignial image and flip the orientation
reflection = im.copy().transpose(Image.FLIP_TOP_BottOM)
# create a new image filled with the bgcolor the same size
background = Image.new("RGB", background_color)
# calculate our alpha mask
start = int(255 - (255 * opacity)) # The start of our gradient
steps = int(255 * amount) # the number of intermedite values
increment = (255 - start) / float(steps)
mask = Image.new('L', (1, 255))
for y in range(255):
if y < steps:
val = int(y * increment + start)
else:
val = 255
mask.putpixel((0, y), val)
alpha_mask = mask.resize(im.size)
# merge the reflection onto our background color using the alpha mask
reflection = Image.composite(background, reflection, alpha_mask)
# crop the reflection
reflection_height = int(im.size[1] * amount)
reflection = reflection.crop((0, reflection_height))
# create new image sized to hold both the original image and the reflection
composite = Image.new("RGB", (im.size[0], im.size[1] + reflection_height), background_color)
# paste the orignal image and the reflection into the composite image
composite.paste(im, 0))
composite.paste(reflection, im.size[1]))
# return the image complete with reflection effect
return composite