Your link previews are blank because og:image is missing. Here is a stdlib-only Python fix
When you paste a link to your product, docs page or side project into Slack, Discord, X or LinkedIn, the platform builds a preview card from your page's Open Graph tags. If og:image is missing or points at a generic platform default, the preview is a grey box or a stock logo. That hurts clicks, and you usually don't notice because you never see your own link the way other people do. This post…
When you link your product, documentation or personal project to Slack, Discord, X or LinkedIn, the platform generates a preview card using your Open Graph tags. If the og:image tag is absent or points to a default image, the preview appears as a grey box or stock logo. This can negatively impact click-through rates, as users cannot see your actual content.
This article outlines three ways to address this issue: a quick check for missing og:image, an all-standard-library Python script to generate a 1280x720 cover image, and how to integrate the image into your webpage.
To verify if your page has an og:image, run this command: curl -s https://your-site.example/page | grep -io meta[^ ]*og:image[^ ]. Three possible outcomes: no output, indicating the absence of an og:image; a generic URL, such as your host's or storefront's logo; or a unique image specific to your page. Additionally, you can check og:title and og:description in the same manner.
To create a PNG image using only the Python standard library, refer to the following code snippet:
```python
import struct
import zlib
def _chunk(kind, data):
body = kind + data
return (struct.pack(I, len(data)) + body + struct.pack(I, zlib.crc32(body) & 0xFFFFFFFF))
def write_png(path, width, height, rows):
raw = b'\x00' + b''.join(rows)
ihdr = struct.pack(IIBBBBB, width, height, 8, 2, 0, 0, 0)
png = b'\x89PNG\r\n\x1a\n' + _chunk(b'IHDR', ihdr) + _chunk(b'IDAT', zlib.compress(raw, 9)) + _chunk(b'IEND', b'')
with open(path, 'wb') as f:
f.write(png)
```
Use this script to create a diagonal two-color gradient with a solid accent bar, which looks far more engaging than a grey box. The gradient can be customized by adjusting the color values for the colors, c1 and c2, and accent.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.