• python下载图片(2)


    #-*- coding: UTF-8 -*-
    import urllib2, re,datetime,time, os,sys
    from PIL import Image, ImageDraw, ImageFont
    from math import atan, degrees

    #
    #图片下载类
    #功能:下载图片,生成缩略图,生成水印图
    #
    class downloadPic:
    def __init__(self,picDir,Font):
    self.picDir=picDir
    self.FONT = Font

    def getFilename(self,urlT):
    #print urlT
    ndate=datetime.datetime.now()

    if not os.path.exists('%s%s' %(self.picDir,ndate.year)):
    os.mkdir('%s%s' %(self.picDir,ndate.year))
    if not os.path.exists('%s%s/%s' % (self.picDir,ndate.year,ndate.month)):
    os.mkdir('%s%s/%s' % (self.picDir,ndate.year,ndate.month))
    if not os.path.exists('%s%s/%s/%s' % (self.picDir,ndate.year,ndate.month,ndate.day)):
    os.mkdir('%s%s/%s/%s' % (self.picDir,ndate.year,ndate.month,ndate.day))
    if not os.path.exists('%s%s/%s/%s/%s' % (self.picDir,ndate.year,ndate.month,ndate.day,'src')):
    os.mkdir('%s%s/%s/%s/%s' % (self.picDir,ndate.year,ndate.month,ndate.day,'small'))
    os.mkdir('%s%s/%s/%s/%s' % (self.picDir,ndate.year,ndate.month,ndate.day,'mark'))
    os.mkdir('%s%s/%s/%s/%s' % (self.picDir,ndate.year,ndate.month,ndate.day,'src'))
    extname=urlT.split('.')
    extname=extname[len(extname)-1]

    ntime="%s.%s0000" % (int(time.mktime(ndate.timetuple())),ndate.microsecond)
    ntime=ntime[:17]
    filename='%s%s/%s/%s/src/%s.%s' % (self.picDir,ndate.year,ndate.month,ndate.day,ntime,extname)
    print filename
    self
    return [filename,ntime]#图片全地址,保存数据库ID
    def downloadPic(self,urlT,filenameT):

    request = urllib2.Request(urlT)
    opener = urllib2.build_opener()
    f = opener.open(request)
    #print f.headers.dict['content-type']
    #if f.headers.dict['content-type']=='image/jpeg':
    #保存到文件
    xfile = open(filenameT, 'wb')
    xfile.write(f.read())
    xfile.close()
    print '下载图片%s ok' % urlT
    return True
    #else:
    #TODO 抛出异常
    #raise Exception,'not img'
    def markPic(self,srcFileName,text,markFileName):
    img = Image.open(srcFileName).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(self.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.thumbnail((300, 300))
    img.save(markFileName)
    def smallPic(self,srcFileName,smallFileNmae):
    im = Image.open(srcFileName)
    im.thumbnail((100, 100))
    im.save(smallFileNmae)

    服务项目 技术咨询 微信图书 微信视频 微信代码 定制开发 其他福利
    服务入口 QQ群有问必答
    查看详情
    一本书解决90%问题
    查看详情
    微信开发视频
    小程序开发视频
    免费代码
    ¥1888阿里云代金券
    查看详情
    营销工具
    微信特异功能
  • 相关阅读:
    Scrapy的安装和基本使用方法
    使用BERT词向量
    主动学习方法实践:让模型变“主动”
    WSAI2020
    mac电脑的图片存储的位置
    问答系统-不能直接根据问题寻找答案?
    谷歌飞马PEGASUS
    通过强化学习协作多代理对话模型训练
    学习笔记(55)- 深度学习工具及平台
    学习笔记(54)- NLU
  • 原文地址:https://www.cnblogs.com/txw1958/p/2218006.html
Copyright © 2020-2023  润新知