0

背景:我正在使用 Eclipse SDK 4.2.1 (Juno) 中的 python 3.2,我注意到一些奇怪的事情 - 虽然我的程序在 Eclipse 中完美运行,但如果我从文件管理器中打开它们,它们总是会因错误而关闭。我设法在cmd关闭之前获得了截图:在此处输入图像描述

该程序似乎在“images”和“Cy.png”之间插入了一个额外的“\”。但是,我不能只从我的程序中删除斜线 - 在其中,我使用了两个斜线,因为您需要在字符串中包含斜线。我的程序如下:

from PIL import Image

def pathConstruction(count, imageName):
    l = []
    l.append('images\\')
    if count == 1:
        l.append('Sepia')
    l.append(imageName)
    imagePath = ''.join(l)
    return imagePath

def grayscale(pix, width, height):
    for col in range(width):
        for row in range(height):
            r,g,b = pix[col, row]
            avg = ((r + g + b) / 3)
            r = int(avg)
            g = int(avg)
            b = int(avg)
            pix[col, row] = r,g,b

def sepia(pix, width, height):
    for col in range(width):
        for row in range(height):
            r,g,b = pix[col, row]
            newR = (r * 0.393 + g * 0.769 + b * 0.189)
            newG = (r * 0.349 + g * 0.686 + b * 0.168)
            newB = (r * 0.272 + g * 0.534 + b * 0.131)
            pix[col, row] = int(newR),int(newG),int(newB)

imageName = input("Please input the full name of your image, including extension: ")
count = 0
imagePath= pathConstruction(count, imageName)
count = count + 1

img = Image.open(imagePath)
pix = img.load()
width, height = img.size

grayscale(pix, width, height)
sepia(pix, width, height)

imagePath = pathConstruction(count, imageName)
img.save(imagePath)
img.show()

问题:如何在 Eclipse 之外运行该程序?

4

2 回答 2

2

我认为问题不在于您在目录和文件名之间看到的额外反斜杠(我认为它只是来自repr字符串的),而是\r最后添加的反斜杠。我不知道为什么你会把它包含在值中,但是你可以通过调用字符串input来删除它。strip

于 2012-12-12T15:41:18.730 回答
1

只需使用内置的 os.path 模块。它将更简单,更可靠。

def pathConstruction(count, imageName):
    import os
    dir = "images"
    if count == 1:
        imageName = "Sepia" + imageName
    return os.path.join(dir, imageName)
于 2012-12-12T15:34:49.950 回答