已有183人关注
文字水印总是提示图片格式错误
发表在Python图书答疑 2021-02-19 《PyQt5&Python游戏开发》第15章 明日图片助手(Python+PyQt5+PIL模块实现)
是否精华
版块置顶:
(imgnewImgPath):
    :
        = Image
        im = Image.open(img).convert()  newImg = Image.new(im.size())  font = ImageFont.truetype(.fontInfo.pointSize())
        imagedraw = ImageDraw.Draw(newImg)  imgwidthimgheight = im.size  txtwidth = .fontSize.maxWidth() * len(.lineEdit.text())  txtheight = .fontSize.height()  .comboBox.currentText() == :
            position = ()
        .comboBox.currentText() == :
            position = (imgheight - txtheight)
        .comboBox.currentText() == :
            position = (imgwidth - txtwidth)
        .comboBox.currentText() == :
            position = (imgwidth - txtwidthimgheight - txtheight)
        .comboBox.currentText() == :
            position = (imgwidth / imgheight / )
        imagedraw.text(position.lineEdit.text()=font=)
        alpha = newImg.split()[]
        alpha = ImageEnhance.Brightness(alpha).enhance(int(.horizontalSlider.value()) / )
        newImg.putalpha(alpha)
        Image.alpha_composite(imnewImg).save(newImgPath)  Exception:
        QMessageBox.warning(QMessageBox.Ok)

分享到:
精彩评论 5
menglu503
学分:12 LV2
2021-02-19
沙发

源代码都有问题,我运行就直接跳到exception 语句了,所以想问问老师们是不是代码有问题

小禾斗
学分:7308 LV13
TA的每日心情
奋斗
2017-04-21 14:00:25
2021-02-19
板凳

用png格式的图片操作,有的图片没有透明度那个通道,比如jpg的,所以就会出现问题。

menglu503
学分:12 LV2
2021-02-20
地板

小禾斗 发表于2021-02-19 16:49

用png格式的图片操作,有的图片没有透明度那个通道,比如jpg的,所以就会出现问题。

老师我用的文字水印,这都出错,jpg格式有问题,这个程序我弄了两个改好了,但是图片水印我还没改

menglu503
学分:12 LV2
2021-02-20
4L
def textMark(self, img, newImgPath):
    try:
        im = Image.open(img).convert("RGBA")  # 打开原始图片,并转换为RGBA
        newImg = Image.new("RGBA", im.size, (255, 255, 255, 0))  # 存储添加水印后的图片
        # 创建字体,说明:默认使用楷体,如果需要使用其他字体,需要将字体文件复制到当前目录中
        # 然后对下面第一个参数进行修改,可以使用self.fontInfo.family()动态获取字体名称,后面加扩展名即可
        # fontziti = self.fontInfo.family()
        font = ImageFont.truetype(r'DENG.ttf', 240)
        imagedraw = ImageDraw.Draw(newImg)  # 创建绘制对象
        imgwidth, imgheight = im.size  # 记录图片大小
        txtwidth =font.getsize(self.lineEdit.text())[0] # 获取字体宽度
        txtheight = font.getsize(self.lineEdit.text())[1] # 获取字体高度

        # 设置水印文字位置
        position = (0, 0)
        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)
        elif self.comboBox.currentText() == '居中位置':
            position = (imgwidth / 2, imgheight / 2)
        # 设置文本颜色
        imagedraw.text(position, self.lineEdit.text(), font=font, fill="#FCA454")
        # 设置透明度
        alpha = newImg.split()[3]
        alphavalue = self.horizontalSlider.value()
        alpha = ImageEnhance.Brightness(alpha).enhance(int(alphavalue) / 10.0)
        newImg.putalpha(alpha)
        newImg.convert("RGBA")
        nnimg =Image.alpha_composite(im, newImg)
        nnimg = nnimg.convert("RGB")
        nnimg.save(newImgPath)  # 保存图片
    except Exception:
        QMessageBox.warning(None, '错误', '图片格式有误,请重新选择……', QMessageBox.Ok)


menglu503
学分:12 LV2
2021-02-20
5L

menglu503 发表于2021-02-20 09:45

老师我用的文字水印,这都出错,jpg格式有问题,这个程序我弄了两个改好了,但是图片水印我还没改

老师,我放在下一个回复了,主要是RGHA保存转换成RGB,另外,有一个我没解决的问题:

self.fontInfo.pointSize()
self.fontSize.maxWidth()

这两行代码因为这两个参数运行都出错,最好我只好改成了常数

首页上一页 1 下一页尾页 5 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照