用Python复制和粘贴图像

时间:2015-11-23 15:32:28

标签: python

我正在尝试复制并粘贴.ppm图像,但我不断获得黑色正方形而不是复制图像的较小版本。我最初打开的图像是一个蓝色和白色的标志,一旦我尝试复制并粘贴图像并将其保存为新文件名,它只是一个黑色方块。我不确定我是否缺少某个功能,或者我的代码中只有一个简单的错误。谢谢!

代码:

    from image import ImagePPM

def getImage():
    filename = input("What is the name of your image file?")
    img = ImagePPM.open(filename)
    return ImagePPM.open(filename)


def copyImage():
    filename = input("What is the image file name again?")
    img = ImagePPM.open(filename) 
    width, height = img.size
    u1width = int(input("What is the upper left width coordinate?"))
    ulheight = int(input("What is the upper left height?"))
    lrwidth = int(input("What is the lower right width?"))
    lrheight = int(input("What is the lower right height coordinate?"))
    copyImage = ImagePPM.new( (width, height))
    for w in range(width//2):
        for h in range(height//2):
            r, g, b = img.getpixel( (w,h) )
            copyImage.putpixel( (w,h), (r,g,b) )
    return copyImage

def saveImage():
    filename = input("Provide a filename to save your image in.")
    img = ImagePPM.open(filename)
    img.save(filename)
    print("Image saved")

def main():
    getImage()
    copyImage()
    saveImage()

main()

输出:

    What is the name of your image file?xray.ppm
What is the image file name again?xray.ppm
What is the upper left width coordinate?45
What is the upper left height?45
What is the lower right width?45
What is the lower right height coordinate?45
Provide a filename to save your image in.clipboard.ppm
Image saved

0 个答案:

没有答案
相关问题