Failed to allocate bitmap python

Failed to allocate bitmap python

A subreddit for all questions related to programming in any language.

I am trying do create some batch plots of numpy data which are saved as PNG files. The plots work but after running the loop that does plotting, it gives the error: Fail to allocate bitmap.

The script always fails at the same iteration of the loop (34/73), even if I change the order of the loop. I looked up stacked overflow posts that said this is due to their being too many plots in the computer memory and that they should be cleared, which I have tried.

The plots are in a function stored in a different script I import into the main script. Each plot function contains a close Plot function that runs the commands: plt.clf, plt.cla(), and plt.close(«all»). At the end of each iteration in the loop, I also include gc.collect(), which to my understanding is to clear memory. Below are some examples of the code.

Am I missing something obvious? Has anyone else had the same issue?

def strainPathPlot(pathArray, array1, array2, array3, array4, exportPath, dataPoint):
print(«pathStrainPlot IN PROGRESS»)

fig, ax = plt.subplots(figsize=(10, 8))

ax.plot(pathArray, array1, label=»Applied Deformation: 0.25 mm», color=»#05FF00″)
ax.plot(pathArray, array2, label=»Applied Deformation: 0.50 mm», color=»#04D500″)
ax.plot(pathArray, array3, label=»Applied Deformation: 0.75 mm», color=»#039B00″)
ax.plot(pathArray, array4, label=»Applied Deformation: 1.00 mm», color=»#026900″)

ax.set(title=’Resulting Right Path Deformation vs. Path Length [‘ + str(dataPoint) + «]»,
xlabel=’Path Length [mm]’,
ylabel=’X-Axis Strain [mm/mm]’)

ax.legend().set_visible(True)
ax.legend(fontsize=’x-small’, loc=»lower right»)

# Y, X AXES
ax.axhline(0, color=’black’, linestyle=»:») # x = 0
ax.axvline(0, color=’black’, linestyle=»:») # y = 0
# Change major ticks to show every 20.
# ax.xaxis.set_major_locator(MultipleLocator(10))
# ax.yaxis.set_major_locator(MultipleLocator(0.02))
# Change minor ticks to show every 5. (20/4 = 5)
ax.xaxis.set_minor_locator(AutoMinorLocator(5))
ax.yaxis.set_minor_locator(AutoMinorLocator(5))

# Turn grid on for both major and minor ticks and style minor slightly
# differently.
ax.grid(which=’major’, color=’#CCCCCC’, linestyle=’—‘)
ax.grid(which=’minor’, color=’#CCCCCC’, linestyle=’:’)

Читайте также:  Html template wysiwyg editor

name = «pathStrain» + str(dataPoint)
plt.savefig(exportPath + name + ‘.png’)
closePlots()

def closePlots():
plt.clf()
plt.cla()
plt.close(«all»)
time.sleep(0.5)

Источник

Failed to allocate bitmap python

I am trying do create some batch plots of numpy data which are saved as PNG files. The plots work but after running the loop that does plotting, it gives the error: Fail to allocate bitmap.

The script always fails at the same iteration of the loop (34/73), even if I change the order of the loop. I looked up stacked overflow posts that said this is due to their being too many plots in the computer memory and that they should be cleared, which I have tried.

The plots are in a function stored in a different script I import into the main script. Each plot function contains a close Plot function that runs the commands: plt.clf, plt.cla(), and plt.close(«all»). At the end of each iteration in the loop, I also include gc.collect(), which to my understanding is to clear memory. Below are some examples of the code.

Am I missing something obvious? Has anyone else had the same issue?

def strainPathPlot(pathArray, array1, array2, array3, array4, exportPath, dataPoint):
print(«pathStrainPlot IN PROGRESS»)

fig, ax = plt.subplots(figsize=(10, 8))

ax.plot(pathArray, array1, label=»Applied Deformation: 0.25 mm», color=»#05FF00″)
ax.plot(pathArray, array2, label=»Applied Deformation: 0.50 mm», color=»#04D500″)
ax.plot(pathArray, array3, label=»Applied Deformation: 0.75 mm», color=»#039B00″)
ax.plot(pathArray, array4, label=»Applied Deformation: 1.00 mm», color=»#026900″)

ax.set(title=’Resulting Right Path Deformation vs. Path Length [‘ + str(dataPoint) + «]»,
xlabel=’Path Length [mm]’,
ylabel=’X-Axis Strain [mm/mm]’)

ax.legend().set_visible(True)
ax.legend(fontsize=’x-small’, loc=»lower right»)

# Y, X AXES
ax.axhline(0, color=’black’, linestyle=»:») # x = 0
ax.axvline(0, color=’black’, linestyle=»:») # y = 0
# Change major ticks to show every 20.
# ax.xaxis.set_major_locator(MultipleLocator(10))
# ax.yaxis.set_major_locator(MultipleLocator(0.02))
# Change minor ticks to show every 5. (20/4 = 5)
ax.xaxis.set_minor_locator(AutoMinorLocator(5))
ax.yaxis.set_minor_locator(AutoMinorLocator(5))

# Turn grid on for both major and minor ticks and style minor slightly
# differently.
ax.grid(which=’major’, color=’#CCCCCC’, linestyle=’—‘)
ax.grid(which=’minor’, color=’#CCCCCC’, linestyle=’:’)

name = «pathStrain» + str(dataPoint)
plt.savefig(exportPath + name + ‘.png’)
closePlots()

def closePlots():
plt.clf()
plt.cla()
plt.close(«all»)
time.sleep(0.5)

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Читайте также:  Создания форм отправки html

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Fail to allocate bitmap in Tree element with icon #3064

[Question] Fail to allocate bitmap in Tree element with icon #3064

Done — Download from GitHub (see docs for how) Download the PySimpleGUI.py file — see http://Upgrading.PySimpleGUI.org

Comments

Type of Issues (Question)

Quit for Fail to allocate bitmap.

Operating System

Python version

PySimpleGUI Port and Version

Your Experience Levels In Months or Years

1 yr_ Python programming experience
+10 yrs___ Programming experience overall
tkinter_____ Have used another Python GUI Framework (tkinter, Qt, etc) previously (yes/no is fine)?

You have completed these steps:

  • Read instructions on how to file an Issue
  • Searched through main docs http://www.PySimpleGUI.org for your problem
  • Searched through the readme for your specific port if not PySimpleGUI (Qt, WX, Remi)
  • Looked for Demo Programs that are similar to your goal http://www.PySimpleGUI.com
  • Note that there are also Demo Programs under each port on GitHub
  • Run your program outside of your debugger (from a command line)
  • Searched through Issues (open and closed) to see if already reported
  • Try again by upgrading your PySimpleGUI.py file to use the current one on GitHub. Your problem may have already been fixed but is not yet on PyPI.

Description of Problem / Question / Details

When I go to find solution for issues, failure Fail to allocate bitmap happened and quit from execution.

I create three icons, each one is 32×32 PNG in my script. The script I supplied is to show content of directory by Tree element. The situation is same for Demo_Tree_Element.py, they do the samething. Program will flash quit if you select a directory with more files and subdirectories and Fail to allocate bitmap message shown.

import os import sys from io import BytesIO from pathlib import Path from PIL import Image, ImageDraw import PySimpleGUI as sg def add_files_in_folder(path): icon0, v = check[0], [0] for root, dirs, files in os.walk(path): p = Path(root) parent = '' if root == path else p.as_posix() for dir in dirs: treedata.Insert(parent, p.joinpath(dir).as_posix(), dir, values=v, icon=icon0) for file in files: treedata.Insert(parent, p.joinpath(file).as_posix(), file, values=v, icon=icon0) def double_click(event): item = tree.Widget.selection()[0] key = tree.IdToKey[item] index = treedata.tree_dict[key].values[-1] index = (index + 1) % 3 treedata.tree_dict[key].values[-1] = index tree.update(key=key, icon=check[index]) def icon(check): box = (32, 32) background = (255, 255, 255, 0) rectangle = (3, 3, 29, 29) line = ((9, 17), (15, 23), (23, 9)) im = Image.new('RGBA', box, background) draw = ImageDraw.Draw(im, 'RGBA') draw.rectangle(rectangle, outline='black', width=3) if check == 1: draw.line(line, fill='black', width=3, joint='curve') elif check == 2: draw.line(line, fill='grey', width=3, joint='curve') with BytesIO() as output: im.save(output, format="PNG") png = output.getvalue() return png check = [icon(0), icon(1), icon(2)] starting_path = sg.popup_get_folder('Folder to display') if not starting_path: sys.exit(0) treedata = sg.TreeData() add_files_in_folder(starting_path) font = ('Helvetica', 16) layout = [[sg.Text('File and folder browser Test')], [sg.Tree(data=treedata, headings=['Size', ], auto_size_columns=True, num_rows=10, col0_width=40, key='-TREE-', font=font, row_height=48, show_expanded=False, enable_events=True)], [sg.Button('Ok', font=font), sg.Button('Cancel', font=font)]] window = sg.Window('Tree Element Test', layout, finalize=True) tree = window['-TREE-'] tree.Widget.bind("", double_click) while True: event, values = window.read() if event in (sg.WIN_CLOSED, 'Cancel'): break window.close()

I think this problem is casued by the function add_treeview_data, line # 11203 in PySimpleGUI.py.
It create each one PhotoImage object and save for each node. There will be 1000 PhotoImage objects if there are 1000 child files and child directories, even if I have only three small icons there. Too many PhotoImage objects make it fail to allocate bitmap.

def add_treeview_data(node): . photo = tk.PhotoImage(data=node.icon) .

The text was updated successfully, but these errors were encountered:

Читайте также:  Графический интерфейс пользователя питон

Источник

Оцените статью