本站首页    管理页面    写新日志    退出


«October 2025»
1234
567891011
12131415161718
19202122232425
262728293031


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7631757
建立时间:2006年5月29日




[Python]图片处理
软件技术

lhwork 发表于 2007/2/2 15:15:16

#-*- coding:GBK -*-#==========================#The work with a picture!#The GPL License#Emal:ghostwwl@gmail.com#  edit by Ghostwwl#==========================  import Imageimport ImageDrawimport ImageFontfrom math import atan, degreesfrom os import pathimport osimport sys class MyImage:    def __init__(self, imfile = None):        if imfile:            self.filename, self.ext = path.splitext(imfile)        self.Image = Image.open(imfile)     def Rotate(self, degrees = 0):        self.Image = self.Image.rotate(degrees)        s = raw_input(”是否保存:”)        if “y” in s or “Y” in s:            self.Image.save(self.filename+”_Rotate.jpg”)            def Show(self):        self.Image.show()            def CreateThumbnail(self, size = (128,128)):        try:            self.Image.thumbnail(size)            self.Image.save(self.filename+”_Thum.jpg”, “JPEG”)            print “缩略图文件保存在: %s” % (self.filename+”_Thum.jpg”,)        except IOError:             sys.exit(”创建缩略图失败!”)                def GetSize(self):        return self.Image.size       def Save(self):        try:            self.Image.save(self.filename+”_Worked.jpg”,”JPEG”)        except IOError:            sys.exit(”保存失败!”)       def WaterMark(self,text):        img = self.Image.convert(”RGB”)        watermark = Image.new(”RGBA”, img.size)        Draw = ImageDraw.ImageDraw(watermark, “RGBA”)        size = 0        while True:            size += 1            font = ImageFont.truetype(”c:\\windows\\Fonts\\simsun.ttc”, size)            twidth, theight = font.getsize(text)            if twidth + theight/3 > watermark.size[0]:               break            Font = font            TextWidth, TextHeight = twidth, theight        Draw.setfont(Font)        Draw.text(((watermark.size[0]-TextWidth)/2, (watermark.size[1]-TextHeight)/2), unicode(text,”GBK”))        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(self.filename+”_Text.jpg”) def main():    while True:        ImageFile = raw_input(”\n请输入图像文件:”)        if path.exists(ImageFile):            Img = MyImage(ImageFile)            break        else:            print “\n请确保你输入的文件存在!”     print “”"======================================================图像旋转输入 rotate创建缩略图输入 create图像显示输入 show图像加水印输入 mark======================================================                     “”"       while True:        n = raw_input(”请选择:”)        if n in (”rotate”,”create”,”show”,”mark”):            break     if n == “rotate” :        degrees = int(raw_input(”\n请输入旋转的角度:”))        Img.Rotate(degrees)    elif n == “create”:        strSize = raw_input(”请输入缩略图大小(如:128,128):”).split(’,')        Size = (int(strSize[0]),int(strSize[1]))        Img.CreateThumbnail(Size)    elif n == “show”:        Img.Show()    else:        WaterText = raw_input(”请输入水印文字:”)        Img.WaterMark(WaterText)    os.system(”pause”)    if __name__==”__main__”:    main() 这几天看了下pil,然后也google下,写了个玩的,里面watermark那个函数根据网上发的zope里那个改的,其他的自己写的,做成exe的时候老出错,后来我把pil库换成1.1.6就没问题了 500)this.width=500'>


阅读全文(2939) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 1.234 second(s), page refreshed 144806468 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号