Thu, 09 May 2024 20:50:45 -0500
Remove the media implementation from the Demo protocol plugin
This API will be rewritten in the future, but keeping the old version around
isn't helping anything.
Testing Done:
Dialed TMNT
Reviewed at https://reviews.imfreedom.org/r/3174/
|
41274
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
1 | #!/usr/bin/env python3 |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
2 | # |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
3 | # Purple - Internet Messaging Library |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
4 | # Copyright (C) Pidgin Developers <devel@pidgin.im> |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
5 | # |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
6 | # Purple is the legal property of its developers, whose names are too numerous |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
7 | # to list here. Please refer to the COPYRIGHT file distributed with this |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
8 | # source distribution. |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
9 | # |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
10 | # This program is free software; you can redistribute it and/or modify |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
11 | # it under the terms of the GNU General Public License as published by |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
12 | # the Free Software Foundation; either version 2 of the License, or |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
13 | # (at your option) any later version. |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
14 | # |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
15 | # This program is distributed in the hope that it will be useful, |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
18 | # GNU General Public License for more details. |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
19 | # |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
20 | # You should have received a copy of the GNU General Public License along with |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
21 | # this program; if not, see <https://www.gnu.org/licenses/>. |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
22 | |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
23 | import argparse |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
24 | import colorsys |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
25 | import hashlib |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
26 | from pathlib import Path |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
27 | |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
28 | from PIL import Image, ImageDraw, ImageFont |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
29 | |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
30 | parser = argparse.ArgumentParser( |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
31 | description='Generate buddy icons from user names.') |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
32 | parser.add_argument('name', nargs='+', help='The name(s) to use.') |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
33 | parser.add_argument('-f', '--font', |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
34 | default='/usr/share/fonts/urw-base35/D050000L.otf', |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
35 | help='Path to (TrueType or OpenType) font to use.') |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
36 | parser.add_argument('-s', '--size', default=96, type=int, |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
37 | help='Size of buddy icons to produce.') |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
38 | parser.add_argument('-o', '--output', default='.', |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
39 | help='Directory in which to place files.') |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
40 | args = parser.parse_args() |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
41 | |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
42 | |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
43 | def calculate_colours_for_text(text): |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
44 | """ |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
45 | Calculate the foreground and background colours from text. |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
46 | |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
47 | This is based on pidgin_color_calculate_for_text in Pidgin C code. |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
48 | """ |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
49 | # Hash the string and get the first 2 bytes of the digest. |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
50 | checksum = hashlib.sha1() |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
51 | checksum.update(text.encode('utf-8')) |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
52 | digest = checksum.digest() |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
53 | |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
54 | # Calculate the hue based on the digest, scaled to 0-1. |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
55 | hue = (digest[0] << 8 | digest[1]) / 65535 |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
56 | # Get the RGB values for the hue at full saturation and value. |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
57 | foreground = colorsys.hsv_to_rgb(hue, 1.0, 1.0) |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
58 | |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
59 | # Calculate the hue based on the end of the digest, scaled to 0-1. |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
60 | hue = (digest[-1] << 8 | digest[-2]) / 65535 |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
61 | # Get the RGB values for the hue at full saturation and low value. |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
62 | background = colorsys.hsv_to_rgb(hue, 1.0, 0.2) |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
63 | |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
64 | # Finally calculate the foreground summing 20% of the inverted background |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
65 | # with 80% of the foreground. |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
66 | foreground = ( |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
67 | (0.2 * (1 - bc)) + (0.8 * fc) for bc, fc in zip(background, foreground) |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
68 | ) |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
69 | |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
70 | # Pillow requires colours in 0-255. |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
71 | return (tuple(int(c * 255) for c in foreground), |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
72 | tuple(int(c * 255) for c in background)) |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
73 | |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
74 | |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
75 | output_dir = Path(args.output) |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
76 | font = ImageFont.truetype(args.font, size=int(args.size * 96/72)) |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
77 | |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
78 | for name in args.name: |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
79 | fore, back = calculate_colours_for_text(name) |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
80 | |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
81 | # Generate an image using the first letter of the name to choose a glyph |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
82 | # from the specified font. The default font generates some star-like or |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
83 | # flower-like symbols. |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
84 | img = Image.new('RGBA', (args.size, args.size), color=back) |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
85 | draw = ImageDraw.Draw(img) |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
86 | letter = name[0].upper() |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
87 | draw.text((args.size // 2, args.size - 1), letter, |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
88 | font=font, anchor='mb', fill=fore) |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
89 | |
|
c6bd9ea15211
Add buddy icons in demo protocol
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
diff
changeset
|
90 | img.save(output_dir / f'{name}.png', 'PNG') |