help with this loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thekid
    New Member
    • Feb 2007
    • 145

    help with this loop

    I need help writing a loop for this.
    Code:
    image = ImageGrab.grab((0,0,800,800))
    box = (100,100,400,400)
    im = image.crop(box)
    im1 = im.save("screen.jpg")
    # at this point I need to rotate the image 90 degrees and crop it again, but do this four times-
    # so I'd like a loop here instead of this:
    im2 = im1.rotate(90)
    im3 = im2.crop(box)
    im4 = im3.save("screen1.jpg")
    im5 = im4.rotate(90)
    im6 = im5.crop(box)
    im7 = im6.save("screen2.jpg")
    im8=im7.rotate(90)
    im9=im8.crop(box)
    im10=im9.save("screen3.jpg")
    # I did try something like this:
    # for i in range(3):
    #       im,im1,im2
    #but that doesn't work
    any help?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    If you don't need to save any intermediate results, maybe something like this:
    [code=Python]
    for i in range(1, 4):
    im1.rotate(90). crop(box).save( "screen%s.j pg" % i)[/code]

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      I thought about it some more, and the code I suggested will only work if the methods modify the image object "in place". I am not so sure whether or not the save method returns an image object, but it probably does not modify the object. Here's the loop again, slightly modified:
      [code=Python]for i in range(1, 4):
      im1 = im1.rotate(90). crop(box)
      im1.save("scree n%s.jpg" % i)[/code]

      Comment

      • Thekid
        New Member
        • Feb 2007
        • 145

        #4
        This looks good but when I try it the image will only rotate one time and then the jpg is saved 3 times.

        Comment

        Working...