Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate

Commit 609cda3297223a3c58e92a7416a45812dc56b563

Implement auto-cropping

By default we scan the full supported area, not actual page size. Getting rid
of empty borders automatically helps a lot with producing readable and
printable (i.e. useful) output.
scan.py
(15 / 1)
  
2525import os
2626import sys
2727import tempfile
28import threading
2829import time
2930
3031import gobject
3132import gtk
3233
34import Image
35import ImageChops
36import ImageColor
3337import reportlab.pdfgen.canvas
3438from reportlab.lib import units
3539import sane
36import threading
3740
3841from sugar.activity.widgets import ActivityToolbarButton, StopButton
3942from sugar.graphics.toggletoolbutton import ToggleToolButton
409409 loader.close()
410410 return pixbuf
411411
412 def autocrop(self, image):
413 if image.mode != 'RGB':
414 image = image.convert('RGB')
415
416 white_image = Image.new('RGB', image.size, ImageColor.getrgb('white'))
417 diff = ImageChops.difference(image, white_image)
418 return image.crop(diff.getbbox())
419
412420 def _process_image(self, image):
421 logging.debug('pre-crop size: %r', image.size)
422 image = self.autocrop(image)
423 logging.debug('post-crop size: %r', image.size)
413424 image_file = tempfile.NamedTemporaryFile(dir=self._temp_dir,
414425 suffix='.png')
415426 image.save(image_file.name)