pythonimagepython-imaging-libraryoverlay

Python PIL image text overlay not displayed with expected color on white background image (RGB vs RGBA mode)


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:

Zebra pattern png image white cross pattern png image

Text overlayed image:

Text overlayed zebra pattern png image Text overlayed white cross pattern png image

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,1)#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.

Updated Console logs:

//zebra pattern image
> & C:/ProgramData/Anaconda3/python.exe ./code/txtoverimgv1.py
src_base imguse gen size:  (1920, 1080, 3)
input img file path: D:/testinput/order_image_1.png
src_imguse gen size:  (540, 360)
saved img in path: D:/testinput/updtxtimg/upd_order_image_1.png
<PIL.Image.Image image mode=RGB size=540x360 at 0x1293F0228E0>
saved_img1 done

//white background
> & C:/ProgramData/Anaconda3/python.exe ./code/txtoverimgv1.py
src_base imguse gen size:  (1920, 1080, 3)
input img file path: D:/testinput/order_image_1.png
src_imguse gen size:  (1920, 1080)
saved img in path: D:/testinput/updtxtimg/upd_order_image_1.png
<PIL.Image.Image image mode=RGBA size=1920x1080 at 0x2636D84B280>
saved_img1 done

mode= RGB and RGBA differs for each image but both are png images

Code update: Converting the image to RGB while opening solves the problem, but would like to know best way to work on RGBA images too.

src_img_use = np.array(Image.open( input_subfolder_path + '/' + filename ).convert("RGB") ) 

Solution

  • The reason you are not seeing the text correctly on the RGBA is that you did not define the colors correctly. The text() method's parameter fill expects an integer-valued tuple, so (0,0,0,1) would result in a nearly transparent color (0=fully transparent, 255=fully opaque). Try again with

    font_color_bgra = (0,0,0,255)  # black
    

    This should resolve your issue.

    On the pure RGB image the alpha-component is not considered, thus you do not see the same error on the zebra background image.

    enter image description here