Home > Blockchain >  pillow change picture color when save the picture
pillow change picture color when save the picture

Time:12-14

I want to write text the picture. with imshow picture is yellow, when i save it, it is blue. i can try change rgb but i couldn't. it is important for my nephew. (his birthday) how can i fix it?

from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw 
import cv2
import numpy as np
import pandas as pd
df=pd.read_csv('atadavetlistesi.csv',encoding= 'UTF-8')
for i in df["isimler"] :

    picture = cv2.imread('atadogumgunu.png')
    pil_im = Image.fromarray(picture)
    draw = ImageDraw.Draw(pil_im)
    font = ImageFont.truetype("comici.ttf", 28, encoding="UTF-8")  
    draw.text((170, 240), i, (60,60,150), font=font)      
    # cv2_text_im = cv2.cvtColor(np.array(pil_im), cv2.COLOR_BGR2BGRA)
    # cv2_text_im = cv2.cvtColor(np.array(pil_im), cv2.COLOR_BGR2RGB)
    cv2_text_im = np.array(pil_im)
    cv2.imshow("Resim",cv2_text_im)
    saved_im=Image.fromarray(cv2_text_im)
    saved_im=saved_im.save(i ".jpg")

added text picture orijinal picture

CodePudding user response:

You read your image from disk with OpenCV here:

picture = cv2.imread('atadogumgunu.png')

which will use BGR order. Then you save it with PIL here:

saved_im=saved_im.save(i ".jpg")

which expects RGB order - so it is bound not to work.


The easiest thing is to load your image with PIL at the start and avoid OpenCV altogether so just use:

pil_im = Image.open('atadogumgunu.png').convert('RGB')
  • Related