Python论坛  - 讨论区

标题:[python-chinese] 前段时间有人问如何用在网站的图片上自动加水印,用python可以这么做

2005年04月18日 星期一 10:07

alang yin alang.yl at gmail.com
Mon Apr 18 10:07:41 HKT 2005

我不记得到底是什么人问的,总之是有这么个问题。我看到一篇文章,改动一下就应能在web下用了。
原主的地址:*Textual watermarks with Python Imaging
Library*<http://www.livejournal.com/users/gniemeyer/10279.html>
(http://www.livejournal.com/users/gniemeyer/10279.html)

代码:

#!/usr/bin/python
from PIL import Image, ImageDraw, ImageFont
from math import atan, degrees
import sys
import os

FONT = "/usr/X11R6/lib/X11/fonts/TTF/Vera.ttf"

def main(filename, text, outfilename):
    img = Image.open(filename).convert("RGB")
    watermark = Image.new("RGBA", (img.size[0], img.size[1]))
    draw = ImageDraw.ImageDraw(watermark, "RGBA")
    size = 0
    while True:
        size += 1
        nextfont = ImageFont.truetype(FONT, size)
        nexttextwidth, nexttextheight = nextfont.getsize(text)
        if nexttextwidth+nexttextheight/3 > watermark.size[0]:
            break
        font = nextfont
        textwidth, textheight = nexttextwidth, nexttextheight
    draw.setfont(font)
    draw.text(((watermark.size[0]-textwidth)/2,
               (watermark.size[1]-textheight)/2), text)
    watermark = watermark.rotate(degrees(atan(float(img.size[1])/img.size[0])),
                                 Image.BICUBIC)
    mask = watermark.convert("L").point(lambda x: min(x, 55))
    watermark.putalpha(mask)
    img.paste(watermark, None, watermark)
    img.save(outfilename)

if __name__ == "__main__":
    if len(sys.argv) != 4:
        sys.exit("Usage: %s   "
                 % os.path.basename(sys.argv[0]))
    main(*sys.argv[1:])
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050418/179c28e0/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年04月18日 星期一 10:23

Frank gavin at sz.net.cn
Mon Apr 18 10:23:23 HKT 2005

Tks,你的记性真好!

是我问过如何用python实现网站登陆时自动生成校验码的问题:)

tks again!



  ----- Original Message ----- 
  From: alang yin 
  To: python-chinese at lists.python.cn 
  Sent: Monday, April 18, 2005 10:07 AM
  Subject: [python-chinese] 前段时间有人问如何用在网站的图片上自动加水印,用python可以这么做


  我不记得到底是什么人问的,总之是有这么个问题。我看到一篇文章,改动一下就应能在web下用了。
  原主的地址:Textual watermarks with Python Imaging Library
  (http://www.livejournal.com/users/gniemeyer/10279.html)

  代码:

#!/usr/bin/pythonfrom PIL import Image, ImageDraw, ImageFontfrom math import atan, degreesimport sysimport osFONT = "/usr/X11R6/lib/X11/fonts/TTF/Vera.ttf"
def main(filename, text, outfilename):    img = Image.open(filename).convert("RGB")    watermark = Image.new("RGBA", (img.size[0], img.size[1]))    draw = ImageDraw.ImageDraw(watermark, "RGBA")
    size = 0    while True:        size += 1        nextfont = ImageFont.truetype(FONT, size)        nexttextwidth, nexttextheight = nextfont.getsize(text)        if nexttextwidth+nexttextheight/3 > 
watermark.size[0]:            break        font = nextfont        textwidth, textheight = nexttextwidth, nexttextheight    draw.setfont(font)    draw.text(((watermark.size[0]-textwidth)/2,               (
watermark.size[1]-textheight)/2), text)    watermark = watermark.rotate(degrees(atan(float(img.size[1])/img.size[0])),                                 Image.BICUBIC)    mask = watermark.convert("L").point(lambda x: min(x, 55))
    watermark.putalpha(mask)    img.paste(watermark, None, watermark)    img.save(outfilename)if __name__ == "__main__":    if len(sys.argv) != 4:        sys.exit("Usage: %s   "
                 % os.path.basename(sys.argv[0]))    main(*sys.argv[1:])



------------------------------------------------------------------------------


  _______________________________________________
  python-chinese list
  python-chinese at lists.python.cn
  http://python.cn/mailman/listinfo/python-chinese
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050418/c47dd402/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年04月18日 星期一 10:25

Zoom Quiet zoom.quiet at gmail.com
Mon Apr 18 10:25:00 HKT 2005

原始链接
http://python.cn/contents/py_pil_imagemagick_pycom

还有新近发现的
http://blog.tcchou.org/Members/tcchou/python/chcaptcha

更加NB!可以使用中文进行图片认识哪!

在05-4-18,alang yin<alang.yl at gmail.com> 写道:
> 我不记得到底是什么人问的,总之是有这么个问题。我看到一篇文章,改动一下就应能在web下用了。
> 原主的地址:Textual watermarks with Python Imaging Library
>http://www.livejournal.com/users/gniemeyer/10279.html)
>  
>  代码:
>  #!/usr/bin/python
> from PIL import Image, ImageDraw, ImageFont
> from math import atan, degrees
> import sys
> import os
> 
> FONT = "/usr/X11R6/lib/X11/fonts/TTF/Vera.ttf"
> 
> 
> def main(filename, text, outfilename):
>  img = Image.open(filename).convert("RGB")
>  watermark = Image.new("RGBA", (img.size[0], img.size[1]))
>  draw = ImageDraw.ImageDraw(watermark, "RGBA")
> 
>  size = 0
>  while True:
>  size += 1
>  nextfont = ImageFont.truetype(FONT, size)
>  nexttextwidth, nexttextheight = nextfont.getsize(text)
>  if nexttextwidth+nexttextheight/3 > 
> watermark.size[0]:
>  break
>  font = nextfont
>  textwidth, textheight = nexttextwidth, nexttextheight
>  draw.setfont(font)
>  draw.text(((watermark.size[0]-textwidth)/2,
>  (
> watermark.size[1]-textheight)/2), text)
>  watermark = watermark.rotate(degrees(atan(float(img.size[1])/img.size[0])),
>  Image.BICUBIC)
>  mask = watermark.convert("L").point(lambda x: min(x, 55))
> 
>  watermark.putalpha(mask)
>  img.paste(watermark, None, watermark)
>  img.save(outfilename)
> 
> if __name__ == "__main__":
>  if len(sys.argv) != 4:
>  sys.exit("Usage: %s   "
> 
>  % os.path.basename(sys.argv[0]))
>  main(*sys.argv[1:])
> 
>  
>  
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 
> 
> 


-- 
[Time is unimportant, only life important!]

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号