I am constantly frustrated by how complicated it is to write graphical programs that would have been only a few lines of code 20+ years ago. Since the invention of the mouse this has become even worse it seems. I was hoping someone could put me out of my misery and show the shortest code to do the following really basic "modern" operations.
The code will read in an image in any sensible format of your choosing (png, jpg, etc.), display it on the screen, allow you to choose a rectangular region with your mouse and then just save the selected region as an image file.
You are free to use any programming language and any library which hasn't been written especially for this competition. Both should be freely (in both senses) online. The code should be runnable in linux.
To start things off.... here is a very lightly golfed solution in python modified from code to Andrew Cox from https://stackoverflow.com/questions/601874/digital-image-cropping-in-python which takes 1294 1101 characters.
import pygame, sys
from PIL import Image
pygame.init()
def displayImage( s, px, t):
s.blit(px, px.get_rect())
if t:
pygame.draw.rect( s, (128,128,128), pygame.Rect(t[0], t[1], pygame.mouse.get_pos()[0] - t[0], pygame.mouse.get_pos()[1] - t[1]))
pygame.display.flip()
def setup(path):
px = pygame.image.load(path)
s = pygame.display.set_mode( px.get_rect()[2:] )
s.blit(px, px.get_rect())
pygame.display.flip()
return s, px
def mainLoop(s, px):
t = None
b = 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 t:
t = event.pos
else:
b = event.pos
runProgram = False
displayImage(s, px, t)
return ( t + b )
s, px = setup(sys.argv[1])
left, upper, right, lower = mainLoop(s, px)
im = Image.open(sys.argv[1])
im = im.crop(( left, upper, right, lower))
im.save(sys.argv[2])
I believe it is traditional to give a time limit, so I will accept the best answer in exactly one week's time.
Current count
- Octave - 44 + 15 = 59 characters
- R - 305 characters
- Python - 343 characters
- Java - 560 characters
if __name__ == "__main__":
if you're just writing a program. \$\endgroup\$gimp
\$\endgroup\$