Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
What is the best way to plot pixel-by-pixel?
-
Hi,
I'm glad I found Pythonista; it is a very neat piece of software indeed. This is a rather basic question, so I appreciate your patience.
I am trying to get back into programming and to learn Python by doing the same things that got me started with programming many years ago: I want to draw an image pixel by pixel. I understand that it is usually very slow to actually update each pixel using a Scene or Canvas method, so I am prepared to work on my bytearray (or other type of object) and then display the final result to the screen.
The value of each pixel is generated algorithmically, so I do not need shape primitives, I don't need animation and I just need to do one screen update.
Since I don't need animation, it sounds like I should stay away from Scene. However, the Drawing Bitmap Images section of the Canvas documentation doesn't help me much:
canvas.draw_image(image_name, x, y[, width, height]) Draws the image with the given name in a rectangle. If width and height are omitted, the image is drawn at its natural size.
How can I generate an object that is of the type that canvas.draw_image takes in its first argument? Where can I find an exact specification of what type of object this is, how to pack pixel information into it and so on?
Thank you and my apologies if this question is more closely related to packages included in Pythonista rather than about Pythonista itself.
Best,
Paul
-
Looking for this?
import Image import photos import clipboard import canvas imagebuffer = Image.new('RGBA', (640,640), 'white') pixel = imagebuffer.load() x1,x2,y1,y2 = 160,480,160,480 for i in range(x1,x2): pixel[i, y1] = 255,0,0,255 #red for i in range(x1,x2): pixel[i, y2] = 0,255,0,255 #green for i in range(y1,y2): pixel[x1, i] = 0,0,255,255 #blue for i in range(y1,y2): pixel[x2, i] = 0,0,0,255 #black clipboard.set_image((imagebuffer), format='png') canvas.set_size(640,640) canvas.draw_clipboard(0,0,imagebuffer.size[0],imagebuffer.size[1]) #photos.save_image(clipboard.get_image()) #print 'Image is saved to camera roll'
-
Why yes! Thank you very much for that. I played with the code a little bit last night, but I haven't been able to draw a conclusion on whether or not it is fast enough. I will update tonight.
-
elapsedTime.py might be useful for measuring the image creation time: https://gist.github.com/cclauss/8344302
-
ccc, I apologize, I did not see your comment. I never formally measured the time taken, but I did end up with the impression that the speed was "good enough".
brumm, thank you very much for the tip on how to create individual pixels. After a few days of tinkering, finding odd moments here and there to tap away in Pythonista, I recreated the Mandelbrot Set from memory. It's been years and years since I last played with fractals.
http://i.imgur.com/0blKkm0.jpg
This gave me the confidence to carry on with Python and generated images on my main computer:
http://i.imgur.com/H9b7htD.jpg
That one took about 8 hours, I think (with completely un-optimized code, running on one core only).
Anyway, thank you all for the help!
-
I love it... Very beautiful!