0% found this document useful (0 votes)
44 views

My Script

This document contains Python code to select a rectangular region of an image and save it as a cropped image. It initializes pygame and loads an image. It defines functions for displaying the image, initializing the display, and running the main loop to select the crop region by clicking and dragging the mouse. The main function calls these to load an image, run the selection loop, and save the cropped region to a new image file.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

My Script

This document contains Python code to select a rectangular region of an image and save it as a cropped image. It initializes pygame and loads an image. It defines functions for displaying the image, initializing the display, and running the main loop to select the crop region by clicking and dragging the mouse. The main function calls these to load an image, run the selection loop, and save the cropped region to a new image file.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

File: /home/project/Desktop/myscript

GIMP 2.6.11 Python Console Python 2.7 (r27:82500, Sep 16 2010, 18:03:06) [GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] >>> import pygame, sys Traceback (most recent call last): File "<input>", line 1, in <module> ImportError: No module named pygame >>> from PIL import Image >>> pygame.init() Traceback (most recent call last): File "<input>", line 1, in <module> NameError: name 'pygame' is not defined >>> >>> def displayImage( screen, px, topleft): ... screen.blit(px, px.get_rect()) ... if topleft: ... pygame.draw.rect( screen, (128,128,128), pygame.Rect(topleft[0], topleft[1], pygame.mouse.get_pos()[0] - topleft[0], pygame.mouse.get_pos()[1] - topleft[1])) ... pygame.display.flip() ... >>> def setup(path): ... px = pygame.image.load(path) ... screen = pygame.display.set_mode( px.get_rect()[2:] ) ... screen.blit(px, px.get_rect()) ... pygame.display.flip() ... return screen, px ... >>> def mainLoop(screen, px): ... topleft = None ... bottomright = None ... runProgram = True ... while runProgram: ... for event in pygame.event.get(): ... if event.type == pygame.QUIT: ... runProgram = False ... elif event.type == pygame.MOUSEBUTTONUP: ... if not topleft: ... topleft = event.pos ... else: ... bottomright = event.pos ... runProgram = False ... displayImage(screen, px, topleft) ... return ( topleft + bottomright ) ... >>> >>> if __name__ == "__main__": ... screen, px = setup(sys.argv[1]) ... left, upper, right, lower = mainLoop(screen, px) ... im = Image.open(sys.argv[1]) ... im = im.crop(( left, upper, right, lower)) ... im.save(sys.argv[2])

Page 1 of 1

You might also like