LoginSignup
2
0

More than 1 year has passed since last update.

discordでmake it a quoteをしたい(python)

Last updated at Posted at 2022-04-19

make it a quoteをdiscordpyでやりたい!

twitterにmake it a quoteというbotがいる。
これをdiscordでやりたいと思った。

まずすること

•discord.pyと画像処理用のpillowのインストール
pip install discord.py
pip install pillow

コード

botの基本部分

画像は最後にのせておきます

import discord
from discord.ext import commands
from PIL import Image,ImageDraw,ImageFont
client=commands.Bot(command_prefix="^")
client.run(token)

まずbotにコマンドを付けずにそのままやってみる

from PIL import Image,ImageDraw,ImageFont
def adjust(content):
  msg=""
  for con in content:
    if len(msg)%9==8:
      msg+="\n"
    msg+=con
  return msg
#文字を8文字ごとに改行する
icon=Image.open("icon.jpeg")
background=Image.open("back.jpeg")
w,h=(680,370)#画像の大きさを揃えるため
background=background.convert("L")#白黒に変更
background=background.resize((w,h))#リサイズ
new=Image.new(mode="L",size=(w,h))#後でアイコンを貼り付ける
icon=icon.resize((h,h))#discordでは画像が正方形で返ってくるから
icon=icon.convert("L")
new.paste(icon)#貼り付ける

ここで困ったのがどのようにグラデーションを付けるかということ。
Image.blend()をしても白の部分で画像が明るくなってしまい、さらに画像に境目ができてしまう。
調べたところ,Image.composite()が使えそうだった。

続き

black=Image.open("black.jpeg")
black=black.convert("L")
result=Image.composite(new,black,background)

ここまでで
C1F1DA57-7989-4781-8380-619B9A15DE45.png
こうなると思います。

次に文字をつけていきたいと思います

draw=ImageDraw.Draw(result)
fnt=ImageFont.truetype("#ここにはフォントをダウンロードしてください。おすすめはにじみ明朝というやつです",40)#フォントサイズ
draw.text((350,120),adjust("hahahahahaha"),font=fnt,fill="#FFF")
#文字の位置、テキスト、フォント、色
result.save("result.png")

ここまでくると
8CAC0894-95F0-4C5A-8AD4-9939B5CD9B1A.png
こうなると思います。

botのコマンドに追加

一番上のコードの続き
返信先を受け取るときはdiscord.Message.referenceで受け取ります
これを探すのに時間がかかった

import requests
@client.command()
async def make_quote(ctx):
  msg=ctx.message.refernce
  id=msg.message_id
  message=await ctx.fetch_message(id)
  ico=message.author.avatar_url
  #アバターを保存する方法がわからなかったのでurlから保存しています
  url=ico
  fp="icon.jpeg"
  response=requests.get(url)
  image=response.content
  with open(fp,"wb") as aaa:
    aaa.write(image)

  def adjust(content):
      msg=""
      for con in content:
        if len(msg)%9==8:
          msg+="\n"
        msg+=con
      return msg

  icon=Image.open("icon.jpeg")
  background=Image.open("back.jpeg")
  black=Image.open("black.jpeg")
  w,h=(680,370)
  background=background.convert("L")
  background=background.resize((w,h))
  black=black.resize((w,h))
  new=Image.new(mode="L",size=(w,h))
  icon=icon.resize((h,h))
  icon=icon.crop((40,0,680,370))
#画像の左にアイコンの中心が来るよう調整
  icon=icon.convert("L")
  new.paste(icon)
  black=black.convert("L")
  result=Image.composite(new,black,background)
  draw = ImageDraw.Draw(result)
  fnt = ImageFont.truetype('nijimi.otf',40)
  cont=message.content
  author=message.author
  draw.text((350,120),adjust(cont),font=fnt,fill='#FFF') #fontを指定
  dr=ImageDraw.Draw(sa)
  font = ImageFont.truetype('nijimi.otf',25)
  dr.text((400,300),str(author),font=font,fill="#FFF")
  #authorの名前も入れます
  result.save("t.png")
  file=discord.File("t.png")
  await ctx.send(file=file)

こうすると
5FA2C2B1-C6BD-404B-87D1-8976676DA8CF.jpeg
のようになると思います。
切れ方がおかしいですがそこは文節で区切るbudouなどのパッケージを使ってもいいです
(名前は隠しています)
back.jpeg↓
888F8AA9-D1B4-4329-8F8A-0F2F23AE9B87.png
icon.jpeg↓
0D1C71FE-66E7-43A8-BC0F-3CB7E7EB62C1.jpeg
black.jpeg↓
5CCCB97F-C86C-477C-AF96-AC17B4A244D3.jpeg

最後に

なかなかうまく再現できたんじゃないかと思います。
マークダウンが色々おかしいですが初めてなのでお願いします。
一応テスト用に作ったコードのせておきます。

from PIL import Image,ImageDraw,ImageFont
def adjust(content):
  msg=""
  for con in content:
    if len(msg)%9==8:
      msg+="\n"
    msg+=con
  return msg
icon=Image.open("yamato.jpeg")
haikei=Image.open("grad.jpeg")
black=Image.open("black.jpeg")
print(haikei.size)
w,h=(680,370)
w1,h1=icon.size
haikei=haikei.convert("L")
haikei=haikei.resize((w,h))
black=black.resize((w,h))
new=Image.new(mode="L",size=(w,h))
icon=icon.resize((h,h))
icon=icon.convert("L")
new.paste(icon)
black=black.convert("L")
sa=Image.composite(new,black,haikei)
draw = ImageDraw.Draw(sa)# im上のImageDrawインスタンスを作る
fnt = ImageFont.truetype('nijimi.otf',40) #ImageFontインスタンスを作る
draw.text((350,120),adjust("hahahahahahahahah"),font=fnt,fill='#FFF') #fontを指定
sa.save("test.png")
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0