背景:我正在使用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之外运行此程序?
答案 0 :(得分:2)
我认为问题不在于您在目录和文件名之间看到的额外反斜杠(我认为它只是来自字符串的repr
),而是\r
最后添加。我不确定为什么你会从input
的值中包含它,但你可以通过在字符串上调用strip
来删除它。
答案 1 :(得分:1)
只需使用内置的os.path模块。它会更简单,更可靠。
def pathConstruction(count, imageName):
import os
dir = "images"
if count == 1:
imageName = "Sepia" + imageName
return os.path.join(dir, imageName)