我尝试使用库 cv2 来更改图片。 在模式调试中我发现了函数 cv2.namedWindow:
中的问题def run(self):
name_of_window = 'Test_version'
image_cv2 = cv2.imread('external_data/probe.jpg')
cv2.namedWindow(name_of_window, cv2.WINDOW_NORMAL)
cv2.imshow(name_of_window, image_cv2)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.namedWindow 出现警告后程序停止。 如果有人提供建议,我会很高兴。
当我调用 os.environ 时,出现以下内容:
environ({
'PATH': '/home/spartak/PycharmProjects/python_base/lesson_016/env/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin',
'LC_MEASUREMENT': 'ru_RU.UTF-8', 'XAUTHORITY': '/run/user/1000/.mutter-Xwaylandauth.MJ52B1',
'INVOCATION_ID': 'dd129fae7f7c452cb8fa8cd53b9da873', 'XMODIFIERS': '@im=ibus',
'LC_TELEPHONE': 'ru_RU.UTF-8',
'XDG_DATA_DIRS': '/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop',
'GDMSESSION': 'ubuntu', 'LC_TIME': 'ru_RU.UTF-8', 'SNAP_COMMON': '/var/snap/pycharm-community/common',
'SNAP_INSTANCE_KEY': '', 'SNAP_USER_COMMON': '/home/spartak/snap/pycharm-community/common',
'DBUS_SESSION_BUS_ADDRESS': 'unix:path=/run/user/1000/bus',
'IDE_PROJECT_ROOTS': '/home/spartak/PycharmProjects/python_base', 'PS1': '(env) ', 'SNAP_REVISION': '256',
'XDG_CURRENT_DESKTOP': 'ubuntu:GNOME', 'JOURNAL_STREAM': '8:37824', 'LC_PAPER': 'ru_RU.UTF-8',
'SESSION_MANAGER': 'local/spartak-pc:@/tmp/.ICE-unix/2082,unix/spartak-pc:/tmp/.ICE-unix/2082',
'USERNAME': 'spartak', 'LOGNAME': 'spartak', 'PWD': '/home/spartak/PycharmProjects/python_base/lesson_016',
'MANAGERPID': '1951', 'IM_CONFIG_PHASE': '1', 'PYCHARM_HOSTED': '1', 'GJS_DEBUG_TOPICS': 'JS ERROR;JS LOG',
'PYTHONPATH': '/home/spartak/PycharmProjects/python_base:/home/spartak/PycharmProjects/python_base/lesson_012/python_snippets:/home/spartak/PycharmProjects/python_base/chatbot:/snap/pycharm-community/256/plugins/python-ce/helpers/third_party/thriftpy:/snap/pycharm-community/256/plugins/python-ce/helpers/pydev:/home/spartak/.cache/JetBrains/PyCharmCE2021.2/cythonExtensions:/home/spartak/PycharmProjects/python_base/lesson_016',
'SHELL': '/bin/bash', 'LC_ADDRESS': 'ru_RU.UTF-8',
'GIO_LAUNCHED_DESKTOP_FILE': '/var/lib/snapd/desktop/applications/pycharm-community_pycharm-community.desktop',
'BAMF_DESKTOP_FILE_HINT': '/var/lib/snapd/desktop/applications/pycharm-community_pycharm-community.desktop',
'IPYTHONENABLE': 'True', 'GNOME_DESKTOP_SESSION_ID': 'this-is-deprecated', 'GTK_MODULES': 'gail:atk-bridge',
'VIRTUAL_ENV': '/home/spartak/PycharmProjects/python_base/lesson_016/env', 'SNAP_ARCH': 'amd64',
'SYSTEMD_EXEC_PID': '2099', 'XDG_SESSION_DESKTOP': 'ubuntu', 'GNOME_SETUP_DISPLAY': ':1',
'SNAP_LIBRARY_PATH': '/var/lib/snapd/lib/gl:/var/lib/snapd/lib/gl32:/var/lib/snapd/void',
'SSH_AGENT_LAUNCHER': 'gnome-keyring', 'SHLVL': '0', 'LC_IDENTIFICATION': 'ru_RU.UTF-8',
'LC_MONETARY': 'ru_RU.UTF-8', 'SNAP_NAME': 'pycharm-community', 'QT_IM_MODULE': 'ibus',
'XDG_CONFIG_DIRS': '/etc/xdg/xdg-ubuntu:/etc/xdg', 'LANG': 'en_US.UTF-8',
'SNAP_INSTANCE_NAME': 'pycharm-community', 'XDG_SESSION_TYPE': 'wayland',
'SNAP_USER_DATA': '/home/spartak/snap/pycharm-community/256', 'PYDEVD_LOAD_VALUES_ASYNC': 'True',
'DISPLAY': ':0', 'SNAP_REEXEC': '', 'WAYLAND_DISPLAY': 'wayland-0',
'LIBRARY_ROOTS': '/home/spartak/.pyenv/versions/3.9.7/lib/python3.9:/home/spartak/.pyenv/versions/3.9.7/lib/python3.9/lib-dynload:/home/spartak/PycharmProjects/python_base/lesson_016/env/lib/python3.9/site-packages:/home/spartak/.cache/JetBrains/PyCharmCE2021.2/python_stubs/-1475777083:/snap/pycharm-community/256/plugins/python-ce/helpers/python-skeletons:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stdlib:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/jwt:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/six:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/mock:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/nmap:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/annoy:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/attrs:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/polib:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/retry:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/docopt:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/enum34:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/orjson:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/pysftp:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/xxhash:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/chardet:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/futures:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/pyaudio:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/tzlocal:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/Markdown:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/Werkzeug:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/aiofiles:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/colorama:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/filelock:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/paramiko:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/pathlib2:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/requests:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/waitress:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/freezegun:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/ipaddress:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/pyRFC3339:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/typed-ast:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/Deprecated:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/cachetools:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/frozendict:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/pyfarmhash:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/JACK-Client:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/contextvars:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/atomicwrites:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/cryptography:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/DateTimeRange:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/click-spinner:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/pkg_resources:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/python-gflags:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/python-slugify:/snap/pycharm-community/256/plugins/python-ce/helpers/typeshed/stubs/python-dateutil',
'SNAP_VERSION': '2021.2.3', 'LC_NAME': 'ru_RU.UTF-8', 'XDG_SESSION_CLASS': 'user',
'_': '/usr/bin/gnome-session', 'SNAP_DATA': '/var/snap/pycharm-community/256', 'PYTHONIOENCODING': 'UTF-8',
'DESKTOP_SESSION': 'ubuntu', 'SNAP': '/snap/pycharm-community/256', 'USER': 'spartak',
'SNAP_REAL_HOME': '/home/spartak', 'XDG_MENU_PREFIX': 'gnome-', 'GIO_LAUNCHED_DESKTOP_FILE_PID': '122719',
'QT_ACCESSIBILITY': '1', 'PYTHONDONTWRITEBYTECODE': '1', 'LC_NUMERIC': 'ru_RU.UTF-8',
'GJS_DEBUG_OUTPUT': 'stderr', 'SSH_AUTH_SOCK': '/run/user/1000/keyring/ssh', 'PYTHONUNBUFFERED': '1',
'GNOME_SHELL_SESSION_MODE': 'ubuntu',
'SNAP_CONTEXT': 'AoM6cqDJGx0xxWBUHLXWyVdhoNwTuHJsXSu2foumZWGYLCOaeHoL', 'XDG_RUNTIME_DIR': '/run/user/1000',
'SNAP_COOKIE': 'AoM6cqDJGx0xxWBUHLXWyVdhoNwTuHJsXSu2foumZWGYLCOaeHoL', 'HOME': '/home/spartak',
'QT_QPA_PLATFORM_PLUGIN_PATH': '/home/spartak/PycharmProjects/python_base/lesson_016/env/lib/python3.9/site-packages/cv2/qt/plugins',
'QT_QPA_FONTDIR': '/home/spartak/PycharmProjects/python_base/lesson_016/env/lib/python3.9/site-packages/cv2/qt/fonts',
'LD_LIBRARY_PATH': '/home/spartak/PycharmProjects/python_base/lesson_016/env/lib/python3.9/site-packages/cv2/../../lib64:'})
我从 Wayland 恢复到 Xorg 并正常工作,不再有警告
步骤如下:
WaylandEnable=false
中的
/etc/gdm3/custom.conf
QT_QPA_PLATFORM=xcb
中添加
/etc/environment
echo $XDG_SESSION_TYPE
在 Wayland 上使用 pandas 和 matplotlib 绘制绘图时也会出现相同的
Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway
。如果您不想停用 Wayland 并返回 X11,您可以选择将图形保存为 pdf 文件。
创建一些数据和绘图
import pandas
df = pandas.DataFrame({'x':range(0,30), 'y':range(10,40)})
df.set_index('x', inplace=True)
plot = df.plot(title='A nice line')
# Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.
将图形保存为 pdf 文件,如此答案中所述:
plot.get_figure().savefig('/tmp/output.pdf', format='pdf')
或使用 jupyter lab 在浏览器中显示绘图。
我在 Debian 12.4 (gnome/wayland) 上从 python 3.10 运行 matplotlib 时遇到了这个问题。我尝试抑制警告并过滤 python 中的日志记录,但没有效果。我不想禁用 Wayland,尽管我知道这可以解决问题。
Python 代码中对我有用的内容: `os.environ["XDG_SESSION_TYPE"] = "xcb"