Open main menu
首页
专栏
课程
分类
归档
Chat
Sci-Hub
谷歌学术
Libgen
GitHub镜像
登录/注册
搜索
搜索
关闭
Previous
Previous
Next
Next
OpenAI-ChatGPT最新官方接口《AI绘图》全网最详细中英文实用指南和教程,助你零基础快速轻松掌握全新技术(三)(附源码)
sockstack
/
144
/
2023-11-07 00:01:49
<p><span style="color: red; font-size: 18px">ChatGPT 可用网址,仅供交流学习使用,如对您有所帮助,请收藏并推荐给需要的朋友。</span><br><a href="https://ckai.xyz/?sockstack§ion=detail" target="__blank">https://ckai.xyz</a><br><br></p> <article class="baidu_pl"><div id="article_content" class="article_content clearfix"> <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css"> <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-25cebea3f9.css"> <div id="content_views" class="markdown_views prism-atom-one-dark"> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg><p></p> <div class="toc"> <h3>ChatGPT-AI绘图</h3> <ul> <li>Image generation <sup>Beta</sup> 图片生成</li> <li>前言</li> <li>Introduction</li> <li>Usage</li> <li><ul> <li>Generations</li> <li>Edits 编辑</li> </ul></li> <li>Variations</li> <li>Language-specific tips 特定语言提示</li> <li><ul> <li>Python 语言</li> <li><ul> <li>Using in-memory image data 使用内存中的图像数据</li> <li>Operating on image data 操作图像数据</li> <li>Error handling</li> </ul></li> <li>Node.js 语言</li> <li><ul> <li>Using in-memory image data 使用内存中的图像数据</li> <li>Working with TypeScript 使用TypeScript</li> <li>Error handling</li> </ul></li> </ul></li> <li>其它资料下载</li> </ul> </div> <p></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/7ade8f585aac4f0896444d2cb39cbe0b.jpeg#pic_center" alt="在这里插入图片描述"></p> <h1> <a id="Image_generation_supBetasup__6"></a>Image generation <sup>Beta</sup> 图片生成</h1> <p>Learn how to generate or manipulate images with our DALL·E models<br> 了解如何使用我们的DALL·E模型生成或操作图像</p> <h1> <a id="_9"></a>前言</h1> <p>ChatGPT不仅仅是一个强大的自然语言处理模型,其DALL·E模型还可以帮助用户快速生成多种多样的图像。用户可以根据文字快速生成图片,即可自动生成出特定的图片,大大提高了工作效率。对于多媒体行业从业者来说,DALL·E模型强大的图像处理能力,可以帮助用户节省大量时间,让用户更加专注地精心制作自己的作品,从而提高工作效率。</p> <h1> <a id="Introduction_13"></a>Introduction</h1> <p>The Images API provides three methods for interacting with images:<br> 图像API提供了三种与图像交互的方法:</p> <ol> <li>Creating images from scratch based on a text prompt<br> 基于文本提示从头开始创建图像</li> <li>Creating edits of an existing image based on a new text prompt<br> 基于新文本提示创建现有图像的编辑</li> <li>Creating variations of an existing image<br> 创建现有图像的变体</li> </ol> <p>This guide covers the basics of using these three API endpoints with useful code samples. To see them in action, check out our DALL·E preview app.<br> 本指南涵盖了使用这三个API端点的基础知识和有用的代码示例。要查看它们的实际操作,请查看我们的DALL·E预览应用程序。</p> <blockquote> <p>The Images API is in beta. During this time the API and models will evolve based on your feedback. To ensure all users can prototype comfortably, the default rate limit is 50 images per minute. You can learn more about rate limits in our rate limit guide.<br> 图像API处于beta阶段。在此期间,API和模型将根据您的反馈进行改进。为了确保所有用户都能轻松地进行原型制作,默认速率限制为每分钟50张图像。您可以在我们的费率限制指南中了解有关费率限制的更多信息。</p> </blockquote> <h1> <a id="Usage_30"></a>Usage</h1> <h2> <a id="Generations_31"></a>Generations</h2> <p>The image generations endpoint allows you to create an original image given a text prompt. Generated images can have a size of 256x256, 512x512, or 1024x1024 pixels. Smaller sizes are faster to generate. You can request 1-10 images at a time using the n parameter.<br> 图像生成端点允许您在给出文本提示的情况下创建原始图像。生成的图像可以具有256 x256、512 x512或1024 x1024像素的大小。较小的尺寸生成速度更快。您可以使用n参数一次请求1-10个图像。</p> <p>python代码如下:</p> <pre><code class="prism language-python">response <span class="token operator">=</span> openai<span class="token punctuation">.</span>Image<span class="token punctuation">.</span>create<span class="token punctuation">(</span>prompt<span class="token operator">=</span><span class="token string">"a white siamese cat"</span><span class="token punctuation">,</span>n<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>size<span class="token operator">=</span><span class="token string">"1024x1024"</span> <span class="token punctuation">)</span> image_url <span class="token operator">=</span> response<span class="token punctuation">[</span><span class="token string">'data'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'url'</span><span class="token punctuation">]</span> </code></pre> <p>node.js代码如下:</p> <pre><code class="prism language-javascript"><span class="token keyword">const</span> response <span class="token operator">=</span> <span class="token keyword">await</span> openai<span class="token punctuation">.</span><span class="token function">createImage</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span><span class="token literal-property property">prompt</span><span class="token operator">:</span> <span class="token string">"a white siamese cat"</span><span class="token punctuation">,</span><span class="token literal-property property">n</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span><span class="token literal-property property">size</span><span class="token operator">:</span> <span class="token string">"1024x1024"</span><span class="token punctuation">,</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> image_url <span class="token operator">=</span> response<span class="token punctuation">.</span>data<span class="token punctuation">.</span>data<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>url<span class="token punctuation">;</span> </code></pre> <p>curl 代码如下:</p> <pre><code class="prism language-bash"><span class="token function">curl</span> https://api.openai.com/v1/images/generations <span class="token punctuation">\</span>-H <span class="token string">"Content-Type: application/json"</span> <span class="token punctuation">\</span>-H <span class="token string">"Authorization: Bearer <span class="token variable">$OPENAI_API_KEY</span>"</span> <span class="token punctuation">\</span>-d <span class="token string">'{"prompt": "a white siamese cat","n": 1,"size": "1024x1024"}'</span> </code></pre> <p>The more detailed the description, the more likely you are to get the result that you or your end user want. You can explore the examples in the DALL·E preview app for more prompting inspiration. Here’s a quick example:<br> 描述越详细,就越有可能获得您或最终用户想要的结果。您可以在DALL·E预览应用程序中探索示例,以获得更多提示灵感。这里有一个简单的例子:</p> <p>PROMPT 提示</p> <blockquote> <p>a white siamese cat 白色暹罗猫</p> </blockquote> <p>GENERATION 产生<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/e4ef237228a140139795c140fc4f5f07.png" alt="在这里插入图片描述"></p> <p>PROMPT 提示</p> <blockquote> <p>a close up, studio photographic portrait of a white siamese cat that looks curious, backlit ears<br> 一个近距离,工作室摄影肖像的白色暹罗猫,看起来好奇,背光的耳朵</p> </blockquote> <p>GENERATION 产生<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/861d666dfd76480b900283a294c69019.png" alt="在这里插入图片描述"><br> Each image can be returned as either a URL or Base64 data, using the response_format parameter. URLs will expire after an hour.<br> 使用response_format参数,每个图像都可以作为URL或Base64数据返回。URL将在一小时后过期。</p> <h2> <a id="Edits__84"></a>Edits 编辑</h2> <p>The image edits endpoint allows you to edit and extend an image by uploading a mask. The transparent areas of the mask indicate where the image should be edited, and the prompt should describe the full new image, not just the erased area. This endpoint can enable experiences like the editor in our DALL·E preview app.<br> 图像编辑端点允许您通过上传遮罩来编辑和扩展图像。蒙版的透明区域指示应该编辑图像的位置,提示应该描述完整的新图像,而不仅仅是擦除的区域。此端点可以实现类似于DALL·E预览应用中的编辑器的体验。</p> <p>python代码如下:</p> <pre><code class="prism language-python">response <span class="token operator">=</span> openai<span class="token punctuation">.</span>Image<span class="token punctuation">.</span>create_edit<span class="token punctuation">(</span>image<span class="token operator">=</span><span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">"sunlit_lounge.png"</span><span class="token punctuation">,</span> <span class="token string">"rb"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>mask<span class="token operator">=</span><span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">"mask.png"</span><span class="token punctuation">,</span> <span class="token string">"rb"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>prompt<span class="token operator">=</span><span class="token string">"A sunlit indoor lounge area with a pool containing a flamingo"</span><span class="token punctuation">,</span>n<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>size<span class="token operator">=</span><span class="token string">"1024x1024"</span> <span class="token punctuation">)</span> image_url <span class="token operator">=</span> response<span class="token punctuation">[</span><span class="token string">'data'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'url'</span><span class="token punctuation">]</span> </code></pre> <p>node.js代码如下:</p> <pre><code class="prism language-javascript"><span class="token keyword">const</span> response <span class="token operator">=</span> <span class="token keyword">await</span> openai<span class="token punctuation">.</span><span class="token function">createImageEdit</span><span class="token punctuation">(</span>fs<span class="token punctuation">.</span><span class="token function">createReadStream</span><span class="token punctuation">(</span><span class="token string">"sunlit_lounge.png"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>fs<span class="token punctuation">.</span><span class="token function">createReadStream</span><span class="token punctuation">(</span><span class="token string">"mask.png"</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">"A sunlit indoor lounge area with a pool containing a flamingo"</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">"1024x1024"</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> image_url <span class="token operator">=</span> response<span class="token punctuation">.</span>data<span class="token punctuation">.</span>data<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>url<span class="token punctuation">;</span> </code></pre> <p>curl 代码如下:</p> <pre><code class="prism language-bash"><span class="token function">curl</span> https://api.openai.com/v1/images/edits <span class="token punctuation">\</span>-H <span class="token string">"Authorization: Bearer <span class="token variable">$OPENAI_API_KEY</span>"</span> <span class="token punctuation">\</span>-F <span class="token assign-left variable">image</span><span class="token operator">=</span><span class="token string">"@sunlit_lounge.png"</span> <span class="token punctuation">\</span>-F <span class="token assign-left variable">mask</span><span class="token operator">=</span><span class="token string">"@mask.png"</span> <span class="token punctuation">\</span>-F <span class="token assign-left variable">prompt</span><span class="token operator">=</span><span class="token string">"A sunlit indoor lounge area with a pool containing a flamingo"</span> <span class="token punctuation">\</span>-F <span class="token assign-left variable">n</span><span class="token operator">=</span><span class="token number">1</span> <span class="token punctuation">\</span>-F <span class="token assign-left variable">size</span><span class="token operator">=</span><span class="token string">"1024x1024"</span> </code></pre> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/10bbacd17dff4b3ea99e8d6f337e6e8f.png" alt="在这里插入图片描述"><br> The uploaded image and mask must both be square PNG images less than 4MB in size, and also must have the same dimensions as each other. The non-transparent areas of the mask are not used when generating the output, so they don’t necessarily need to match the original image like the example above.<br> 上传的图像和蒙版必须都是小于4MB的方形PNG图像,并且彼此的尺寸必须相同。生成输出时不使用蒙版的非透明区域,因此它们不一定需要像上面的示例那样匹配原始图像。</p> <h1> <a id="Variations_125"></a>Variations</h1> <p>The image variations endpoint allows you to generate a variation of a given image.<br> 图像变体端点允许您生成给定图像的变体。</p> <p>python代码如下:</p> <pre><code class="prism language-python">response <span class="token operator">=</span> openai<span class="token punctuation">.</span>Image<span class="token punctuation">.</span>create_variation<span class="token punctuation">(</span>image<span class="token operator">=</span><span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">"corgi_and_cat_paw.png"</span><span class="token punctuation">,</span> <span class="token string">"rb"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>n<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>size<span class="token operator">=</span><span class="token string">"1024x1024"</span> <span class="token punctuation">)</span> image_url <span class="token operator">=</span> response<span class="token punctuation">[</span><span class="token string">'data'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'url'</span><span class="token punctuation">]</span> </code></pre> <p>node.js代码如下:</p> <pre><code class="prism language-javascript"><span class="token keyword">const</span> response <span class="token operator">=</span> <span class="token keyword">await</span> openai<span class="token punctuation">.</span><span class="token function">createImageVariation</span><span class="token punctuation">(</span>fs<span class="token punctuation">.</span><span class="token function">createReadStream</span><span class="token punctuation">(</span><span class="token string">"corgi_and_cat_paw.png"</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">"1024x1024"</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> image_url <span class="token operator">=</span> response<span class="token punctuation">.</span>data<span class="token punctuation">.</span>data<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>url<span class="token punctuation">;</span> </code></pre> <p>curl 代码如下:</p> <pre><code class="prism language-bash"><span class="token function">curl</span> https://api.openai.com/v1/images/variations <span class="token punctuation">\</span>-H <span class="token string">"Authorization: Bearer <span class="token variable">$OPENAI_API_KEY</span>"</span> <span class="token punctuation">\</span>-F <span class="token assign-left variable">image</span><span class="token operator">=</span><span class="token string">'@corgi_and_cat_paw.png'</span> <span class="token punctuation">\</span>-F <span class="token assign-left variable">n</span><span class="token operator">=</span><span class="token number">1</span> <span class="token punctuation">\</span>-F <span class="token assign-left variable">size</span><span class="token operator">=</span><span class="token string">"1024x1024"</span> </code></pre> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/308b32fa62b543e385c69fb350d56da6.png" alt="在这里插入图片描述"></p> <h1> <a id="Languagespecific_tips__158"></a>Language-specific tips 特定语言提示</h1> <h2> <a id="Python__160"></a>Python 语言</h2> <h3> <a id="Using_inmemory_image_data__161"></a>Using in-memory image data 使用内存中的图像数据</h3> <p>The Python examples in the guide above use the <code>open</code> function to read image data from disk. In some cases, you may have your image data in memory instead. Here’s an example API call that uses image data stored in a <code>BytesIO</code> object:<br> 上面的Python示例使用 <code>open</code> 函数从磁盘读取图像数据。在某些情况下,您可能会将图像数据保存在内存中。下面是一个使用存储在 <code>BytesIO</code> 对象中的图像数据的示例API调用:</p> <pre><code class="prism language-python"><span class="token keyword">from</span> io <span class="token keyword">import</span> BytesIO<span class="token comment"># This is the BytesIO object that contains your image data</span> byte_stream<span class="token punctuation">:</span> BytesIO <span class="token operator">=</span> <span class="token punctuation">[</span>your image data<span class="token punctuation">]</span> byte_array <span class="token operator">=</span> byte_stream<span class="token punctuation">.</span>getvalue<span class="token punctuation">(</span><span class="token punctuation">)</span> response <span class="token operator">=</span> openai<span class="token punctuation">.</span>Image<span class="token punctuation">.</span>create_variation<span class="token punctuation">(</span>image<span class="token operator">=</span>byte_array<span class="token punctuation">,</span>n<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>size<span class="token operator">=</span><span class="token string">"1024x1024"</span> <span class="token punctuation">)</span> </code></pre> <h3> <a id="Operating_on_image_data__178"></a>Operating on image data 操作图像数据</h3> <p>It may be useful to perform operations on images before passing them to the API. Here’s an example that uses <code>PIL</code> to resize an image:<br> 在将图像传递给API之前对图像执行操作可能是有用的。下面是一个使用 <code>PIL</code> 调整图像大小的示例:</p> <pre><code class="prism language-python"><span class="token keyword">from</span> io <span class="token keyword">import</span> BytesIO <span class="token keyword">from</span> PIL <span class="token keyword">import</span> Image<span class="token comment"># Read the image file from disk and resize it</span> image <span class="token operator">=</span> Image<span class="token punctuation">.</span><span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">"image.png"</span><span class="token punctuation">)</span> width<span class="token punctuation">,</span> height <span class="token operator">=</span> <span class="token number">256</span><span class="token punctuation">,</span> <span class="token number">256</span> image <span class="token operator">=</span> image<span class="token punctuation">.</span>resize<span class="token punctuation">(</span><span class="token punctuation">(</span>width<span class="token punctuation">,</span> height<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment"># Convert the image to a BytesIO object</span> byte_stream <span class="token operator">=</span> BytesIO<span class="token punctuation">(</span><span class="token punctuation">)</span> image<span class="token punctuation">.</span>save<span class="token punctuation">(</span>byte_stream<span class="token punctuation">,</span> <span class="token builtin">format</span><span class="token operator">=</span><span class="token string">'PNG'</span><span class="token punctuation">)</span> byte_array <span class="token operator">=</span> byte_stream<span class="token punctuation">.</span>getvalue<span class="token punctuation">(</span><span class="token punctuation">)</span>response <span class="token operator">=</span> openai<span class="token punctuation">.</span>Image<span class="token punctuation">.</span>create_variation<span class="token punctuation">(</span>image<span class="token operator">=</span>byte_array<span class="token punctuation">,</span>n<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>size<span class="token operator">=</span><span class="token string">"1024x1024"</span> <span class="token punctuation">)</span> </code></pre> <h3> <a id="Error_handling_203"></a>Error handling</h3> <p>API requests can potentially return errors due to invalid inputs, rate limits, or other issues. These errors can be handled with a <code>try...except</code> statement, and the error details can be found in <code>e.error</code>:<br> API请求可能由于无效输入、速率限制或其他问题而返回错误。这些错误可以用 <code>try...except</code> 语句处理,错误细节可以在 <code>e.error</code> 中找到:</p> <pre><code class="prism language-python"><span class="token keyword">try</span><span class="token punctuation">:</span>openai<span class="token punctuation">.</span>Image<span class="token punctuation">.</span>create_variation<span class="token punctuation">(</span><span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">"image.png"</span><span class="token punctuation">,</span> <span class="token string">"rb"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>n<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>size<span class="token operator">=</span><span class="token string">"1024x1024"</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>response<span class="token punctuation">[</span><span class="token string">'data'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'url'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">except</span> openai<span class="token punctuation">.</span>error<span class="token punctuation">.</span>OpenAIError <span class="token keyword">as</span> e<span class="token punctuation">:</span><span class="token keyword">print</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span>http_status<span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span>error<span class="token punctuation">)</span> </code></pre> <h2> <a id="Nodejs__219"></a>Node.js 语言</h2> <h3> <a id="Using_inmemory_image_data__221"></a>Using in-memory image data 使用内存中的图像数据</h3> <p>The Node.js examples in the guide above use the <code>fs</code> module to read image data from disk. In some cases, you may have your image data in memory instead. Here’s an example API call that uses image data stored in a Node.js <code>Buffer</code> object:<br> 上述指南中的Node.js示例使用 <code>fs</code> 模块从磁盘读取图像数据。在某些情况下,您可能会将图像数据保存在内存中。下面是一个使用存储在Node.js <code>Buffer</code> 对象中的图像数据的示例API调用:</p> <pre><code class="prism language-javascript"><span class="token comment">// This is the Buffer object that contains your image data 这是包含图像数据的Buffer对象</span> <span class="token keyword">const</span> buffer <span class="token operator">=</span> <span class="token punctuation">[</span>your image data<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// Set a `name` that ends with .png so that the API knows it's a PNG image 设置一个以.png结尾的“名称”,这样API就知道这是一个PNG图像</span> buffer<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">"image.png"</span><span class="token punctuation">;</span> <span class="token keyword">const</span> response <span class="token operator">=</span> <span class="token keyword">await</span> openai<span class="token punctuation">.</span><span class="token function">createImageVariation</span><span class="token punctuation">(</span>buffer<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">"1024x1024"</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> </code></pre> <h3> <a id="Working_with_TypeScript_TypeScript_237"></a>Working with TypeScript 使用TypeScript</h3> <p>If you’re using TypeScript, you may encounter some quirks with image file arguments. Here’s an example of working around the type mismatch by explicitly casting the argument:<br> 如果你使用TypeScript,你可能会遇到一些图像文件参数的怪异。下面是一个通过显式转换参数来解决类型不匹配的示例:</p> <pre><code class="prism language-javascript"><span class="token comment">// Cast the ReadStream to `any` to appease the TypeScript compiler 将ReadStream转换为' any '以便TypeScript编译器使用</span> <span class="token keyword">const</span> response <span class="token operator">=</span> <span class="token keyword">await</span> openai<span class="token punctuation">.</span><span class="token function">createImageVariation</span><span class="token punctuation">(</span>fs<span class="token punctuation">.</span><span class="token function">createReadStream</span><span class="token punctuation">(</span><span class="token string">"image.png"</span><span class="token punctuation">)</span> <span class="token keyword">as</span> any<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">"1024x1024"</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> </code></pre> <p>And here’s a similar example for in-memory image data:<br> 下面是内存中图像数据的类似示例:</p> <pre><code class="prism language-javascript"><span class="token comment">// This is the Buffer object that contains your image data 这是包含图像数据的Buffer对象</span> <span class="token keyword">const</span> <span class="token literal-property property">buffer</span><span class="token operator">:</span> Buffer <span class="token operator">=</span> <span class="token punctuation">[</span>your image data<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// Cast the buffer to `any` so that we can set the `name` property 将缓冲区转换为' any ',以便我们可以设置' name '属性</span> <span class="token keyword">const</span> <span class="token literal-property property">file</span><span class="token operator">:</span> any <span class="token operator">=</span> buffer<span class="token punctuation">;</span> <span class="token comment">// Set a `name` that ends with .png so that the API knows it's a PNG image 设置一个以.png结尾的“名称”,这样API就知道这是一个PNG图像</span> file<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">"image.png"</span><span class="token punctuation">;</span> <span class="token keyword">const</span> response <span class="token operator">=</span> <span class="token keyword">await</span> openai<span class="token punctuation">.</span><span class="token function">createImageVariation</span><span class="token punctuation">(</span>file<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">"1024x1024"</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> </code></pre> <h3> <a id="Error_handling_266"></a>Error handling</h3> <p>API requests can potentially return errors due to invalid inputs, rate limits, or other issues. These errors can be handled with a <code>try...catch</code> statement, and the error details can be found in either <code>error.response</code> or <code>error.message</code>:<br> API请求可能由于无效输入、速率限制或其他问题而返回错误。这些错误可以用 <code>try...catch</code> 语句处理,错误详细信息可以在<code>error.response</code> 或<code>error.message</code> 中找到:</p> <pre><code class="prism language-javascript"><span class="token keyword">try</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">const</span> response <span class="token operator">=</span> <span class="token keyword">await</span> openai<span class="token punctuation">.</span><span class="token function">createImageVariation</span><span class="token punctuation">(</span>fs<span class="token punctuation">.</span><span class="token function">createReadStream</span><span class="token punctuation">(</span><span class="token string">"image.png"</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">"1024x1024"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span>data<span class="token punctuation">.</span>data<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>url<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">if</span> <span class="token punctuation">(</span>error<span class="token punctuation">.</span>response<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>error<span class="token punctuation">.</span>response<span class="token punctuation">.</span>status<span class="token punctuation">)</span><span class="token punctuation">;</span>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>error<span class="token punctuation">.</span>response<span class="token punctuation">.</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{<!-- --></span>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>error<span class="token punctuation">.</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token punctuation">}</span> </code></pre> <h1> <a id="_288"></a>其它资料下载</h1> <p>如果大家想继续了解人工智能相关学习路线和知识体系,欢迎大家翻阅我的另外一篇博客《重磅 | 完备的人工智能AI 学习——基础知识学习路线,所有资料免关注免套路直接网盘下载》<br> 这篇博客参考了Github知名开源平台,AI技术平台以及相关领域专家:Datawhale,ApacheCN,AI有道和黄海广博士等约有近100G相关资料,希望能帮助到所有小伙伴们。</p> </div> <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/markdown_views-0407448025.css" rel="stylesheet"> <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/style-c216769e99.css" rel="stylesheet"> </div> <div id="treeSkill"></div> </article>
OpenAI-ChatGPT最新官方接口《AI绘图》全网最详细中英文实用指南和教程,助你零基础快速轻松掌握全新技术(三)(附源码)
作者
sockstack
许可协议
CC BY 4.0
发布于
2023-11-07
修改于
2024-09-08
上一篇:软件:常用 Linux 软件汇总,值得收藏
下一篇:〖ChatGPT实践指南 - 零基础扫盲篇②〗- 深度体验 ChatGPT
尚未登录
登录 / 注册
文章分类
博客重构之路
5
Spring Boot简单入门
4
k8s 入门教程
0
MySQL 知识
1
NSQ 消息队列
0
ThinkPHP5 源码分析
5
使用 Docker 从零开始搭建私人代码仓库
3
日常开发汇总
3
标签列表
springboot
hyperf
swoole
webman
php
多线程
数据结构
docker
k8s
thinkphp
mysql
tailwindcss
flowbite
css
前端