2

I have jpg format images and I don't want to convert in png format to resolve this error any other way to resolve this error

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

actually i want to create watermark image according to my code i have to compulsory use RGBA mode. please give me solution

Error : cannot write mode RGBA as JPEG

Jessica
  • 205
  • 2
  • 6
  • 14
  • The JPEG format didn't support RGBA, so you can't do it unless you convert to an image file format that does. – martineau Sep 25 '18 at 11:51
  • and 'RGB' mode ?? @martineau – Jessica Sep 25 '18 at 11:54
  • Yes, JPEG does support RGB mode – martineau Sep 25 '18 at 11:55
  • i set 'RGB' mode then i get error :image has wrong mode @martineau – Jessica Sep 25 '18 at 11:57
  • May be it's not the `convert()` call. What kind of image file are you reading? – martineau Sep 25 '18 at 11:58
  • it has str type... image with path – Jessica Sep 25 '18 at 12:04
  • i edited question if you find out some solution @martineau – Jessica Sep 25 '18 at 12:17
  • `Image.open()` is used to load an existing image file, i.e. `Image.open("picture.jpg")`. Here's some [documentation](https://pillow.readthedocs.io/en/5.2.x/reference/Image.html#PIL.Image.open). The file name extension determines the image file format expected. My question was what kind of image file are you trying to read (and convert)? You don't have any code in your question that is tries to write a file (and the default mode for `open()` is to open the image only for reading). – martineau Sep 25 '18 at 13:31
  • The `A` in `RGBA` stands for `alpha` also known as `transparency`. JPEG (Joint **Photographic** Experts Group) does not support transparency because it is for photos and cameras don't normally create transparency layers or photograph transparent things - mainly graphics designers and web designers create transparency layers. – Mark Setchell Sep 25 '18 at 15:04

1 Answers1

3

summary 1 and 2:

  • backgroud
    • JPG not support alpha = transparency
    • RGBA, P has alpha = transparency
      • RGBA= Red Green Blue Alpha
  • result
    • cannot write mode RGBA as JPEG
    • cannot write mode P as JPEG
  • solution
    • before save to JPG, discard alpha = transparency
      • such as: convert Image to RGB
    • then save to JPG
  • your code
input_image = Image.open(image_path).convert('RGB')
txt = Image.new('RGB', input_image.size, (255, 255, 255, 0))
crifan
  • 12,947
  • 1
  • 71
  • 56