image manipulate with pillow discord.py
Question:
I’m trying to make a calculate command for discord.py. However, every time I can’t insert an integer number it shows me this error:
Ignoring exception in command crafting:
Traceback (most recent call last):
File "/home/runner/lmcbot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 380, in crafting
draw.text((355,565), copper_rod, (37, 35, 32), font=font)
File "/home/runner/lmcbot/venv/lib/python3.8/site-packages/PIL/ImageDraw.py", line 373, in text
if self._multiline_check(text):
File "/home/runner/lmcbot/venv/lib/python3.8/site-packages/PIL/ImageDraw.py", line 348, in _multiline_check
return split_character in text
TypeError: argument of type 'int' is not iterable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/runner/lmcbot/venv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/home/runner/lmcbot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/home/runner/lmcbot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: argument of type 'int' is not iterable
Here is the code:
@client.command()
async def crafting(ctx, num: int):
img = Image.open("gearboxcrafting.png")
font = ImageFont.truetype("Bold-X3RG.ttf", 30)
copper_rod = num * 10
steel_bolt = num * 50
steel_plate = num * 25
draw = ImageDraw.Draw(img)
draw.text((355,565), copper_rod, (37, 35, 32), font=font)
draw.text((355,656), steel_bolt, (37, 35, 32), font=font)
draw.text((355,747), steel_plate, (37, 35, 32), font=font)
img.save("test.png")
await ctx.send(file = discord.File("test.png"))
the problem is that every time I insert an iterable it says and error
Answers:
You’re passing an int value to a function that wants a string.
Try changing the code to:
draw.text((355,565), str(copper_rod), fill=(37, 35, 32), font=font)
draw.text((355,656), str(steel_bolt), fill=(37, 35, 32), font=font)
draw.text((355,747), str(steel_plate), fill=(37, 35, 32), font=font)
I’m trying to make a calculate command for discord.py. However, every time I can’t insert an integer number it shows me this error:
Ignoring exception in command crafting:
Traceback (most recent call last):
File "/home/runner/lmcbot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 380, in crafting
draw.text((355,565), copper_rod, (37, 35, 32), font=font)
File "/home/runner/lmcbot/venv/lib/python3.8/site-packages/PIL/ImageDraw.py", line 373, in text
if self._multiline_check(text):
File "/home/runner/lmcbot/venv/lib/python3.8/site-packages/PIL/ImageDraw.py", line 348, in _multiline_check
return split_character in text
TypeError: argument of type 'int' is not iterable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/runner/lmcbot/venv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/home/runner/lmcbot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/home/runner/lmcbot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: argument of type 'int' is not iterable
Here is the code:
@client.command()
async def crafting(ctx, num: int):
img = Image.open("gearboxcrafting.png")
font = ImageFont.truetype("Bold-X3RG.ttf", 30)
copper_rod = num * 10
steel_bolt = num * 50
steel_plate = num * 25
draw = ImageDraw.Draw(img)
draw.text((355,565), copper_rod, (37, 35, 32), font=font)
draw.text((355,656), steel_bolt, (37, 35, 32), font=font)
draw.text((355,747), steel_plate, (37, 35, 32), font=font)
img.save("test.png")
await ctx.send(file = discord.File("test.png"))
the problem is that every time I insert an iterable it says and error
You’re passing an int value to a function that wants a string.
Try changing the code to:
draw.text((355,565), str(copper_rod), fill=(37, 35, 32), font=font)
draw.text((355,656), str(steel_bolt), fill=(37, 35, 32), font=font)
draw.text((355,747), str(steel_plate), fill=(37, 35, 32), font=font)