def variation_watermark(image, **wm_settings):
""" ????????? ???????? ????? ?? ???????? """
if image.mode != 'RGBA':
image = image.convert('RGBA')
with open(wm_settings['file'], 'rb') as fp:
watermark = Image.open(fp)
info = watermark.info
if watermark.mode not in ('RGBA', 'LA') and not (watermark.mode == 'P' and 'transparency' in info):
watermark.putalpha(255)
img_width, img_height = image.size
wm_width, wm_height = watermark.size
scale = wm_settings['scale']
if scale != 1:
watermark = watermark.resize((int(wm_width * scale), int(wm_height * scale)), Image.ANTIALIAS)
wm_width, wm_height = watermark.size
position = wm_settings['position']
padding = wm_settings['padding']
if position == 'TL':
left = padding[0]
top = padding[1]
elif position == 'TR':
left = img_width - wm_width - padding[0]
top = padding[1]
elif position == 'BL':
left = padding[0]
top = img_height - wm_height - padding[1]
elif position == 'BR':
left = img_width - wm_width - padding[0]
top = img_height - wm_height - padding[1]
elif position == 'C':
top = (img_height - wm_height) // 2
left = (img_width - wm_width) // 2
else:
left = top = padding
opacity = wm_settings['opacity']
if opacity < 1:
alpha = watermark.convert('RGBA').split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
watermark.putalpha(alpha)
image.paste(watermark, (left, top), watermark)
return image
python类Brightness()的实例源码
def _draw_watermark(self):
'''Draws a watermark on the image'''
watermark_image = PIL_Image.new('RGBA', self.image.size, (0, 0, 0, 0))
self.watermark._adjust_fontsize(self.image)
draw = ImageDraw.Draw(watermark_image, 'RGBA')
w_width, w_height = self.watermark.font.getsize(self.watermark.text)
draw.text(((watermark_image.size[0] - w_width) / 2,
(watermark_image.size[1] - w_height) / 2),
self.watermark.text, font=self.watermark.font,
fill=self.watermark.color)
if self.watermark.rotate_angle:
watermark_image = watermark_image.rotate(
self.watermark.rotate_angle, PIL_Image.BICUBIC)
alpha = watermark_image.split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(self.watermark.opacity)
watermark_image.putalpha(alpha)
# Because watermark can be rotated we create a separate image for cross
# so that it doesn't get rotated also. + It's impossible to drawn
# on a rotated image
if self.watermark.cross:
watermark_cross_image = PIL_Image.new(
'RGBA', self.image.size, (0, 0, 0, 0))
cross_draw = ImageDraw.Draw(watermark_cross_image, 'RGBA')
line_width = 1 + int(sum(self.image.size) / 2 * 0.007)
cross_draw.line(
(0, 0) + watermark_image.size,
fill=self.watermark.color,
width=line_width
)
cross_draw.line(
(0, watermark_image.size[1], watermark_image.size[0], 0),
fill=self.watermark.color,
width=line_width
)
watermark_cross_alpha = watermark_cross_image.split()[3]
watermark_cross_alpha = ImageEnhance.Brightness(
watermark_cross_alpha).enhance(self.watermark.opacity)
watermark_cross_image.putalpha(watermark_cross_alpha)
# Adds cross to the watermark
watermark_image = PIL_Image.composite(
watermark_cross_image, watermark_image, watermark_cross_image)
self.image = PIL_Image.composite(
watermark_image, self.image, watermark_image)
def gen_pic(MediaId):
pic = Image.open('media/' + MediaId).convert('LA')
# pic = ImageEnhance.Brightness(pic).enhance(0.35)
portion = pic.size[0]/pic.size[1]
if portion < 1:
#portrait
if portion <= 0.75:
pic_w = 960
pic_h = round(960/portion)
box = (0,round((pic_h-1280)/2),960,round(pic_h/2+640))
if portion > 0.75:
pic_h = 1280
pic_w = round(1280*portion)
box = (round((pic_w-960)/2),0,round(pic_w/2+480),1280)
layer = Image.open('./lomolive/layer_p.png')
elif portion > 1:
#landscape
if portion >= 1.3333:
pic_h = 960
pic_w = round(960*portion)
box = (round((pic_w-1280)/2),0,round(pic_w/2+640),960)
if portion < 1.3333:
pic_w = 1280
pic_h = round(1280/portion)
box = (0,round((pic_h-960)/2),1280,round(pic_h/2+480))
layer = Image.open('./lomolive/layer_l.png')
elif portion == 1:
#square
(pic_w,pic_h) = (960,960)
box = (0,0,960,960)
layer = Image.open('./lomolive/layer_s.png')
pic = pic.resize((pic_w, pic_h))
pic = pic.crop(box)
pic = lomoize(pic, 0.4, 1)
pic = pic.convert('RGB')
pic.paste(layer,(0,0),layer)
pic.save('media/' + MediaId + '.jpg', quality=95)
def main():
import argparse
parser = argparse.ArgumentParser(description="Combine your QR code with custom picture")
parser.add_argument("image")
parser.add_argument("text", help="QRcode Text.")
parser.add_argument("-o", "--output", help="Name of output file.")
parser.add_argument("-v", "--version", type=int, help="QR version.In range of [1-40]")
parser.add_argument("-e", "--errorcorrect", choices={"L","M","Q","H"}, help="Error correct")
parser.add_argument("-b", "--brightness", type=float, help="Brightness enhance")
parser.add_argument("-c", "--contrast", type=float, help="Contrast enhance")
parser.add_argument("-C", "--colourful", action="store_true",help="colourful mode")
parser.add_argument("-r", "--rgba", nargs=4, metavar=('R','G','B','A'),type = int, help="color to replace black")
parser.add_argument("-p", "--pixelate", action="store_true",help="pixelate")
args = parser.parse_args()
img = args.image
txt = args.text
output = args.output if args.output else 'qr.png'
ec = qrcode.constants.ERROR_CORRECT_H
if args.errorcorrect:
ec_raw = args.errorcorrect
if ec_raw == 'L':
ec = qrcode.constants.ERROR_CORRECT_L
if ec_raw == 'M':
ec = qrcode.constants.ERROR_CORRECT_M
if ec_raw == 'Q':
ec = qrcode.constants.ERROR_CORRECT_Q
ver = 5
if args.version:
if args.version >= 1 and args.version <= 40:
ver = args.version
cont = args.contrast if args.contrast else 1.0
bri = args.brightness if args.brightness else 1.0
colr = True if args.colourful else False
pixelate = True if args.pixelate else False
if colr :
if args.rgba:
rgba = tuple(args.rgba)
else:
rgba = (0,0,0,255)
else:
rgba = (0,0,0,255)
frames = produce(txt,img,ver,ec,bri, cont ,colourful = colr,rgba=rgba,pixelate = pixelate)
if len(frames) == 1 or output.upper()[-3:] != "GIF":
frames[0].save(output)
elif len(frames) > 1:
frames[0].save(output,save_all=True,append_images=frames[1:],duration=100,optimize=True)