Skip to content

Commit

Permalink
Merge pull request #355 from NaturalHistoryMuseum/feature/353-save-crops
Browse files Browse the repository at this point in the history
[#353] cruder scheme for save crops
  • Loading branch information
quicklizard99 authored Sep 20, 2016
2 parents f1bb163 + b9abda9 commit 68791ec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 38 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ This is an overview of major changes. Refer to the git repository for a full log

Version 0.1.34
-------------
- #353 Error 5
- #351 Metadata templates to allow fields with fixed values
- #349 Linux instructions
- #347 Explicitly reference python2 in scripts
- #342 Conda environment files
- #331 Box resize doing crazy things - emergency fix: restrict max zoom level
- #333 Ugly error message when opening or saving a file
- #332 Error when saving crops in Dropbox folder
- #330 libdmtx URL incorrect
- #328 Menu commands to show cookie cutter and metadata template files
- #325 Command-line option to set window size
Expand Down
60 changes: 22 additions & 38 deletions inselect/lib/document_export.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import errno
import shutil
import tempfile

from collections import defaultdict
from functools import partial
Expand Down Expand Up @@ -44,46 +44,30 @@ def crops_dir(self, document):

def save_crops(self, document, progress=None):
"""Saves images cropped from document.scanned to document.crops_dir.
Crops are first written to a tempdir in the same directory as document.
If it exists, document.crops_dir is unlinked, and the tempdir is
renamed to document.crops_dir. Any existing data in document.crops_dir
is therefore lost.
If document.crops_dir already exists, it is unlinked first.
"""
# Create temp dir alongside scan
tempdir = tempfile.mkdtemp(
dir=str(document.scanned.path.parent),
prefix=document.scanned.path.stem + '_temp_crops'
)
tempdir = Path(tempdir)
debug_print(u'Saving crops to to temp dir [{0}]'.format(tempdir))

crop_fnames = self.crop_fnames(document)
if progress:
progress('Removing existing saved crops')

crops_dir = self.crops_dir(document)
try:
# Save crops
document.save_crops_from_image(document.scanned,
(tempdir / fn for fn in crop_fnames),
progress)

# rm existing crops dir
crops_dir = self.crops_dir(document)
shutil.rmtree(str(crops_dir), ignore_errors=True)

# Rename temp dir
debug_print(u'Moving temp crops dir [{0}] to [{1}]'.format(
tempdir, crops_dir
))
tempdir.rename(crops_dir)
tempdir = None

debug_print(u'Saved [{0}] crops to [{1}]'.format(
document.n_items, crops_dir
))

return crops_dir
finally:
if tempdir:
shutil.rmtree(str(tempdir))
shutil.rmtree(unicode(crops_dir))
except OSError as e:
if errno.ENOENT == e.errno:
# Directory does not exist - do nothing
pass
else:
# Some other error
raise

crop_fnames = self.crop_fnames(document)
crops_dir.mkdir()
document.save_crops_from_image(
document.scanned,
(crops_dir / fn for fn in crop_fnames),
progress
)
return crops_dir

def csv_path(self, document):
return document.document_path.with_suffix('.csv')
Expand Down

0 comments on commit 68791ec

Please sign in to comment.