我用Mandelbrot程序寫入圖像時遇到了問題;它與「(maxx-minx)/ width」與「(maxy-miny)/ width」第31行的舍入誤差相關,這導致501乘500圖片而不是500圖片。 ((寬度+ 1)*長度)不是(寬度*長度)。 我該如何解決這個問題?用PIL舍入錯誤
from PIL import Image
from cmath import *
from math import sqrt
from numpy import arange
width = 500
height = 500
minx = -0.752 #float(input("Please enter the minimum x value:"))
maxx = -0.748 #float(input("Please enter the maximum x value:"))
miny = 0.098 #float(input("Please enter the minimum y value:"))
maxy = 0.102 #float(input("Please enter the maximum y value:"))
gradient = Image.open("mandelbrot.png")
gradlist = list(gradient.getdata())
def testMandelbrot(x, y):
z = 0 + 0j
c = x + (y*1j)
iter = 0
while iter <= 69 and sqrt(z.real**2 + z.imag**2) < 4:
z = (z*z) + c
iter += 1
if iter == 70:
return (0, 0, 0, 255)
else:
return gradlist[int((iter - 1) * 140/70)]
img = Image.new('RGBA', (width, height), color=(255, 255, 255, 255))
image = [testMandelbrot(x, y) for y in arange(miny, maxy, (maxy-miny)/height) for x in arange(minx, maxx, (maxx-minx)/width)] #this line creates the error ((maxx-minx)/width)/(maxx - min) gives (width+1) not width
print(len(image), img.size)
img.putdata(image)
img.save("picture111.png", "PNG")
我已經測試過試圖強制舍入值,但這會使舍入誤差變得更糟。我該怎麼辦? –