PIL 给图片增加水印

JPG[不支持]透明度 – RGBA表示红色,绿色,蓝色,Alpha – Alpha是透明度.
你需要丢弃Alpha通道或保存为[支持]透明度的东西 – 比如PNG。

模式
1 1位像素,黑和白,存成8位的像素
L 8位像素,黑白
P 8位像素,使用调色板映射到任何其他模式
RGB 3×8位像素,真彩
RGBA 4×8位像素,真彩+透明通道
CMYK 4×8位像素,颜色隔离
YCbCr 3×8位像素,彩色视频格式
I 32位整型像素
F 32位浮点型像素

jpg 转换成png

#----------------------------------------------
from PIL import Image
im = Image.open("3d.jpg")
im.save("3d.png") 

图片上面添加内容

from PIL import Image, ImageDraw, ImageFont
font = ImageFont.truetype("segoeui.ttf", 30)   # 字体、字体大小

给图片加上水印文本,newImgPath新文件的后缀应为 png。

   def textMark(self, imgfile, newImgPath):
        try:
            img = Image.open(imgfile).convert("RGBA")
            newImg = Image.new('RGBA', img.size, (255,255,255,0))

            imagedraw = ImageDraw.Draw(newImg)

            imgwidth , imgheight = img.size
            txtwidth = self.fontSize.maxWidth()
            txtheight = self.fontSize.height()

            #设置 水印位置
            if self.comboBox.currentText() == "左上角":
                position = (0, 0)
            elif self.comboBox.currentText() == "左下角":
                position = (0, imgheight-txtheight)
            elif self.comboBox.currentText() == "右上角":
                position = (imgwidth-txtwidth, 0)
            elif self.comboBox.currentText() == "右下角":
                position = (imgwidth - txtwidth, imgheight - txtheight)
            else:
                position = (imgwidth/2-txtwidth/2, imgheight/2-txtheight/2)

            font = ImageFont.truetype("segoeui.ttf", self.fontInfo.pointSize())
            imagedraw.text(position, self.lineEdit.text(), font = font, fill="#FCA454")

            alpha = newImg.split()[3]
            alpha = ImageEnhance.Brightness(alpha).enhance(int(self.horizontalSlider.value())/10.0)
            newImg.putalpha(alpha)
            Image.alpha_composite(img, newImg).save(newImgPath)
            return True

        except Exception as e:
            print (imgfile, e)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值