Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-08-06· OpenAIAi2 WildChat-4.8M (AI2)

using System.Drawing; using System.Text.Json; using System.Text.Json.Serialization; var…

  1. User

    using System.Drawing; using System.Text.Json; using System.Text.Json.Serialization;

    var icons = Directory.GetFiles("H:\iccocooons\test\icons").ToDictionary(f => Path.GetFileNameWithoutExtension(f)!, f => new Icon() { Path = f, //Image = new Bitmap(f) });

    var recipes = Directory.EnumerateFiles("H:\iccocooons\test\recipes", "*.json", SearchOption.AllDirectories) .Select(f => { var text = File.ReadAllText(f); var type = JsonSerializer.Deserialize(text); if (type.Type != "minecraft:crafting_shaped") return null;

        //Console.WriteLine(File.ReadAllText(f));
        return JsonSerializer.Deserialize<RecipeRoot>(File.ReadAllText(f))!;
    }).Where(r => r != null);
    

    var count = 0; foreach (var recipeRoot in recipes) { var keys = recipeRoot.Key; if (keys == null) continue;

    var good = true;
    
    if (!recipeRoot.Result.TryGetValue("item", out var resultElement))
        continue;
    
    var result = resultElement.Deserialize<string>();
    var recipeItems = new Dictionary<char, string>();
    
    foreach (var (key, value) in recipeRoot.Key)
    {
        string itemName;
        if (value.ValueKind == JsonValueKind.Array)
        {
            var items = value.Deserialize<List<ItemName>>()!;
            if (!items.All(i => i.Item != null))
            {
                good = false;
                break;
            }
    
            itemName = items.First().Item;
        }
        else
        {
            var item = value.Deserialize<ItemName>()!;
            if (item.Item == null)
            {
                good = false;
                break;
            }
    
            itemName = item.Item;
        }
    
        if (itemName.Contains(":"))
            itemName = itemName.Split(":")[1];
        
        if (!icons.ContainsKey(itemName))
        {
            good = false;
            break;
        }
    
        recipeItems[key] = itemName;
    }
    
    if (good == false)
        continue;
    
    var recipeArray = new List<string?>();
    foreach (var patternLine in recipeRoot.Pattern)
    {
        foreach (var patternItem in patternLine)
        {
            if (patternItem == ' ')
            {
                recipeArray.Add(null);
                continue;
            }
    
            var item = recipeItems[patternItem];
            recipeArray.Add(icons[item].Path);
        }
    }
    
    //Console.WriteLine(JsonSerializer.Serialize(recipeArray));
    count++;
    

    }

    Console.WriteLine(count);

    class Icon { public string Path { get; set; } public Bitmap Image { get; set; } }

    public class ItemName { [JsonPropertyName("item")] public string? Item { get; set; } }

    public class RecipeRoot { [JsonPropertyName("type")] public string Type { get; set; }

    [JsonPropertyName("key")] public Dictionary<char, JsonElement> Key { get; set; }
    
    [JsonPropertyName("pattern")] public List<string> Pattern { get; set; }
    
    [JsonPropertyName("result")] public Dictionary<string, JsonElement> Result { get; set; }
    

    }

    public class RecipeType { [JsonPropertyName("type")] public string Type { get; set; } }

    extend code to make it merge crafts into 3x3 single image constisting of 16x16 icons, if Icon is null then just empty 16x16

  2. Assistant

    To extend your code to merge crafts into a 3x3 single image consisting of 16x16 icons, you'll need to use the System.Drawing namespace for image processing. You will create a new Bitmap with dimensions of 48x48 pixels (since each icon is 16x16 pixels and the grid is 3x3), draw each icon at the correct position, and handle null (empty) icons by leaving those 16x16 sections empty. Here’s how you can achieve that:

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text.Json;
    using System.Text.Json.Serialization;
    
    // Your existing code...
    
    var icons = Directory.GetFiles("H:\\iccocooons\\test\\icons")
        .ToDictionary(f => Path.GetFileNameWithoutExtension(f)!,
            f =>
                new Icon()
                {
                    Path = f,
                    Image = new Bitmap(f)
                });
    
    var recipes = Directory.EnumerateFiles("H:\\iccocooons\\test\\recipes", "*.json", SearchOption.AllDirectories)
        .Select(f =>
        {
            var text = File.ReadAllText(f);
            var type = JsonSerializer.Deserialize<RecipeType>(text);
            if (type.Type != "minecraft:crafting_shaped")
                return null;
    
            return JsonSerializer.Deserialize<RecipeRoot>(text)!;
        }).Where(r => r != null);
    
    var count = 0;
    foreach (var recipeRoot in recipes)
    {
        var keys = recipeRoot.Key;
        if (keys == null)
            continue;
    
        var good = true;
    
        if (!recipeRoot.Result.TryGetValue("item", out var resultElement))
            continue;
    
        var result = resultElement.Deserialize<string>();
        var recipeItems = new Dictionary<char, string>();
    
        foreach (var (key, value) in recipeRoot.Key)
        {
            string itemName;
            if (value.ValueKind == JsonValueKind.Array)
            {
                var items = value.Deserialize<List<ItemName>>()!;
                if (!items.All(i => i.Item != null))
                {
                    good = false;
                    break;
                }
    
                itemName = items.First().Item;
            }
            else
            {
                var item = value.Deserialize<ItemName>()!;
                if (item.Item == null)
                {
                    good = false;
                    break;
                }
    
                itemName = item.Item;
            }
    
            if (itemName.Contains(":"))
                itemName = itemName.Split(":")[1];
            
            if (!icons.ContainsKey(itemName))
            {
                good = false;
                break;
            }
    
            recipeItems[key] = itemName;
        }
    
        if (good == false)
            continue;
    
        var recipeArray = new List<string?>();
        foreach (var patternLine in recipeRoot.Pattern)
        {
            foreach (var patternItem in patternLine)
            {
                if (patternItem == ' ')
                {
                    recipeArray.Add(null);
                    continue;
                }
    
                var item = recipeItems[patternItem];
                recipeArray.Add(icons[item].Path);
            }
        }
    
        // Create a new 48x48 bitmap to hold the 3x3 grid
        using Bitmap bitmap = new Bitmap(48, 48);
        using Graphics graphics = Graphics.FromImage(bitmap);
    
        for (int i = 0; i < recipeArray.Count; i++)
        {
            int x = (i % 3) * 16;
            int y = (i / 3) * 16;
    
            if (recipeArray[i] != null)
            {
                using Bitmap iconBitmap = new Bitmap(recipeArray[i]!);
                graphics.DrawImage(iconBitmap, x, y);
            }
            else
            {
                // If it's null, just leave the space empty, or draw something like a placeholder
                // You could fill it with a background color
            }
        }
        
        bitmap.Save($"H:\\iccocooons\\output_{count}.png");
        count++;
    }
    
    Console.WriteLine(count);
    
    class Icon
    {
        public string Path { get; set; }
        public Bitmap Image { get; set; }
    }
    
    public class ItemName
    {
        [JsonPropertyName("item")]
        public string? Item { get; set; }
    }
    
    public class RecipeRoot
    {
        [JsonPropertyName("type")] public string Type { get; set; }
    
        [JsonPropertyName("key")] public Dictionary<char, JsonElement> Key { get; set; }
    
        [JsonPropertyName("pattern")] public List<string> Pattern { get; set; }
    
        [JsonPropertyName("result")] public Dictionary<string, JsonElement> Result { get; set; }
    }
    
    public class RecipeType
    {
        [JsonPropertyName("type")] public string Type { get; set; }
    }
    

    Explanation:

    1. Bitmap and Graphics: A Bitmap of size 48x48 pixels is created to store the 3x3 grid.
    2. Drawing: For each icon in the recipe, it checks if it's null. If not, it draws the icon onto the appropriate 16x16 position in the bitmap using Graphics.DrawImage().
    3. Output: The resulting images are saved to the specified output directory.

    This code will generate a 3x3 recipe image for each recipe and save it to the filesystem. Adjust the output path and any other specifics as required by your application.

  3. User

    it somehow doesnt fit but all images are 16x16, i checked

  4. Assistant

    If your images are indeed 16x16 but are not aligning correctly in the 3x3 grid, there might be a few possible issues to check. Issues could be related to discrepancies in image dimensions or drawing routines. Here's how you can adjust and verify your code:

    1. Verify Image Dimensions: Double-check that all icons used are indeed 16x16 pixels without any extra padding or transparent borders. Sometimes images reported as 16x16 might contain whitespace or transparency that extends their visual dimensions.

    2. Ensure Proper Drawing: Ensure that each icon is drawn at precisely the intended 16-pixel intervals in both the x and y directions. This configuration creates three distinct regions across both axes.

    Here I've added additional verification and a slight optimization to ensure all images fit perfectly in the grid:

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text.Json;
    using System.Text.Json.Serialization;
    
    var icons = Directory.GetFiles("H:\\iccocooons\\test\\icons")
        .ToDictionary(f => Path.GetFileNameWithoutExtension(f)!,
            f =>
            {
                var bitmap = new Bitmap(f);
                if (bitmap.Width != 16 || bitmap.Height != 16)
                {
                    throw new Exception($"Icon {f} is not 16x16 pixels.");
                }
                return new Icon()
                {
                    Path = f,
                    Image = bitmap
                };
            });
    
    var recipes = Directory.EnumerateFiles("H:\\iccocooons\\test\\recipes", "*.json", SearchOption.AllDirectories)
        .Select(f =>
        {
            var text = File.ReadAllText(f);
            var type = JsonSerializer.Deserialize<RecipeType>(text);
            if (type.Type != "minecraft:crafting_shaped")
                return null;
    
            return JsonSerializer.Deserialize<RecipeRoot>(text)!;
        }).Where(r => r != null);
    
    int count = 0;
    foreach (var recipeRoot in recipes)
    {
        var keys = recipeRoot.Key;
        if (keys == null)
            continue;
    
        var isValidRecipe = true;
    
        if (!recipeRoot.Result.TryGetValue("item", out var resultElement))
            continue;
    
        var result = resultElement.Deserialize<string>();
        var recipeItems = new Dictionary<char, string>();
    
        foreach (var (key, value) in recipeRoot.Key)
        {
            string itemName;
            if (value.ValueKind == JsonValueKind.Array)
            {
                var items = value.Deserialize<List<ItemName>>()!;
                if (!items.All(i => i.Item != null))
                {
                    isValidRecipe = false;
                    break;
                }
    
                itemName = items.First().Item;
            }
            else
            {
                var item = value.Deserialize<ItemName>()!;
                if (item.Item == null)
                {
                    isValidRecipe = false;
                    break;
                }
    
                itemName = item.Item;
            }
    
            if (itemName.Contains(":"))
                itemName = itemName.Split(":")[1];
    
            if (!icons.ContainsKey(itemName))
            {
                isValidRecipe = false;
                break;
            }
    
            recipeItems[key] = itemName;
        }
    
        if (!isValidRecipe)
            continue;
    
        var recipeArray = new List<string?>();
        foreach (var patternLine in recipeRoot.Pattern)
        {
            foreach (var patternItem in patternLine)
            {
                recipeArray.Add(patternItem == ' ' ? null : recipeItems[patternItem]);
            }
        }
    
        using Bitmap bitmap = new Bitmap(48, 48);
        using Graphics graphics = Graphics.FromImage(bitmap);
    
        for (int i = 0; i < recipeArray.Count; i++)
        {
            int x = (i % 3) * 16;
            int y = (i / 3) * 16;
    
            if (recipeArray[i] != null)
            {
                graphics.DrawImage(icons[recipeArray[i]].Image, x, y);
            }
            else
            {
                graphics.FillRectangle(Brushes.Transparent, x, y, 16, 16);
            }
        }
        
        bitmap.Save($"H:\\iccocooons\\output_{count}.png", System.Drawing.Imaging.ImageFormat.Png);
        count++;
    }
    
    Console.WriteLine(count);
    
    class Icon
    {
        public string Path { get; set; }
        public Bitmap Image { get; set; }
    }
    
    public class ItemName
    {
        [JsonPropertyName("item")]
        public string? Item { get; set; }
    }
    
    public class RecipeRoot
    {
        [JsonPropertyName("type")] public string Type { get; set; }
    
        [JsonPropertyName("key")] public Dictionary<char, JsonElement> Key { get; set; }
    
        [JsonPropertyName("pattern")] public List<string> Pattern { get; set; }
    
        [JsonPropertyName("result")] public Dictionary<string, JsonElement> Result { get; set; }
    }
    
    public class RecipeType
    {
        [JsonPropertyName("type")] public string Type { get; set; }
    }
    

    Things to Consider:

    • Exception Handling: The code now checks if each icon is indeed 16x16 pixels and throws an exception if not. This ensures you are working with the intended image dimensions.
    • Placeholder for Empty Spaces: If a specific cell in the grid corresponds to a null icon, draw a transparent rectangle or fill it with a background color (transparent in this code).
    • Image File Saving: Make sure you have permission to write to the directory specified. Adjust paths according to your setup and ensure they're accessible.

    This should ensure the images fit correctly into the 3x3 grid while handling any potential inconsistencies in icon sizes.