0

I make one python script to make fractal image.

I try to use pp python module to make faster this source code.

The big issue is: image.putpixel((x, y), (i % 8 * 16, i % 4 * 32,i % 2 * 64)). This line of source code when is use like job give me somethig about : cPickle.UnpickleableError: Cannot pickle objects.

I think this resource cannot be serialized under pp. Any idea ? Thank you. Regards.

My source code :

from PIL import Image
#size of image
imgx = 600
imgy = 400
#make image buffer
image = Image.new("RGB", (imgx, imgy))

# area of fractal
xa = -2.0
xb = 2.0
ya = -2.0
yb = 2.0

#define constants
max_iterations = 10 # max iterations allowed
step_derivat = 0.002e-1 # step size for numerical derivative
error = 5e-19 # max error allowed

# function will generate the newton fractal
def f(z): return z * z  +complex(-0.31,0.031)

# draw derivate fractal for each y and x 
for y in range(imgy):
 zy = y * (yb - ya)/(imgy - 1) + ya
 for x in range(imgx):
  zx = x * (xb - xa)/(imgx - 1) + xa
  z = complex(zx, zy)
  for i in range(max_iterations):
   # make complex numerical derivative
   dz = (f(z + complex(step_derivat, step_derivat)) - f(z))/complex(step_derivat,step_derivat)
    # Newton iteration see wikipedia   
   z0 = z - f(z)/dz 
   # stop to the error 
   if abs(z0 - z) < error: 
    break
   z = z0
  #I use modulo operation expression to do RGB colors of the pixels 
  image.putpixel((x, y), (i % 8 * 16, i % 4 * 32,i % 2 * 64))

#save the result 
image.save("fractal.png", "PNG")
divibisan
  • 11,659
  • 11
  • 40
  • 58

1 Answers1

0

Parallel Python or the multiprocessing module require the objects which need to be passed around to other processes to be picklable, and the image object from PIL is not.

I recommend removing the call to image.putpixel from the function to be parallelized, returnin a simple list of RGB points or a numpy array which are picklable. then, after the calculations are done you can assemble the image.

Also, in order to get more specific advice, you should post the parallel version of the code as well.

fccoelho
  • 6,012
  • 10
  • 55
  • 67
  • I try to do that. Seam to be more complicate for me. Why? Because I can return the list. Seam pp and calling function over : job =job_server.submit has some problems with global definition. The way I try to do that : I make 2 function . One is : def newton_fractal(z):return z * z +complex(-0.31,0.031) . The next function I made is: – Cătălin George Feștilă Feb 26 '13 at 14:05
  • the big problem is image.putpixel((x, y), (i ... I need to use ob =job_server.submit(make_fractal, I don't know how to write this 2 function because depends one from another. One good tutorial about pp is how to transform the normal python code in to pp way because is some limitation. About my code with pp module is a mess. Not working. – Cătălin George Feștilă Feb 26 '13 at 14:18