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)