0

I created a terrain generator that can user input in, and I stumble upon a problem which is when i generate terrain and save it in (.png) when I display it in my canvas it's empty.

Here is my function that will create a map, this is outside of my class:

def get_call(result,height, width, color_range, scale, octaves, persistence, lacunarity):
    height = (height.get())
    width = (width.get())
    color_range = (color_range.get())
    scale = (scale.get())
    octaves = (octaves.get())
    persistence = (persistence.get())
    lacunarity = (lacunarity.get())

    map_data = GenerateMap(
            (int(height), int(width)),
            x_starting_pos=random_seed(),
            y_starting_pos=random_seed(),
            color_range=int(color_range),
            scale= float(scale),
            octaves= int(octaves),
            persistance= float(persistence),
            lacunarity= float(lacunarity))
    Generate = map_data.generate_map("Generate")
    Image.fromarray((Generate).astype('uint8')).save('result.png')
    return Generate

I get the result using functools partial. I follow this method to render the output: here:

partial(get_call, Preview_Image, Height, Width, Color_range, Scale, Octaves, Persistence, Lacunarity)

and here is the problem:

Preview_Image = tk.Canvas(top)
Preview_Image.pack()
# Display output
img = PhotoImage(file="result.png")
Preview_Image.create_image(0, 0, image=img, anchor=tk.NW)
Preview_Image.place(relx=0.017, rely=0.067, relheight=0.784
        , relwidth=0.605)

I tried to use ImageTk.PhotoImage(Image.open("result.png")) and still not displaying image in my canvas. I tried to create a new one, only the display image, works fine. Can you tell me what is the problem of this code?

martineau
  • 119,623
  • 25
  • 170
  • 301
KNTY
  • 351
  • 3
  • 11
  • 1
    Is the last code block inside a function? – acw1668 Nov 30 '21 at 09:00
  • Yes, the full code is [here](https://pastebin.com/nDXznYK9) to understand more, Thanks. – KNTY Nov 30 '21 at 12:03
  • If it is inside a function, then it is the same issue as this question [why-does-tkinter-image-not-show-up-if-created-in-a-function](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) – acw1668 Nov 30 '21 at 12:33

0 Answers0