回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在编写一个python代码,它从命令行获取一个图像名称,并在一个窗口中打印它,并根据用户的喜好重新缩放,即输入<em>python3.2调整大小.py图像.gif2 3</em>需要图像.gif宽度加倍,高度增加三倍。我写了一个代码让图像翻两番:</p>
<pre><code>import sys
from cImage import *
def main():
oldImage = FileImage(sys.argv[1])
width = oldImage.getWidth()
height = oldImage.getHeight()
myWin = ImageWin("Old Image", width, height)
myNewWin = ImageWin("Quadrupled Image", width*4, height*4)
newImage = EmptyImage(width*4, height*4)
for r in range(width):
for c in range(height):
pixel = oldImage.getPixel(r, c)
newImage.setPixel(4*r, 4*c, pixel)
newImage.setPixel(4*r, 4*c+1, pixel)
newImage.setPixel(4*r, 4*c+2, pixel)
newImage.setPixel(4*r, 4*c+3, pixel)
newImage.setPixel(4*r+1, 4*c, pixel)
newImage.setPixel(4*r+1, 4*c+1, pixel)
newImage.setPixel(4*r+1, 4*c+2, pixel)
newImage.setPixel(4*r+1, 4*c+3, pixel)
newImage.setPixel(4*r+2, 4*c, pixel)
newImage.setPixel(4*r+2, 4*c+1, pixel)
newImage.setPixel(4*r+2, 4*c+2, pixel)
newImage.setPixel(4*r+2, 4*c+3, pixel)
newImage.setPixel(4*r+3, 4*c, pixel)
newImage.setPixel(4*r+3, 4*c+1, pixel)
newImage.setPixel(4*r+3, 4*c+2, pixel)
newImage.setPixel(4*r+3, 4*c+3, pixel)
oldImage.draw(myWin)
newImage.draw(myNewWin)
myWin.exitOnClick()
myNewWin.exitOnClick()
main()
</code></pre>
<p>但是我在尝试如何编辑我的代码以便它能按要求缩放时遇到了困难。我觉得我应该能够实现for循环,但是我很难让事情正常工作。在</p>
<p>任何帮助都将不胜感激!
编辑:</p>
<p>我正在使用以下修改后的代码:</p>
^{pr2}$
<p>我也试过用更复杂的方法:</p>
<pre><code>for r in range(oldHeight):
for c in range(oldWidth):
pixel = oldImage.getPixel(r,c)
for i in range(heightScalar):
for j in range(widthScalar):
myNewImage.setPixel((heightScalar*r)+i, (widthScalar*c)+j, pixel)
</code></pre>
<p>我用相等的标量(即3x3)对它进行了测试,它可以很好地处理正方形图像,但是当我试图改变其中一个时,我得到了一个错误:</p>
<blockquote>
<p>ValueError: Pixel index out of range.</p>
</blockquote>
<p>当我尝试用一个不正方形的图像时,我得到了一个错误,我的坐标超出了范围。在</p>
<p>我有点不知所措。。。谢谢你的帮助!
——————————————————————————————————————————————
我去测试了我的两个<em>四倍.py</em>和<em>调整大小.py</em>,对于方形图像,它们是有效的,但是当我放入一个非方形图像时,我得到以下结果:</p>
<blockquote>
<p>pixel = oldImage.getPixel(r, c)</p>
<p>File "/Users/jakebenedict/CPS/Lab6/cImage.py", line 313, in getTkPixel</p>
<p>p = [int(j) for j in self.im.get(x,y).split()]</p>
<p>File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/tkinter/<strong>init</strong>.py", line 3260, in get</p>
<p>return self.tk.call(self.name, 'get', x, y)</p>
<p>_tkinter.TclError: pyimage1 get: coordinates out of range</p>
</blockquote>
<p>不太清楚为什么它说。。。在</p>