img

package
v0.0.0-...-e9ad91d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Generate = &cobra.Command{
	Use:     "gen [prompt]",
	GroupID: "img",
	Short:   "Generate image",
	Long:    `Generates image with Dall-E`,
	Args:    cobra.MinimumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		c := openai.NewClient(os.Getenv("OPENAI_API_KEY"))

		ctx := context.Background()

		prompt := strings.Join(args, " ")

		request := openai.ImageRequest{
			Model:          openai.CreateImageModelDallE3,
			Prompt:         prompt,
			Size:           openai.CreateImageSize1024x1024,
			ResponseFormat: openai.CreateImageResponseFormatB64JSON,
			N:              1,
		}

		response, err := c.CreateImage(ctx, request)
		if err != nil {
			_, _ = fmt.Fprintf(os.Stderr, "Image creation error: %v\n", err)
			return
		}

		imgBytes, err := base64.StdEncoding.DecodeString(response.Data[0].B64JSON)
		if err != nil {
			_, _ = fmt.Fprintf(os.Stderr, "Base64 decode error: %v\n", err)
			return
		}

		r := bytes.NewReader(imgBytes)
		imgData, err := png.Decode(r)
		if err != nil {
			_, _ = fmt.Fprintf(os.Stderr, "PNG decode error: %v\n", err)
			return
		}

		outPath, err := cmd.Flags().GetString("out")
		if err != nil {
			_, _ = fmt.Fprintf(os.Stderr, "invalid out flag: %v\n", err)
			return
		}
		file, err := os.Create(outPath)
		if err != nil {
			_, _ = fmt.Fprintf(os.Stderr, "File creation error: %v\n", err)
			return
		}
		defer func(file *os.File) {
			_ = file.Close()
		}(file)

		if err := png.Encode(file, imgData); err != nil {
			_, _ = fmt.Fprintf(os.Stderr, "PNG encode error: %v\n", err)
			return
		}

		fmt.Printf("The image was saved as %s\n", outPath)
	},
}
View Source
var Group = &cobra.Group{
	ID:    "img",
	Title: "Image operations",
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL