Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Nested while loop problem + turtle DreamingInsanity Splitter Posts: 154 Threads: 34 Joined: Jun 2018 Reputation: 10 Likes received: 9 #1 Jul-06-2019, 08:14 AM Here's a bit of info. I am creating a simple program (for now) that takes an image, makes it black and white, then using turtle, draws it on the screen. ```from PIL import Image from turtle import * image_path = "image.png" loaded_image = None x, y, old_x = 0, 0, 0 def convert_img(): image_file = Image.open(image_path) image_file = image_file.convert('L') image_file.save('bw_image.png') def get_pix_col(pixels, x, y): return pixels[x,y] def draw(img, pixels): global old_x, x ,y while x < img.size[0]: while y < img.size[1]: pendown() brightness = get_pix_col(pixels, x, y) color(brightness, brightness, brightness) if(x == old_x): goto((-img.size[0]/2) + x, (-img.size[1]/2) + y) else: penup() goto((-img.size[0]/2) + x, (-img.size[1]/2) + y) old_x = x y += 1 x += 1 done() def main(): convert_img() loaded_image = Image.open('bw_image.png') pix = loaded_image.load() canvas_setup(loaded_image) draw(loaded_image, pix) def canvas_setup(img): colormode(255) setup(img.size[0], img.size[1]) bgcolor(0, 0, 0) speed(10) penup() setposition(-img.size[0], -img.size[1]) main() ```The first problem with this is the nested while loops. They should go: ``````Output: x,y: 0,0 0,1 0,2 0,3 0,4 ... ``````until 'y' reaches, 562 in the case of my image. But it is whatever the 'y' size of the image is. When y is this the output becomes: ``````Output: 1,0 1,1 1,2 ... ``````over and over until 'x' is equal to the x size of the image (1000 in my case). However the problem is, 'x' never increments, it always is 0. There's got to be a simple fix but I can't find it. The only reason I am not using a nested for loop is so that I can increment 'x' and 'y' with different values. The other problem is how can I speed up the turtle? I am already using 'speed(10)' but it is still very slow when drawing. If I can speed it up a lot that would be nice! Thanks, Dream ichabod801 Bunny Rabbit Posts: 4,231 Threads: 94 Joined: Sep 2016 Reputation: 273 Likes received: 1264 #2 Jul-06-2019, 11:27 AM You need to reset y to 0. When x is 0 and y reaches img.size[1], the inner loop ends. Then x is incremented, and it checks the inner loop's condition again. But since you didn't reset y, it skips the loop and increments x again, until the outer loop is done. So when you increment x, reset y. DreamingInsanity likes this post Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures DreamingInsanity Splitter Posts: 154 Threads: 34 Joined: Jun 2018 Reputation: 10 Likes received: 9 #3 Jul-06-2019, 12:14 PM (Jul-06-2019, 11:27 AM)ichabod801 Wrote: You need to reset y to 0. When x is 0 and y reaches img.size[1], the inner loop ends. Then x is incremented, and it checks the inner loop's condition again. But since you didn't reset y, it skips the loop and increments x again, until the outer loop is done. So when you increment x, reset y. Ohhh right. I forgot about that! Thanks. DreamingInsanity Splitter Posts: 154 Threads: 34 Joined: Jun 2018 Reputation: 10 Likes received: 9 #4 Jul-06-2019, 02:01 PM (This post was last modified: Jul-06-2019, 02:01 PM by DreamingInsanity. Edited 2 times in total.) As for speeding it up - I now have this: ```from PIL import Image from turtle import * image_path = "image.png" loaded_image = None x, y, old_x = 0, 0, 0 def convert_img(): image_file = Image.open(image_path) image_file = image_file.convert('L') image_file.save('bw_image.png') def get_pix_col(pixels, x, y): return pixels[x,y] def draw(img, pixels): global old_x, x ,y while x < img.size[0]: while y < img.size[1]: pendown() brightness = get_pix_col(pixels, x, y) color(brightness, brightness, brightness) if(x == old_x): goto((-img.size[0]/2) + x, (img.size[1]/2) - y) else: penup() goto((-img.size[0]/2) + x, (img.size[1]/2) - y) old_x = x y += 1 update() x += 1 y = 0 done() def main(): convert_img() loaded_image = Image.open('bw_image.png') pix = loaded_image.load() canvas_setup(loaded_image) draw(loaded_image, pix) def canvas_setup(img): colormode(255) setup(img.size[0], img.size[1]) bgcolor(0, 0, 0) tracer(0, 0) speed("fastest") penup() setposition(-img.size[0] / 2, img.size[1] / 2) main() ```All it does now is update the screen only once one line of the x has been completed. The auto screen refresh was disabled. It is considerably faster than before, however, even with my image (1000x563) It still takes over 5 mins to draw it on the canvas. What else can I do to speed it up? EDIT: explanation « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Dataframe mean calculation problem: do we have to loop? sparkt 1 247 Aug-28-2020, 02:41 PM Last Post: sparkt Begginer problem with turtle Damien_X 1 116 Aug-25-2020, 11:23 AM Last Post: GOTO10 Nested loop indexing Morte 4 415 Aug-04-2020, 07:24 AM Last Post: Morte Python loop problem Kristenl2784 11 643 Jun-18-2020, 07:22 PM Last Post: buran Nested for loop not looping puttingwordstogether 0 224 Jun-16-2020, 11:15 PM Last Post: puttingwordstogether Help: for loop with dictionary and nested lists mart79 1 357 Apr-12-2020, 02:52 PM Last Post: TomToad Problem with append list in loop michaelko03 0 332 Feb-16-2020, 07:04 PM Last Post: michaelko03 Nested Loop for user input Ads 2 609 Dec-30-2019, 11:44 AM Last Post: Ads openpyxl nested for loop help rmrten 3 1,119 Oct-16-2019, 03:11 PM Last Post: stullis nested for loop dilemma YoungGrassHopper 9 737 Sep-13-2019, 03:56 AM Last Post: jsira2003

Forum Jump:

Users browsing this thread: 1 Guest(s)