【问题标题】:pycharm can not open image filepycharm无法打开图片文件
【发布时间】:2020-07-02 16:35:47
【问题描述】:

我正在尝试编写一个 python 计算器,这是我的代码

main.py:
import pygame
import os
import Item
global items


one = Item.Item('/images/numbers/1.png',       "1",       200, 200) # here is where you specify the image file name ( must be exact!)
two = Item.Item('/images/numbers/2.png',       "2",       200, 200) # here is where you specify the image file name ( must be exact!)
three = Item.Item('/images/numbers/3.png',       "3",       200, 200) # here is where you specify the image file name ( must be exact!)
four = Item.Item('/images/numbers/4.png',       "4",       200, 200) # here is where you specify the image file name ( must be exact!)
five = Item.Item('/images/numbers/5.png',       "5",       200, 200) # here is where you specify the image file name ( must be exact!)
six = Item.Item('/images/numbers/6.png',       "6",       200, 200) # here is where yo specify the image file name ( must be exact!)
seven = Item.Item('/images/numbers/7.png',       "7",       200, 200) # here is where you specify the image file name ( must be exact!)
eight = Item.Item('/images/numbers/8.jpg',       "8",       200, 200)# here is where you specify the image file name ( must be exact!)
nine = Item.Item('/images/numbers/9.png',       "9",       200, 200)# here is where you specify the image file name ( must be exact!)
ten = Item.Item('/images/numbers/0.jpg',       "0",       200, 200)# here is where you specify the image file name ( must be exact!)
plus = Item.Item('/images/sim/+.png',       "+",       200, 200)# here is where you specify the image file name ( must be exact!)
minus = Item.Item('/images/sim/-.png',       "-",       200, 200

这里是 item.py:

import pygame
import os
global items

items = pygame.sprite.Group()

class Item(pygame.sprite.Sprite):
    global hide

    # the constructor
    def __init__(self, img_name, name, x, y):
        pygame.sprite.Sprite.__init__(self, items)
        self.image = pygame.image.load(os.path.join(img_name))
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.name = name;
        self.hide = False

    # to display on game panel
    def display(self, surface):
        if self.hide == False:
            surface.blit(self.image, self.rect)

但是当我在 pycharm 上运行它时,它会出现这个:

/usr/local/bin/python3.7 /Users/oscar/Desktop/python_calculator/main.py
pygame 1.9.6
Traceback (most recent call last):
Hello from the pygame community. https://www.pygame.org/contribute.html
  File "/Users/oscar/Desktop/python_calculator/main.py", line 8, in <module>
    one = Item.Item('/images/numbers/1.png',       "1",       200, 200) # here is where you specify the image file name ( must be exact!)
  File "/Users/oscar/Desktop/python_calculator/Item.py", line 14, in __init__
    self.image = pygame.image.load(os.path.join(img_name))
pygame.error: Couldn't open /images/numbers/1.png

Process finished with exit code 1

我有 pygame 下载,我试图运行其他人的代码非常相似,但它的工作原理不知道为什么 请帮我 谢谢

【问题讨论】:

  • 您确定图像文件位于/images/numbers/,并且在使用os.path.join 时没有错过添加基本路径吗?
  • 您确定图像文件/images/numbers/1.png 存在于您的系统上吗?最直接的解释是它没有。 - 正如@TheGamer007 所说,您是否应该将基本路径传递给您的join() 调用?就像现在一样,join() 调用没有做任何事情。
  • 感谢您的回复。它肯定存在于finder中
  • 如果你只想创建一个 Image 对象,你可以尝试使用PIL library
  • 从路径中删除起始/。这使它变得绝对,我假设您的代码文件夹下有一个名为 images 的文件夹

标签: python pycharm


【解决方案1】:

我遇到了类似的问题,我已经找到了解决方案。 我认为这主要与虚拟环境如何运行您的应用程序有关。简而言之,它在不同的目录中运行它。这是解决方案:

  1. Click on the "Edit Configuration" button.

  2. Edit the path in the highlighted box. (For my own project, I just removed the \venv path.)

我希望这会有所帮助!

【讨论】:

  • 好吧,它没有帮助,你还有其他选择吗?
  • 回应bit_scientist,不,对不起。
【解决方案2】:

将图像插入与您正在使用的代码相同的项目文件中。我希望这会有所帮助

【讨论】:

    【解决方案3】:

    因为python只支持.gif文件扩展名所以改变文件格式。

    【讨论】:

    • 这是什么意思 Python 仅支持 .gif 扩展名? Python中有不同的库来处理有自己限制的图像。 pygame 特别是might support 其他格式
    猜你喜欢
    • 1970-01-01
    • 2015-04-23
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    相关资源
    最近更新 更多