本文整理汇总了Python中imagekit.lib.Image.new方法的典型用法代码示例。如果您正苦于以下问题:Python Image.new方法的具体用法?Python Image.new怎么用?Python Image.new使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类imagekit.lib.Image
的用法示例。
在下文中一共展示了Image.new方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import new [as 别名]
def process(self, img):
# Convert bgcolor string to RGB value.
background_color = ImageColor.getrgb(self.background_color)
# Handle palleted images.
img = img.convert('RGBA')
# Copy orignial image and flip the orientation.
reflection = img.copy().transpose(Image.FLIP_TOP_BOTTOM)
# Create a new image filled with the bgcolor the same size.
background = Image.new("RGBA", img.size, background_color)
# Calculate our alpha mask.
start = int(255 - (255 * self.opacity)) # The start of our gradient.
steps = int(255 * self.size) # 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(img.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(img.size[1] * self.size)
reflection = reflection.crop((0, 0, img.size[0], reflection_height))
# Create new image sized to hold both the original image and
# the reflection.
composite = Image.new("RGBA", (img.size[0], img.size[1] + reflection_height), background_color)
# Paste the orignal image and the reflection into the composite image.
composite.paste(img, (0, 0))
composite.paste(reflection, (0, img.size[1]))
# Return the image complete with reflection effect.
return composite
示例2: process
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import new [as 别名]
def process(cls, image, fmt, obj=None):
"""Adds a watermark to an image."""
if image.mode != 'RGBA':
image = image.convert('RGBA')
pink = (255, 94, 200)
white = (255, 255, 255)
im_width, im_height = image.size
copyright = "Copyright \xa9 by the Artist and Burning Man"
overlay = Image.new('RGBA', image.size, (0,0,0,0))
draw = ImageDraw.Draw(overlay)
draw.rectangle((0, im_height - 20, im_width, im_height),
fill=(0,0,0,90))
draw.text((10, im_height - 15), copyright, fill=(255,255,255,90))
newimage = Image.new('RGB', (im_width, im_height + 50), white)
draw2 = ImageDraw.Draw(newimage)
draw2.text((10, im_height + 5),
"\xa9 All images are copyright in their respective year, by "
"both the ",
fill=pink)
draw2.text((10, im_height + 15),
"photographer and Burning Man. For publication or other use ",
fill=pink)
draw2.text((10, im_height + 25),
"requests, contact the photographer at the email provided and ",
fill=pink)
draw2.text((10, im_height + 35),
"[email protected] for written permission.", fill=pink)
comp = Image.composite(overlay, image, overlay)
newimage.paste(comp, (0,0))
return newimage, fmt
示例3: detect_border_color
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import new [as 别名]
def detect_border_color(img):
mask = Image.new("1", img.size, 1)
w, h = img.size[0] - 2, img.size[1] - 2
if w > 0 and h > 0:
draw = ImageDraw.Draw(mask)
draw.rectangle([1, 1, w, h], 0)
return ImageStat.Stat(img.convert("RGBA").histogram(mask)).median
示例4: process
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import new [as 别名]
def process(self, image):
mask = Image.open(os.path.join(settings.STATIC_ROOT, 'img/avatar-mask.png'))
layer = Image.new('RGBA', image.size)
layer.paste(mask)
newImage = Image.composite(layer, image, layer)
return newImage
示例5: process
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import new [as 别名]
def process(self, img):
# get watermark
wm = self._get_watermark()
wm_size = wm.size
# print('wm', wm)
# print('wm.mode', wm.mode)
# from PIL.PngImagePlugin import PngImageFile
# from PIL.JpegImagePlugin import JpegImageFile
if self.scale:
if isinstance(self.scale, (int, float)) and self.scale != 1:
# L&X
# wm_size[0] *= self.scale
# wm_size[1] *= self.scale
wm_size = (wm_size[0] * self.scale, wm_size[1] * self.scale)
wm = wm.scale(wm_size)
elif self.scale == True:
# from .resize import ResizeToFit
from imagekit.processors import ResizeToFit
wm = ResizeToFit(width=img.size[0], height=img.size[1],
upscale=True).process(wm)
wm_size = wm.size
# prepare image for overlaying (ensure alpha channel)
if img.mode != 'RGBA':
img = img.convert('RGBA')
# create a layer to place the watermark
layer = Image.new('RGBA', img.size, (0, 0, 0, 0))
coords = _process_coords(img.size, wm_size, self.position)
print('L&X', 'wm', wm)
print('L&X', 'wm.size', wm.size)
print('L&X', 'coords', coords)
coords = (int(coords[0]), int(coords[1]))
if self.repeat:
sx = coords[0] % wm_size[0] - wm_size[0]
sy = coords[1] % wm_size[1] - wm_size[1]
for x in range(sx, img.size[0], wm_size[0]):
for y in range(sy, img.size[1], wm_size[1]):
layer.paste(wm, (x, y))
else:
layer.paste(wm, coords)
if self.opacity < 1:
alpha = layer.split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(self.opacity)
layer.putalpha(alpha)
# merge watermark layer
img = Image.composite(layer, img, layer)
return img
示例6: process
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import new [as 别名]
def process(cls, img, fmt, obj):
if cls.width and cls.height:
background_color = ImageColor.getrgb(cls.background_color)
#FIXME : Image is not imported but it never raises exception so ...
bg_picture = Image.new("RGB", (cls.width, cls.height), background_color)
## paste it
bg_w, bg_h = bg_picture.size
img_w, img_h = img.size
coord_x, coord_y = (bg_w - img_w) / 2, (bg_h - img_h) / 2
bg_picture.paste(img, (coord_x, coord_y, coord_x + img_w, coord_y + img_h))
return bg_picture, fmt
示例7: process
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import new [as 别名]
def process(self, img):
original_width, original_height = img.size
if self.anchor:
anchor = Anchor.get_tuple(self.anchor)
trim_x, trim_y = self.width - original_width, \
self.height - original_height
x = int(float(trim_x) * float(anchor[0]))
y = int(float(trim_y) * float(anchor[1]))
else:
x, y = self.x, self.y
new_img = Image.new('RGBA', (self.width, self.height), self.color)
new_img.paste(img, (x, y))
return new_img
示例8: process
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import new [as 别名]
def process(self, img):
# get watermark
wm = self._get_watermark()
wm_size = wm.size
if self.scale:
if isinstance(self.scale, (int, float)) and self.scale != 1:
wm_size[0] *= self.scale
wm_size[1] *= self.scale
wm = wm.scale(wm_size)
elif self.scale == True:
wm = ResizeToFit(width=img.size[0], height=img.size[1],
upscale=True).process(wm)
wm_size = wm.size
# prepare image for overlaying (ensure alpha channel)
if img.mode != 'RGBA':
img = img.convert('RGBA')
# create a layer to place the watermark
layer = Image.new('RGBA', img.size, (0,0,0,0))
coords = _process_coords(img.size, wm_size, self.position)
if self.repeat:
sx = coords[0] % wm_size[0] - wm_size[0]
sy = coords[1] % wm_size[1] - wm_size[1]
for x in range(sx, img.size[0], wm_size[0]):
for y in range(sy, img.size[1], wm_size[1]):
layer.paste(wm, (x,y))
else:
layer.paste(wm, coords)
if self.opacity < 1:
alpha = layer.split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(self.opacity)
layer.putalpha(alpha)
# merge watermark layer
img = Image.composite(layer, img, layer)
return img
示例9: process
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import new [as 别名]
def process(self, img):
source = img.convert("RGBA")
border_color = self.color or tuple(detect_border_color(source))
bg = Image.new("RGBA", img.size, border_color)
diff = ImageChops.difference(source, bg)
if self.tolerance not in (0, 1):
# If tolerance is zero, we've already done the job. A tolerance of
# one would mean to trim EVERY color, and since that would result
# in a zero-sized image, we just ignore it.
if not 0 <= self.tolerance <= 1:
raise ValueError(
"%s is an invalid tolerance. Acceptable values" " are between 0 and 1 (inclusive)." % self.tolerance
)
tmp = ImageChops.constant(diff, int(self.tolerance * 255)).convert("RGBA")
diff = ImageChops.subtract(diff, tmp)
bbox = diff.getbbox()
if bbox:
img = _crop(img, bbox, self.sides)
return img
示例10: generate_image
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import new [as 别名]
def generate_image(self):
tmp = tempfile.TemporaryFile()
Image.new('RGB', (800, 600)).save(tmp, 'JPEG')
tmp.seek(0)
return tmp
示例11: add_watermark
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import new [as 别名]
def add_watermark(image, footer=True, extended=True):
if image.mode != 'RGBA':
image = image.convert('RGBA')
pink = (255, 94, 200)
white = (255, 255, 255)
im_width, im_height = image.size
log.debug('size: %s', image.size)
if im_height:
fontsize = int(im_height/34)
else:
fontsize = int(im_width/34)
if fontsize > 16:
fontsize = 16
log.debug('font size=%i', fontsize)
ttf = os.path.join(settings.MEDIA_ROOT, 'fonts', 'Tahoma.ttf')
font = ImageFont.truetype(ttf, fontsize)
overlay = Image.new('RGBA', image.size, (0,0,0,0))
if footer:
copyright = "Copyright \xa9 by the Artist and Burning Man"
draw = ImageDraw.Draw(overlay)
draw.rectangle((0, im_height - fontsize - 6, im_width, im_height),
fill=(0,0,0,90))
draw.text((10, im_height - fontsize - 4), copyright, fill=(255,255,255,90), font=font)
if extended:
newimage = Image.new('RGB', (im_width, im_height + 200), white)
textheight = draw_word_wrap(newimage, EXTRA_COPY, 10, 5, max_width=im_width-10, fill=pink, font=font, height_only=True)
h = im_height + textheight + 5
log.debug('new height: %s, textheight=%s', h, textheight)
newimage = Image.new('RGB', (im_width, h), white)
else:
newimage = Image.new('RGB', (im_width, im_height))
if extended:
#draw2 = ImageDraw.Draw(newimage)
draw_word_wrap(newimage, EXTRA_COPY, 10, im_height + 5, max_width=im_width-10, fill=pink, font=font)
# draw2.text((10, h),
# "\xa9 All images are copyright in their respective year, by "
# "both the ",
# fill=pink,
# font=font)
# h += fontsize + 3
# draw2.text((10, h),
# "photographer and Burning Man. For publication or other use ",
# fill=pink,
# font=font)
# h += fontsize + 3
# draw2.text((10, h),
# "requests, contact the photographer at the email provided and ",
# fill=pink,
# font=font)
# h += fontsize + 3
# draw2.text((10, h),
# "[email protected] for written permission.",
# fill=pink,
# font=font)
comp = Image.composite(overlay, image, overlay)
newimage.paste(comp, (0,0))
return newimage
示例12: process
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import new [as 别名]
def process(self, img):
matte = False
self.save_kwargs = {}
self.rgba_ = img.mode == 'RGBA'
if self.rgba_:
if self.format in RGBA_TRANSPARENCY_FORMATS:
pass
elif self.format in PALETTE_TRANSPARENCY_FORMATS:
# If you're going from a format with alpha transparency to one
# with palette transparency, transparency values will be
# snapped: pixels that are more opaque than not will become
# fully opaque; pixels that are more transparent than not will
# become fully transparent. This will not produce a good-looking
# result if your image contains varying levels of opacity; in
# that case, you'll probably want to use a processor to matte
# the image on a solid color. The reason we don't matte by
# default is because not doing so allows processors to treat
# RGBA-format images as a super-type of P-format images: if you
# have an RGBA-format image with only a single transparent
# color, and save it as a GIF, it will retain its transparency.
# In other words, a P-format image converted to an
# RGBA-formatted image by a processor and then saved as a
# P-format image will give the expected results.
alpha = img.split()[-1]
mask = Image.eval(alpha, lambda a: 255 if a <= 128 else 0)
img = img.convert('RGB').convert('P', palette=Image.ADAPTIVE,
colors=255)
img.paste(255, mask)
self.save_kwargs['transparency'] = 255
else:
# Simply converting an RGBA-format image to an RGB one creates a
# gross result, so we matte the image on a white background. If
# that's not what you want, that's fine: use a processor to deal
# with the transparency however you want. This is simply a
# sensible default that will always produce something that looks
# good. Or at least, it will look better than just a straight
# conversion.
matte = True
elif img.mode == 'P':
if self.format in PALETTE_TRANSPARENCY_FORMATS:
try:
self.save_kwargs['transparency'] = img.info['transparency']
except KeyError:
pass
elif self.format in RGBA_TRANSPARENCY_FORMATS:
# Currently PIL doesn't support any RGBA-mode formats that
# aren't also P-mode formats, so this will never happen.
img = img.convert('RGBA')
else:
matte = True
else:
img = img.convert('RGB')
# GIFs are always going to be in palette mode, so we can do a little
# optimization. Note that the RGBA sources also use adaptive
# quantization (above). Images that are already in P mode don't need
# any quantization because their colors are already limited.
if self.format == 'GIF':
img = img.convert('P', palette=Image.ADAPTIVE)
if matte:
img = img.convert('RGBA')
bg = Image.new('RGBA', img.size, (255, 255, 255))
bg.paste(img, img)
img = bg.convert('RGB')
if self.format == 'JPEG':
self.save_kwargs['optimize'] = True
return img
示例13: get_watermark
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import new [as 别名]
def get_watermark(self):
wm = Image.new("RGBA", self.font_size, (0,0,0,0))
draw = ImageDraw.Draw(wm, "RGBA")
draw.text((0,0), self.text, font=self.font,
fill=self.text_color)
return wm