回答編集履歴
1
サンプル追加
answer
CHANGED
@@ -21,4 +21,32 @@
|
|
21
21
|

|
22
22
|
|
23
23
|
実行結果
|
24
|
-

|
24
|
+

|
25
|
+
|
26
|
+
|
27
|
+
`Python`コードで透過する文字画像を作成し、重ねて描画するサンプルを書いてみました。
|
28
|
+
```Python
|
29
|
+
from PIL import Image, ImageDraw, ImageFont
|
30
|
+
|
31
|
+
# 透過する文字画像を作成
|
32
|
+
img = Image.new('RGBA', (640,480),(0,0,0,0)) # 適当なサイズの透過画像を生成
|
33
|
+
draw = ImageDraw.Draw(img)
|
34
|
+
font = ImageFont.truetype("arial.ttf", 24) # 適当な大きさのフォントで
|
35
|
+
x,y,max_w = 0,90,0 # 適当な位置から
|
36
|
+
DOCS = ['I am a dog.','As yet I have no name.',"I've no idea where I was born."]
|
37
|
+
for text in DOCS:
|
38
|
+
draw.text((x,y), text, fill=(0,0,0), font=font)
|
39
|
+
w,h = font.getsize(text)
|
40
|
+
max_w = max(max_w, w)
|
41
|
+
y += h
|
42
|
+
img = img.crop((0, 0, max_w, y)) # 画像サイズをテキスト範囲に合わせて調整
|
43
|
+
img.save('name.png')
|
44
|
+
|
45
|
+
# 犬の画像にテキスト画像を重ねる
|
46
|
+
card = Image.open("img.png").convert("RGBA") # 犬
|
47
|
+
#img = Image.open("name.png").convert("RGBA") # 文字
|
48
|
+
x, y = img.size
|
49
|
+
card.paste(img, (0, 0), img)
|
50
|
+
card.save("test.png")
|
51
|
+
```
|
52
|
+

|