0
我试图复制并粘贴一个.ppm图像,但我不断收到一个黑色的正方形,而不是较小版本的复制图像。我最初打开的图像是蓝色和白色的标志,一旦我尝试复制并粘贴图像并将其保存为新文件名,它就是一个黑色方块。我不确定是否缺少某个功能,或者我的代码中是否有简单的错误。谢谢!在Python中复制和粘贴图像
代码:
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
'saveImage'对我来说看起来很奇怪。您正在打开一张图片,然后将该图片保存为该图片的文件名?你不应该对'copyImage'返回的图像做些什么吗? – Kevin
看来你不明白'return'是如何工作的。您可能想重新阅读有关函数如何工作的文档。 –