# [Embrace AIGC, apply ChatGPT and OpenAI API] Use OpenAI to generate four-frame comics The content of the book "Embrace AIGC, apply ChatGPT and OpenAI API" is basically related to practical combat. This sharing is to use OpenAI to generate four-frame comics after reading it. It uses OpenAI's dialogue function and drawing function. ## Simple process: 1. Use a sentence as a prompt and let OpenAI expand it. 2. Divide the expanded content into 4 parts. 3. Use the expanded content to call OpenAI's Dall-E-3 to generate comic pictures. ## Implementation process: ### Install the openai extension The SDK provided by openai is mainly for the Python environment. First, install the openai extension for the Python environment: ``` pip install openai ``` ### Instantiate the openai object``` from openai import OpenAI import time import requests import os os.environ["OPENAI_LOG"] = 'debug' # openai parameter definition OPENAI_API_KEY = "sk-cPjd4D89kSsj2dhDkx678DbSi29D01bCqU4PGSW3a7D8kS30" OPENAI_BASE_URL = "https://api.openai.com/v1" client = OpenAI( api_key=OPENAI_API_KEY, base_url=OPENAI_BASE_URL ) ``` It should be noted that OpenAI currently has strict request verification and needs to ensure a stable and reliable operating environment. ### Define drawing-related parameters``` # Image size# ['1024x1024', '1024x1792', '1792x1024'] image_width = 1024 image_height = 1024 image_num = 4 # Style type style = "comic-book" ``` Drawing calls OpenAI's Dall-E-3, which can support more than dozens of styles. The hand-painted style is used here. ### Expand content Call the expand content, directly call the chat interface of OpenAI, provide the appropriate prompt words, and you can expand a sentence into one or more paragraphs. prompt = "Draw a four-frame comic of Zhu Bajie fighting Transformers" messages = [ {"role": "system", "content": "You are a four-frame comic generation assistant"}, {"role": "user","content": prompt} ] completion = client.chat.completions.create( model="gpt-4o-mini", #"gpt-3.5-turbo", messages=messages, temperature=0.7, ) content = completion.choices[0].message.content ``` After calling client.chat.completions.create successfully, the content contains the actual text content returned. The actual run results are as follows: > **First frame**: Zhu Bajie holds a rake in his hand, looks confident, and stands in front of the Transformers. Narration: "Today, I want to challenge this mechanical giant!" > > **Second frame**: Transformers transform, make a roar, and show a tall and mighty posture. Narration: "Watch out, Zhu Bajie!" > > **Third frame**: Zhu Bajie wields a spike rake and bravely rushes towards Transformers, shouting: "I am Bajie, who is afraid of me!" > > **Fourth frame**: Transformers are hit by the spike rake and torn into pieces, and Zhu Bajie looks proud. Narration: "It seems that today is my victory!" ### Generate image call After generating the content, you can call Dall-E-3 to generate the corresponding comic image. ``` prompt2 = "Generate {} pictures of {} style: {}".format( image_num, style, content ) response = client.images.generate( model="dall-e-3", prompt=prompt2, size="{}x{}".format(image_width,image_height), # ['1024x1024', '1024x1792', '1792x1024'] # quality="standard", n=1, ) image_url = response.data[0].url ``` The above code submits the previously expanded content to Dall-E-3 to generate the corresponding comic. After successful generation, a long URL will be returned, which corresponds to the actual generated image. The final generated result is as follows:
## Summary Through the above steps, the OpenAI interface is called to achieve the expansion of content to generate corresponding images. Of course, the prompts in each of the above steps can be further optimized to better convey our intentions to the AI service, so as to obtain output results that are more in line with our expectations. In addition, Python image processing tools will be used in the future to display the files corresponding to each grid on the corresponding comic grid. Although this step can be completed with OpenAI, the effect may not be very good sometimes.