【问题标题】:cannot write mode RGBA as JPEG how to resolve this error without converting image format无法将模式 RGBA 写入 JPEG 如何在不转换图像格式的情况下解决此错误
【发布时间】:2019-03-01 00:48:36
【问题描述】:

我有 jpg 格式的图像,我不想转换为 png 格式来解决这个错误,任何其他方式来解决这个错误

input_image = Image.open(image_path).convert('RGBA')
txt = Image.new('RGBA', input_image.size, (255, 255, 255, 0))

实际上我想根据我的代码创建水印图像,我必须强制使用 RGBA 模式。请给我解决方案

错误:无法将模式 RGBA 写入 JPEG

【问题讨论】:

  • JPEG 格式不支持 RGBA,所以除非你转换成支持 RGBA 的图像文件格式,否则你不能这样做。
  • 和“RGB”模式?? @martineau
  • 是的,JPEG 确实支持 RGB 模式
  • 我设置了“RGB”模式然后我得到错误:图像模式错误@martineau
  • 可能不是convert() 电话。你正在阅读什么样的图像文件?

标签: python python-imaging-library


【解决方案1】:

总结12

  • 背景
    • JPG不支持alpha = transparency
    • RGBA, Palpha = transparency
      • RGBA= Red Green Blue Alpha
  • 结果
    • cannot write mode RGBA as JPEG
    • cannot write mode P as JPEG
  • 解决方案
    • 在保存为 JPG 之前,丢弃alpha = transparency
      • 如:将Image转换为RGB
    • 然后保存到JPG
  • 您的代码
input_image = Image.open(image_path).convert('RGB')
txt = Image.new('RGB', input_image.size, (255, 255, 255, 0))

【讨论】:

    猜你喜欢
    • 2018-06-23
    • 1970-01-01
    • 2015-11-27
    • 2023-04-01
    • 2015-04-10
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多