This project is an AI-driven engine that automatically transforms a text manuscript into a series of visually appealing images, perfect for social media carousels on platforms like Xiaohongshu.
It takes a simple text file as input, uses the DeepSeek AI model to intelligently identify key sentences and phrases for styling, and generates a set of polished images including a cover, content pages, and a back cover, based on HTML templates.
- Automated Page Generation: Creates a full set of images (cover, content, back cover) from a single text file.
- AI-Powered Styling: Automatically identifies and styles "golden sentences," "bold phrases," and "viewpoint statements" to create visual interest using the DeepSeek API.
- AI-Powered Content Extraction: Automatically extracts the author's name and core keywords from the article content.
- Template-Based: Uses HTML and CSS for easy and flexible layout and styling. All pages are generated by rendering an HTML template and capturing it with a headless browser.
- Configurable: Key details like character portraits are easily configured in a
config.jsonfile. - Robust Text Pagination: Intelligently splits content across multiple pages, ensuring that sentences and paragraphs flow correctly without being awkwardly cut off.
- Bundled Font: Includes a local font file to ensure that text measurement and rendering are perfectly consistent, eliminating cross-platform rendering issues.
├── fonts/
│ └── NotoSansSC-Regular.otf # Bundled font for consistent rendering
├── output/ # Generated images are saved here
├── template/ # Contains image assets for templates
├── templates/ # Contains HTML templates (not used in final version)
├── .env # Environment variable file for API keys
├── config.json # Configuration for character portrait, etc.
├── content.txt # Your input text file
├── image_generator.py # Handles HTML template rendering and screenshotting
├── llm_handler.py # Manages interaction with the AI model
├── main.py # The main script to run the generator
├── requirements.txt # Python dependencies
├── text_utils.py # Handles text measurement and pagination logic
└── Readme.md # This file
- Python 3.9+
- Git
- A DeepSeek API Key. You can get one from https://platform.deepseek.com/
-
Clone the repository:
git clone <repository-url> cd <repository-directory>
-
Create a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate`
-
Install dependencies:
pip install -r requirements.txt
-
Install Playwright browsers: The script uses Playwright to render the images in a headless browser. You need to install the necessary browser binaries for it to work.
playwright install
-
Set up Environment Variables: This project requires a DeepSeek API key. For security, it's best to use an environment variable.
Create a file named
.envin the root of the project directory. Add your API key to this file:DEEPSEEK_API_KEY="your_deepseek_api_key" -
Configure Your Details: Open
config.jsonand edit the values:{ "character_portrait_path": "template/people.png" }character_portrait_path: The file path to the portrait/character image used on the cover and back cover.
-
Provide Input Text: Open
content.txtand add your content. The first line will be used as the title for the cover page, and the rest of the content will be paginated across the content pages. -
Run the Script: Execute the main script from your terminal:
python main.py
-
Find Your Images: The generated images will be saved sequentially in the
output/directory (e.g.,sop_1.png,sop_2.png, etc.).
The script works in the following steps:
- Reads Input: Reads the content from
content.txt. - Analyzes Text: Sends the text to the DeepSeek API to identify key sentences and phrases.
- Paginates Content: Splits the text into pages, ensuring that sentences are not cut off.
- Generates HTML: Injects the text and styling into HTML templates.
- Renders Images: Uses Playwright to render the HTML and take screenshots of each page, saving them to the
output/directory.
You can customize the look and feel of the generated images by editing the HTML and CSS files in the templates/ directory. The script uses Jinja2 for templating, so you can modify the templates to change the layout, fonts, colors, and more.