Python PILPillow-如何在附加到PDF文件的图像上获得不透明度?

问题描述

目标:将图像保存到文件中(每个图像都有自己的页面)。每个图像上都会添加文字,并且可以打印。

我有一个要与RGBA文本图像(从API提取的文本字符串)组合的图像数组(png),创建一个alpha复合图像(将它们组合),然后附加到PDF文件。问题是,如果我想将其附加到pdf上,则图像不能是RGBA,将其设置为RGB会使背景变为黑色,而不是白色/不透明。

这是代码:

from PIL import Image,ImageDraw,ImageFont
import time



def write_custom_strings(model,strings,font_color):
  base = Image.open(f"./label_templates/{model}.png")
  # make a blank image for the text,initialized to transparent text color
  txt = Image.new('RGBA',base.size,(255,255,1))
  image_width = base.size[0]
  image_height = base.size[1]

  # get a drawing context
  d = ImageDraw.Draw(txt)
  # Get font
  fnt = ImageFont.truetype('./cond_mono.ttf',65)
  text_width,text_height = d.textsize(strings) 
  # draw text
  d.multiline_text(((image_width - text_width * 3.5)/2,725),font=fnt,fill=font_color)
  out = Image.alpha_composite(base,txt)
  out = out.convert('RGB')
  out.show() # Show Image in development
  return out

def save_to_file(images):
  filename = generate_filename()
  if len(images) > 0:
    images[0].save(f"./product_labels/{filename}",save_all=True,append_images=images[1:])
  else:
    print("No new items")
def generate_filename():
  date = time.localtime()
   # Create filename using date/time
  return f"Custom-Orders:{str(date.tm_mon)}.{str(date.tm_mday)}.{str(date.tm_year)}-{str(date.tm_hour)}:{str(date.tm_min)}:{str(date.tm_sec)}.pdf" 


解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...