4
2
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
You should golf your program a little bit more :) Use one letter variables and as little whitespace as possible. Also, you don't need the
if __name__ == "__main__":
if you're just writing a program. – beary605 – 2013-02-09T00:40:40.067@beary605 Thanks. I don't want to obfuscate it too much as I was hoping that it might inspire someone to make a great alternative solution. I suspect but don't know that the key may be to find a language+library pair that does almost the work for you. – felipa – 2013-02-09T09:04:56.610
I don't like "You are free to use any (..) libraries you like as long as they are available freely (..) online." This means I can write a library that does all the work, make it available online, and then publish a one-line program in an answer here that does nothing but call the library! – Mr Lister – 2013-02-10T15:06:23.970
@MrLister Thanks you are right. I need to fix that. Is the new version better? – felipa – 2013-02-10T16:35:00.507
@felipa good enough. – Mr Lister – 2013-02-10T18:27:28.207
1in bash, I just type
gimp
– boothby – 2013-02-12T07:11:12.653@boothby :) I am not sure that is in the spirit of the game. – felipa – 2013-02-12T07:56:31.633
@felipa Octave should be 44+15! – kaoD – 2013-02-12T21:38:14.280