This repository was archived by the owner on Dec 26, 2022. It is now read-only.
-
Couldn't load subscription status.
- Fork 33
🚸 new example with PIL #241
Merged
Merged
Changes from 30 commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
04c5de6
:sparkles: new example with an Image generator
drawbu 3f7bee5
Merge pull request #1 from Pincer-org/main
drawbu 01f4a82
Merge remote-tracking branch 'origin/main'
drawbu 0bbe208
Merge branch 'Pincer-org:main' into main
drawbu f70f7e5
Merge remote-tracking branch 'origin/main'
drawbu caf79e7
Merge branch 'Pincer-org:main' into main
drawbu ee4e5df
Merge remote-tracking branch 'origin/main'
drawbu ff22f93
Merge branch 'Pincer-org:main' into main
drawbu 5f72a0a
Merge remote-tracking branch 'origin/main'
drawbu 2b17668
:lipstick: changed raw user ping to an nice `@ping` thing
drawbu 1482075
:recycle: download avatar with Pincer method
drawbu 72cd529
:heavy_minus_sign: unused dependencies: `io`, `urllib.request`
drawbu c325485
:bug: argument were not passed
Sigmanificient a60d18e
Update examples/tweet_generator.py
Sigmanificient 2a0f4e0
Update examples/tweet_generator.py
Sigmanificient 8356852
:art: fixing formatting
drawbu 83cef97
moved everything in a subfolder and the resources use _ instead of sp…
drawbu 7cad52e
Merge branch 'Pincer-org:main' into main
drawbu c1ec274
Update examples/tweet_generator/tweet_generator.py
Sigmanificient 5f3eb2d
Update examples/tweet_generator/tweet_generator.py
Sigmanificient 0fbfbaf
Update examples/tweet_generator/tweet_generator.py
Sigmanificient 7984bcc
Update examples/tweet_generator/tweet_generator.py
Sigmanificient 7db46ac
Update examples/tweet_generator/tweet_generator.py
Sigmanificient 43e56d6
Revert "Update examples/tweet_generator/tweet_generator.py"
Sigmanificient 75be6bd
:bug: changed to font used
drawbu 1110ff6
:ambulance: hide my token
drawbu 7a945f2
Merge branch 'Pincer-org:main' into main
drawbu 5b00ca2
:fire: delete `name=twitter` line requested by Endercheif
drawbu 20e3696
:art: using f-string
drawbu 441bc40
:bug: using composite commands
drawbu 9b8e8ce
:fire: re-hide my token...
drawbu ca90b4f
:art: requested changes in `trans_paste` requested by Lunarmagpie
drawbu a17b8c6
:see_no_evil: improve syntax
drawbu 345d018
:art: extract the full tweet creation to another method to shorter th…
drawbu efb9259
:art: each parameter on a separate line, the line's too long https://…
drawbu c23b4d5
:art: merge into 1 line https://github.com/Pincer-org/Pincer/pull/241…
drawbu b6dc77c
:art: simplified list's square brackets usage
drawbu f009ec5
Update examples/tweet_generator/tweet_generator.py
drawbu 46c4979
Update examples/tweet_generator/tweet_generator.py
drawbu 3bb5ca8
Update examples/tweet_generator/tweet_generator.py
drawbu 8379414
:art: split into multiple lines, the line's too long https://github.c…
drawbu 112ed8d
:art: split into multiple lines https://github.com/Pincer-org/Pincer/…
drawbu 9756eab
Update examples/tweet_generator/tweet_generator.py
drawbu 9350a92
:wheelchair: add a print statement with the instructions if the user …
drawbu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import re | ||
| import textwrap | ||
| import os | ||
| import sys | ||
| from datetime import datetime | ||
| from PIL import Image, ImageFont, ImageDraw, ImageOps | ||
| from pincer import command, Client | ||
| from pincer.commands import CommandArg, Description | ||
| from pincer.objects import Message, Embed, MessageContext | ||
|
|
||
|
|
||
| # you need to manually download the font files and put them into the folder | ||
| # ./examples/tweet_generator/ to make the script works using this link: | ||
| # https://fonts.google.com/share?selection.family=Noto%20Sans:wght@400;700 | ||
| if not all(font in os.listdir() for font in ["NotoSans-Regular.ttf", "NotoSans-Bold.ttf"]): | ||
drawbu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| sys.exit() | ||
drawbu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| class Bot(Client): | ||
| @Client.event | ||
| async def on_ready(self): | ||
| print( | ||
| f"Started client on {self.bot}\n" | ||
| f"Registered commands: {', '.join(self.chat_commands)}" | ||
| ) | ||
|
|
||
| @command( | ||
| description="to create fake tweets", | ||
| guild=690604075775164437 | ||
| ) | ||
| async def twitter( | ||
| self, ctx: MessageContext, content: CommandArg[str, Description["The content of the message"]] | ||
drawbu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ): | ||
| await ctx.interaction.ack() | ||
|
|
||
| message = content | ||
| for text_match, user_id in re.findall( | ||
| re.compile(r"(<@!(\d+)>)"), message | ||
| ): | ||
| message = message.replace( | ||
| text_match, f"@{await self.get_user(user_id)}" | ||
| ) | ||
|
|
||
| if len(message) > 280: | ||
| return "A tweet can be at maximum 280 characters long" | ||
|
|
||
| # wrap the message to be multi-line | ||
| message = textwrap.wrap(message, 38) | ||
|
|
||
| # download the profile picture and convert it into Image object | ||
| avatar = (await ctx.author.user.get_avatar()).resize((128, 128)) | ||
|
|
||
| # modify profile picture to be circular | ||
| mask = Image.new("L", (128, 128), 0) | ||
| draw = ImageDraw.Draw(mask) | ||
| draw.ellipse((0, 0, 128, 128), fill=255) | ||
| avatar = ImageOps.fit(avatar, mask.size, centering=(0.5, 0.5)) | ||
| avatar.putalpha(mask) | ||
|
|
||
| # create the tweet by pasting the profile picture into a white image | ||
| tweet = trans_paste( | ||
| avatar, | ||
| # background | ||
| Image.new("RGBA", (800, 250 + 50 * len(message)), (255, 255, 255)), | ||
| box=(15, 15), | ||
| ) | ||
|
|
||
| # add the fonts | ||
| font = ImageFont.truetype("NotoSans-Regular.ttf", 40) | ||
| font_small = ImageFont.truetype("NotoSans-Regular.ttf", 30) | ||
| font_bold = ImageFont.truetype("NotoSans-Bold.ttf", 40) | ||
|
|
||
| # write the name and username on the Image | ||
| draw = ImageDraw.Draw(tweet) | ||
| draw.text( | ||
| (180, 20), str(ctx.author.user), fill=(0, 0, 0), font=font_bold | ||
| ) | ||
| draw.text( | ||
| (180, 70), | ||
| "@" + ctx.author.user.username, | ||
| fill=(120, 120, 120), | ||
| font=font, | ||
| ) | ||
|
|
||
| # write the content of the tweet on the Image | ||
| message = "\n".join(message).split(" ") | ||
Sigmanificient marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| result = [] | ||
|
|
||
| # generate a dict to set were the text need to be in different color. | ||
| # for example, if a word starts with '@' it will be write in blue. | ||
| # example: | ||
| # [ | ||
| # {'color': (0, 0, 0), 'text': 'hello world '}, | ||
| # {'color': (0, 154, 234), 'text': '@drawbu'} | ||
| # ] | ||
| for word in message: | ||
| for index, text in enumerate(word.splitlines()): | ||
|
|
||
| text += "\n" if index != len(word.split("\n")) - 1 else " " | ||
|
|
||
| if not result: | ||
| result.append({"color": (0, 0, 0), "text": text}) | ||
| continue | ||
|
|
||
| if not text.startswith("@"): | ||
| if result[-1:][0]["color"] == (0, 0, 0): | ||
| result[-1:][0]["text"] += text | ||
| continue | ||
|
|
||
| result.append({"color": (0, 0, 0), "text": text}) | ||
| continue | ||
|
|
||
| result.append({"color": (0, 154, 234), "text": text}) | ||
|
|
||
| # write the text | ||
| draw = ImageDraw.Draw(tweet) | ||
| x = 30 | ||
| y = 170 | ||
| for text in result: | ||
| y -= font.getsize(" ")[1] | ||
| for l_index, line in enumerate(text["text"].splitlines()): | ||
| if l_index: | ||
| x = 30 | ||
| y += font.getsize(" ")[1] | ||
| draw.text((x, y), line, fill=text["color"], font=font) | ||
| x += font.getsize(line)[0] | ||
|
|
||
| # write the footer | ||
| draw.text( | ||
| (30, tweet.size[1] - 60), | ||
| datetime.now().strftime( | ||
| "%I:%M %p · %d %b. %Y · Twitter for Discord" | ||
| ), | ||
| fill=(120, 120, 120), | ||
| font=font_small, | ||
| ) | ||
|
|
||
| return Message( | ||
| embeds=[ | ||
| Embed(title="Twitter for Discord", description="").set_image( | ||
drawbu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| url="attachment://image0.png" | ||
| ) | ||
| ], | ||
| attachments=[tweet], | ||
| ) | ||
|
|
||
|
|
||
| # https://stackoverflow.com/a/53663233/15485584 | ||
| def trans_paste(fg_img, bg_img, alpha=1.0, box=(0, 0)): | ||
| """ | ||
| paste an image into one another | ||
| """ | ||
| fg_img_trans = Image.new("RGBA", fg_img.size) | ||
| fg_img_trans = Image.blend(fg_img_trans, fg_img, alpha) | ||
| bg_img.paste(fg_img_trans, box, fg_img_trans) | ||
| return bg_img | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| # Of course we have to run our client, you can replace the | ||
| # XXXYOURBOTTOKENHEREXXX with your token, or dynamically get it | ||
| # through a dotenv/env. | ||
| Bot("OTA1MzczMDYwMjcyNjMxODI4.YYJIXg.oegS9NPilP449bk_BCKIpYWjq5Y").run() | ||
drawbu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.