Strange ugly black edges of text after using paste #8359
-
from PIL import Image, ImageDraw, ImageFont
font = ImageFont.truetype(r'C:\Users\44483\Desktop\zzz_thins.ttf', size=60)
img = Image.new("RGBA", (200, 200), (200, 160, 130, 0))
img_draw = ImageDraw.Draw(img)
img_draw.text((0, 0), "你好", font=font, fill=(255, 255, 255))
base = Image.new("RGBA", (200, 100), (0, 0, 0, 120))
base_draw = ImageDraw.Draw(base)
base_draw.text((0, 0), "你好", font=font, fill=(255, 255, 255))
base.save("test1.png")
img.paste(base, (0, 100), base)
img.save("test2.png") After paste, the background color seems to fade and there is a strange black edge around the text, I'm not sure what's going on and I'm hoping someone can answer my questions for me!extremely grateful! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 13 replies
-
I can't see the 'strange black edge' in the images you've attached. As to why the background colour is faded, you have provided https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.paste
Because base = Image.new("RGBA", (200, 100), (0, 0, 0, 120)) to base = Image.new("RGBA", (200, 100), (0, 0, 0, 0)) you will see the background disappear entirely. If you do not want the background to fade, then you can just change img.paste(base, (0, 100), base) to img.paste(base, (0, 100)) to stop using a mask in your call to |
Beta Was this translation helpful? Give feedback.
-
I'm sorry to say that I've reorganized a test case, in which it is obvious that the text generated by the pillow seems to be blocked by a circle (about 1px or less). @radarhere pillow(right) from pathlib import Path
from PIL import Image, ImageDraw, ImageFont
path = Path(__file__).parent
font = ImageFont.truetype(str(path / 'zzz_thins.ttf'), size=15)
img1 = Image.open(path / '1.png')
img2 = Image.open(path / '2.png')
img2_draw = ImageDraw.Draw(img2)
img2_draw.text((125, 50), '你好测试', 'white', font, 'mm')
img_temp = Image.new('RGBA', img1.size)
img_temp.paste(img2, (75, 150))
img1 = Image.alpha_composite(img1, img_temp)
img1.save(path / 'test.png') |
Beta Was this translation helpful? Give feedback.
I've created #8369 to accept float stroke widths.