[Python] Python PIL image text overlay not displayed with expected color on white background...

Discussão em 'Python' iniciado por Stack, Março 20, 2025.

  1. Stack

    Stack Membro Participativo

    Python PIL Image not working properly with overlay text image. I am trying to use FPDF image to convert an overlayed text png image to pdf file. However, the overlay text is not in expected colour (looks transparent) on a white background image. However, the same logic works in a zebra pattern background image.

    Source background image:

    [​IMG] [​IMG]

    Text overlayed image:

    [​IMG] [​IMG]

    Code used for overlayed text: Python Image AttributeError: 'ImageDraw' object has no attribute '__array_interface__'

    code:

    fontuse = "D:/Ubuntusharefolder/CustomFonts/EnglishFonts/NotoSans-Medium.ttf"
    font_color_bgra = (0,0,0,0)#black #(0,255,0,0)#green
    font = ImageFont.truetype(fontuse, 32 * int(2), layout_engine=ImageFont.LAYOUT_RAQM)

    src_img_use = np.array(Image.open(os.path.join(input_subfolder_path, filename) ) ) #(511, 898, 4)
    print('src_img_use gen size: ',os.path.join(input_subfolder_path, filename), src_img_use.shape)
    src_img_pil = Image.fromarray(src_img_use)
    print('src_img_pil gen size: ', src_img_use.size)

    img_pil_4dtxt = ImageDraw.Draw(src_img_pil)
    img_pil_4dtxt.text(textpos, subjectuse, font = font, fill = font_color_bgra) #fill = "black" )

    src_img_pil.save(output_folder_final_path + '/updtxtimg/' + str(imgidx) + '_' + filename)
    print('label_img_withtxt drawtext saved subjectuse idx', imgidx, subjectuse)


    Note sure whether there is a problem with input image or overlay code logic. The same image has to be used in FPDF.image but same behavior noticed.

    Continue reading...

Compartilhe esta Página