diff --git a/cmd/img.go b/cmd/img.go index 300b836..f9f0ac5 100644 --- a/cmd/img.go +++ b/cmd/img.go @@ -22,6 +22,7 @@ var ( imgGenParallel bool imgGenSaveAsPng bool imgGenOnlyMissing bool + imgGenTransparent bool imgGenCmd = &cobra.Command{ Use: "gen", Short: "Generate images from a collection.json and the layers folder", @@ -33,7 +34,7 @@ var ( --out ./generated-imgs \ --parallel`, RunE: func(cmd *cobra.Command, args []string) error { - if err := imgen.GenerateImagesFromCollectionAttributesJson(imgGenWidth, imgGenHeight, imgGenStartingIndex, imgGenCollectionPath, imgGenConfigPath, imgGenOutDir, imgGenParallel, imgGenSaveAsPng, imgGenOnlyMissing); err != nil { + if err := imgen.GenerateImagesFromCollectionAttributesJson(imgGenWidth, imgGenHeight, imgGenStartingIndex, imgGenCollectionPath, imgGenConfigPath, imgGenOutDir, imgGenParallel, imgGenSaveAsPng, imgGenOnlyMissing, imgGenTransparent); err != nil { return err } return nil @@ -54,6 +55,7 @@ func init() { imgGenCmd.MarkFlagRequired("config") imgGenCmd.Flags().StringVar(&imgGenOutDir, "out", "", "path to the folder where all imgs will be generated") imgGenCmd.MarkFlagRequired("out") + imgGenCmd.Flags().BoolVar(&imgGenTransparent, "transparent", false, "use this flag if the background of your nft is transparent") imgGenCmd.Flags().BoolVar(&imgGenParallel, "parallel", false, "generate images in parallel using all your cores") imgGenCmd.Flags().BoolVar(&imgGenSaveAsPng, "png", false, "generate png images instead of jpg images") imgGenCmd.Flags().BoolVar(&imgGenOnlyMissing, "onlyMissing", false, "generate only the images that are missing in the output dir") diff --git a/imgen/imgen.go b/imgen/imgen.go index 8bfb2ca..9722ac2 100644 --- a/imgen/imgen.go +++ b/imgen/imgen.go @@ -15,7 +15,7 @@ import ( "github.com/alephao/nftool/utils" ) -func GenerateImagesFromCollectionAttributesJson(width, height, startingIndex int, collectionPath, configPath, outDir string, parallel, saveAsPng, onlyMissing bool) error { +func GenerateImagesFromCollectionAttributesJson(width, height, startingIndex int, collectionPath, configPath, outDir string, parallel, saveAsPng, onlyMissing, transparent bool) error { var config traits_fs.Config err := utils.LoadYamlFileIntoStruct(configPath, &config) if err != nil { @@ -28,17 +28,17 @@ func GenerateImagesFromCollectionAttributesJson(width, height, startingIndex int } if onlyMissing { - return GenerateMissingImagesFromCollectionAttributes(width, height, collectionAttributes, config.PathMap, outDir, saveAsPng) + return GenerateMissingImagesFromCollectionAttributes(width, height, collectionAttributes, config.PathMap, outDir, saveAsPng, transparent) } if parallel { - return GenerateManyImagesFromCollectionAttributesParallel(width, height, startingIndex, collectionAttributes, config.PathMap, outDir, saveAsPng) + return GenerateManyImagesFromCollectionAttributesParallel(width, height, startingIndex, collectionAttributes, config.PathMap, outDir, saveAsPng, transparent) } else { - return GenerateManyImagesFromCollectionAttributes(width, height, startingIndex, collectionAttributes, config.PathMap, outDir, saveAsPng) + return GenerateManyImagesFromCollectionAttributes(width, height, startingIndex, collectionAttributes, config.PathMap, outDir, saveAsPng, transparent) } } -func GenerateManyImagesFromCollectionAttributesParallel(width, height, startingIndex int, collectionAttributes []traits.TraitGroup, layersMap map[string]string, outputDir string, saveAsPng bool) error { +func GenerateManyImagesFromCollectionAttributesParallel(width, height, startingIndex int, collectionAttributes []traits.TraitGroup, layersMap map[string]string, outputDir string, saveAsPng, transparent bool) error { var chunks []traits.TraitCollection numCpu := runtime.NumCPU() chunkSize := (len(collectionAttributes) + numCpu - 1) / numCpu @@ -61,7 +61,7 @@ func GenerateManyImagesFromCollectionAttributesParallel(width, height, startingI chunkCopy := make(traits.TraitCollection, chunkSize) copy(chunkCopy, chunk) go func() { - GenerateManyImagesFromCollectionAttributes(width, height, startingIndex, chunkCopy, layersMap, outputDir, saveAsPng) + GenerateManyImagesFromCollectionAttributes(width, height, startingIndex, chunkCopy, layersMap, outputDir, saveAsPng, transparent) wg.Done() }() } @@ -71,7 +71,7 @@ func GenerateManyImagesFromCollectionAttributesParallel(width, height, startingI return nil } -func GenerateManyImagesFromCollectionAttributes(width, height, startingIndex int, collectionAttributes []traits.TraitGroup, layersMap map[string]string, outputDir string, saveAsPng bool) error { +func GenerateManyImagesFromCollectionAttributes(width, height, startingIndex int, collectionAttributes []traits.TraitGroup, layersMap map[string]string, outputDir string, saveAsPng, transparent bool) error { for i, traitGroup := range collectionAttributes { var extension string if saveAsPng { @@ -83,7 +83,7 @@ func GenerateManyImagesFromCollectionAttributes(width, height, startingIndex int if i%50 == 0 { fmt.Printf("Generating %s\n", out) } - err := GenerateImageFromAttributes(width, height, traitGroup, layersMap, out, saveAsPng) + err := GenerateImageFromAttributes(width, height, traitGroup, layersMap, out, saveAsPng, transparent) if err != nil { // return err fmt.Printf("Failed to generate: %s\nError: %s\n", out, err.Error()) @@ -92,7 +92,7 @@ func GenerateManyImagesFromCollectionAttributes(width, height, startingIndex int return nil } -func GenerateMissingImagesFromCollectionAttributes(width, height int, collection traits.TraitCollection, layersMap map[string]string, outputDir string, saveAsPng bool) error { +func GenerateMissingImagesFromCollectionAttributes(width, height int, collection traits.TraitCollection, layersMap map[string]string, outputDir string, saveAsPng, transparent bool) error { missingIndexes, err := FindMissingIndexes(0, len(collection), outputDir, saveAsPng) if err != nil { return fmt.Errorf("failed to find missing indexes: %s", err.Error()) @@ -109,7 +109,7 @@ func GenerateMissingImagesFromCollectionAttributes(width, height int, collection out := fmt.Sprintf("%s/%d.%s", outputDir, missingIndex, extension) traitGroup := collection[missingIndex] fmt.Printf("Generating %s\n", out) - err := GenerateImageFromAttributes(width, height, traitGroup, layersMap, out, saveAsPng) + err := GenerateImageFromAttributes(width, height, traitGroup, layersMap, out, saveAsPng, transparent) if err != nil { fmt.Printf("Failed to generate: %s\nError: %s\n", out, err.Error()) } @@ -117,7 +117,7 @@ func GenerateMissingImagesFromCollectionAttributes(width, height int, collection return nil } -func GenerateImageFromAttributes(width, height int, attributes traits.TraitGroup, layersMap map[string]string, outputPath string, saveAsPng bool) error { +func GenerateImageFromAttributes(width, height int, attributes traits.TraitGroup, layersMap map[string]string, outputPath string, saveAsPng, transparent bool) error { keys := []string{} layersPaths := []string{} for _, attr := range attributes { @@ -128,10 +128,10 @@ func GenerateImageFromAttributes(width, height int, attributes traits.TraitGroup keys = append(keys, key) layersPaths = append(layersPaths, layersMap[key]) } - return GenerateImageFromLayers(width, height, keys, layersPaths, outputPath, saveAsPng) + return GenerateImageFromLayers(width, height, keys, layersPaths, outputPath, saveAsPng, transparent) } -func GenerateImageFromLayers(width, height int, keys []string, layerPaths []string, outputPath string, saveAsPng bool) error { +func GenerateImageFromLayers(width, height int, keys []string, layerPaths []string, outputPath string, saveAsPng, transparent bool) error { newImage := image.NewNRGBA(image.Rect(0, 0, width, height)) for i, path := range layerPaths { @@ -147,7 +147,7 @@ func GenerateImageFromLayers(width, height int, keys []string, layerPaths []stri return fmt.Errorf("failed to decode image as png at path '%s': %s", path, err.Error()) } - if i == 0 { + if i == 0 && !transparent { draw.Draw(newImage, newImage.Bounds(), img, image.Point{}, draw.Src) } else { draw.Draw(newImage, newImage.Bounds(), img, image.Point{}, draw.Over)