def create_crop(self, original_image, crop_name, crop_data): """Create a new crop based on the crop co-ordinates :param original: Article to add the crop :param crop_name: Name of the crop :param doc: Crop details :raises SuperdeskApiError.badRequestError :return dict: rendition """ original_file = superdesk.app.media.fetch_rendition(original_image) if not original_file: raise SuperdeskApiError.badRequestError('Original file couldn\'t be found') try: cropped, out = crop_image(original_file, crop_name, crop_data) crop = self.get_crop_by_name(crop_name) if not cropped: raise SuperdeskApiError.badRequestError('Saving crop failed.') # resize if needed if crop.get('width') or crop.get('height'): out, width, height = _resize_image(out, size=(crop.get('width'), crop.get('height')), keepProportions=crop.get('keep_proportions', True)) crop['width'] = width crop['height'] = height out.seek(0) return self._save_cropped_image(out, original_image, crop_data) except SuperdeskApiError: raise except Exception as ex: raise SuperdeskApiError.badRequestError('Generating crop failed: {}'.format(str(ex)))
def store_file(self, doc, content, filename, content_type): res = process_file_from_stream(content, filename=filename, content_type=content_type) file_name, content_type, metadata = res cropping_data = self.get_cropping_data(doc) _, out = crop_image(content, filename, cropping_data) metadata['length'] = json.dumps(len(out.getvalue())) try: logger.debug('Going to save media file with %s ' % file_name) out.seek(0) id = app.media.put(out, filename=file_name, content_type=content_type, metadata=metadata) doc['media'] = id doc['mime_type'] = content_type doc['filemeta'] = decode_metadata(metadata) inserted = [doc['media']] file_type = content_type.split('/')[0] rendition_spec = config.RENDITIONS['avatar'] renditions = generate_renditions(out, doc['media'], inserted, file_type, content_type, rendition_spec, url_for_media) doc['renditions'] = renditions except Exception as io: logger.exception(io) for file_id in inserted: delete_file_on_error(doc, file_id) raise SuperdeskError(message='Generating renditions failed')
def crop_and_store_file(self, doc, content, filename, content_type): # retrieve file name and metadata from file file_name, content_type, metadata = process_file_from_stream(content, content_type=content_type) # crop the file if needed, can change the image size was_cropped, out = crop_image(content, filename, doc) # the length in metadata could be updated if it was cropped if was_cropped: file_name, content_type, metadata_after_cropped = process_file_from_stream(out, content_type=content_type) # when cropped, metadata are reseted. Then we update the previous metadata variable metadata['length'] = metadata_after_cropped['length'] try: logger.debug('Going to save media file with %s ' % file_name) out.seek(0) file_id = app.media.put(out, filename=file_name, content_type=content_type, resource=self.datasource, metadata=metadata) doc['media'] = file_id doc['mimetype'] = content_type doc['filemeta'] = decode_metadata(metadata) inserted = [doc['media']] file_type = content_type.split('/')[0] rendition_spec = config.RENDITIONS['avatar'] renditions = generate_renditions(out, file_id, inserted, file_type, content_type, rendition_spec, url_for_media) doc['renditions'] = renditions except Exception as io: logger.exception(io) for file_id in inserted: delete_file_on_error(doc, file_id) raise SuperdeskApiError.internalError('Generating renditions failed')
def create_crop(self, original_image, crop_name, crop_data): """Create a new crop based on the crop co-ordinates :param original: Article to add the crop :param crop_name: Name of the crop :param doc: Crop details :raises SuperdeskApiError.badRequestError :return dict: rendition """ original_file = app.media.fetch_rendition(original_image) if not original_file: raise SuperdeskApiError.badRequestError("Original file couldn't be found") try: cropped, out = crop_image(original_file, crop_name, crop_data) crop = self.get_crop_by_name(crop_name) if not cropped: raise SuperdeskApiError.badRequestError("Saving crop failed.") # resize if needed if crop.get("width") or crop.get("height"): out, width, height = _resize_image( out, size=(crop.get("width"), crop.get("height")), keepProportions=crop.get("keep_proportions", True), ) crop["width"] = width crop["height"] = height out.seek(0) return self._save_cropped_image(out, original_image, crop_data) except SuperdeskApiError: raise except Exception as ex: raise SuperdeskApiError.badRequestError("Generating crop failed: {}".format(str(ex)))
def create_crop(self, original_image, crop_name, crop_data): """ Create a new crop based on the crop co-ordinates :param original: Article to add the crop :param crop_name: Name of the crop :param doc: Crop details :raises SuperdeskApiError.badRequestError :return dict: rendition """ original_file = superdesk.app.media.fetch_rendition(original_image) if not original_file: raise SuperdeskApiError.badRequestError( 'Original file couldn\'t be found') try: cropped, out = crop_image(original_file, crop_name, crop_data) crop = self.get_crop_by_name(crop_name) if not cropped: raise SuperdeskApiError.badRequestError('Saving crop failed.') # resize if needed if crop.get('width') or crop.get('height'): out, width, height = _resize_image(out, size=(crop.get('width'), crop.get('height'))) crop['width'] = width crop['height'] = height out.seek(0) return self._save_cropped_image(out, original_image, crop_data) except SuperdeskApiError: raise except Exception as ex: raise SuperdeskApiError.badRequestError( 'Generating crop failed: {}'.format(str(ex)))
def create(self, docs, **kwargs): ids = [] for doc in docs: item = doc.pop("item") crop = doc.pop("crop") orig = item["renditions"]["original"] size = get_crop_size(crop, orig.get("width", 800), orig.get("height", 600)) orig_file = get_file(orig, item) filename = get_random_string() ok, output = crop_image(orig_file, filename, crop, size) if ok: metadata = encode_metadata(process_image(orig_file)) metadata.update({"length": json.dumps(len(output.getvalue()))}) output = fix_orientation(output) media = app.media.put(output, filename=filename, content_type=orig["mimetype"], metadata=metadata) doc["href"] = app.media.url_for_media(media, orig["mimetype"]) doc["width"] = output.width doc["height"] = output.height doc["metadata"] = metadata ids.append(media) return ids
def publish_images(images, original, item): orig_file = get_file(original, item) for image in images: content_type = original['mimetype'] ok, output = crop_image(orig_file, image['file_name'], image['crop'], image['spec']) if ok: app.media.put(output, image['file_name'], content_type, _id=image['media'])
def create_crop(self, original, crop_name, crop_data): """ Create a new crop based on the crop co-ordinates :param original: Article to add the crop :param crop_name: Name of the crop :param doc: Crop details :raises SuperdeskApiError.badRequestError :return dict: modified renditions """ renditions = original.get('renditions', {}) original_crop = renditions.get(crop_name, {}) fields = ('CropLeft', 'CropTop', 'CropRight', 'CropBottom') crop_created = False if any(crop_data.get(name) != original_crop.get(name) for name in fields): original_image = renditions.get('original', {}) original_file = superdesk.app.media.fetch_rendition(original_image) if not original_file: raise SuperdeskApiError.badRequestError('Original file couldn\'t be found') try: cropped, out = crop_image(original_file, crop_name, crop_data) if not cropped: raise SuperdeskApiError.badRequestError('Saving crop failed.') renditions[crop_name] = self._save_cropped_image(out, original_image, crop_data) crop_created = True except SuperdeskApiError: raise except Exception as ex: raise SuperdeskApiError.badRequestError('Generating crop failed: {}'.format(str(ex))) return renditions, crop_created
def crop_and_store_file(self, doc, content, filename, content_type): # retrieve file name and metadata from file file_name, content_type, metadata = process_file_from_stream( content, content_type=content_type) # crop the file if needed, can change the image size was_cropped, out = crop_image(content, filename, doc) # the length in metadata could be updated if it was cropped if was_cropped: file_name, content_type, metadata_after_cropped = process_file_from_stream( out, content_type=content_type) # when cropped, metadata are reseted. Then we update the previous metadata variable metadata['length'] = metadata_after_cropped['length'] try: logger.debug('Going to save media file with %s ' % file_name) out.seek(0) file_id = app.media.put(out, filename=file_name, content_type=content_type, resource=self.datasource, metadata=metadata) doc['media'] = file_id doc['mimetype'] = content_type set_filemeta(doc, decode_metadata(metadata)) inserted = [doc['media']] file_type = content_type.split('/')[0] rendition_spec = config.RENDITIONS['avatar'] renditions = generate_renditions(out, file_id, inserted, file_type, content_type, rendition_spec, url_for_media) doc['renditions'] = renditions except Exception as io: for file_id in inserted: delete_file_on_error(doc, file_id) raise SuperdeskApiError.internalError( 'Generating renditions failed', exception=io)
def test_crop_image_resize(self): with open(self.img, mode="rb") as f: status, output = crop_image(f, "test", self.crop_data, { "width": 4, "height": 3 }) self.assertEqual(True, status) self.assertEqual(4, output.width) self.assertEqual(3, output.height)
def test_crop_image_exact_size(self): img = get_picture_fixture() size = {'width': '300', 'height': '200'} crop = {'CropTop': '0', 'CropRight': '300', 'CropBottom': '200', 'CropLeft': '0'} with open(img, 'rb') as imgfile: res = crop_image(imgfile, img, crop, size) self.assertTrue(res[0]) self.assertEqual(300, res[1].width) self.assertEqual(200, res[1].height)
def test_crop_image_resize(self): with open(self.img, mode='rb') as f: status, output = crop_image(f, 'test', self.crop_data, { 'width': 4, 'height': 3 }) self.assertEqual(True, status) self.assertEqual(4, output.width) self.assertEqual(3, output.height)
def test_crop_image_exact_size(self): img = get_picture_fixture() size = {'width': '300', 'height': '200'} crop = { 'CropTop': '0', 'CropRight': '300', 'CropBottom': '200', 'CropLeft': '0' } with open(img, 'rb') as imgfile: res = crop_image(imgfile, img, crop, size) self.assertTrue(res[0]) self.assertEqual(300, res[1].width) self.assertEqual(200, res[1].height)
def test_crop_image_exact_size(self): img = get_picture_fixture() size = {"width": "300", "height": "200"} crop = { "CropTop": "0", "CropRight": "300", "CropBottom": "200", "CropLeft": "0" } with open(img, "rb") as imgfile: res = crop_image(imgfile, img, crop, size) self.assertTrue(res[0]) self.assertEqual(300, res[1].width) self.assertEqual(200, res[1].height)
def create(self, docs, **kwargs): ids = [] for doc in docs: item = doc.pop('item') crop = doc.pop('crop') size = get_crop_size(crop) orig = item['renditions']['original'] orig_file = get_file(orig, item) filename = get_random_string() ok, output = crop_image(orig_file, filename, crop, size) if ok: media = app.media.put(output, filename, orig['mimetype']) doc['href'] = app.media.url_for_media(media, orig['mimetype']) doc['width'] = output.width doc['height'] = output.height ids.append(media) return ids
def create_crop(self, original, crop_name, doc): """ Create a new crop based on the crop co-ordinates :param original: Article to add the crop :param crop_name: Name of the crop :param doc: Crop details :raises SuperdeskApiError.badRequestError :return dict: modified renditions """ renditions = original.get('renditions', {}) crop_data = UploadService().get_cropping_data(doc) original_crop = renditions.get(crop_name, {}) fields = ('CropLeft', 'CropTop', 'CropRight', 'CropBottom') crop_created = False if any(crop_data[i] != original_crop.get(name) for i, name in enumerate(fields)): original_image = renditions.get('original', {}) original_file = superdesk.app.media.get( original_image.get('media'), 'upload') if not original_file: raise SuperdeskApiError.badRequestError( 'Original file couldn\'t be found') try: cropped, out = crop_image(original_file, original_file.name, crop_data) if not cropped: raise SuperdeskApiError.badRequestError( 'Saving crop failed.') renditions[crop_name] = self._save_cropped_image( out, original_file, doc) crop_created = True except SuperdeskApiError: raise except Exception as ex: raise SuperdeskApiError.badRequestError( 'Generating crop failed: {}'.format(str(ex))) return renditions, crop_created
def create_crop(self, original, crop_name, crop_data): """ Create a new crop based on the crop co-ordinates :param original: Article to add the crop :param crop_name: Name of the crop :param doc: Crop details :raises SuperdeskApiError.badRequestError :return dict: modified renditions """ renditions = original.get('renditions', {}) original_crop = renditions.get(crop_name, {}) fields = ('CropLeft', 'CropTop', 'CropRight', 'CropBottom') crop_created = False if any( crop_data.get(name) != original_crop.get(name) for name in fields): original_image = renditions.get('original', {}) original_file = superdesk.app.media.fetch_rendition(original_image) if not original_file: raise SuperdeskApiError.badRequestError( 'Original file couldn\'t be found') try: cropped, out = crop_image(original_file, crop_name, crop_data) crop = self.get_crop_by_name(crop_name) if not cropped: raise SuperdeskApiError.badRequestError( 'Saving crop failed.') # resize if needed if crop.get('width') or crop.get('height'): out, width, height = _resize_image( out, size=(crop.get('width'), crop.get('height'))) crop['width'] = width crop['height'] = height out.seek(0) renditions[crop_name] = self._save_cropped_image( out, original_image, crop_data) crop_created = True except SuperdeskApiError: raise except Exception as ex: raise SuperdeskApiError.badRequestError( 'Generating crop failed: {}'.format(str(ex))) return renditions, crop_created
def create(self, docs, **kwargs): ids = [] for doc in docs: item = doc.pop('item') crop = doc.pop('crop') orig = item['renditions']['original'] size = get_crop_size(crop, orig.get('width', 800), orig.get('height', 600)) orig_file = get_file(orig, item) filename = get_random_string() ok, output = crop_image(orig_file, filename, crop, size) if ok: metadata = encode_metadata(process_image(orig_file)) metadata.update({'length': json.dumps(len(output.getvalue()))}) output = fix_orientation(output) media = app.media.put(output, filename, orig['mimetype'], metadata=metadata) doc['href'] = app.media.url_for_media(media, orig['mimetype']) doc['width'] = output.width doc['height'] = output.height ids.append(media) return ids
def create(self, docs, **kwargs): ids = [] for doc in docs: item = doc.pop('item') crop = doc.pop('crop') size = get_crop_size(crop) orig = item['renditions']['original'] orig_file = get_file(orig, item) filename = get_random_string() ok, output = crop_image(orig_file, filename, crop, size) if ok: metadata = encode_metadata(process_image(orig_file)) metadata.update({'length': json.dumps(len(output.getvalue()))}) output = fix_orientation(output) media = app.media.put(output, filename, orig['mimetype'], metadata=metadata) doc['href'] = app.media.url_for_media(media, orig['mimetype']) doc['width'] = output.width doc['height'] = output.height ids.append(media) return ids
def test_crop_image_resize(self): with open(self.img, mode='rb') as f: status, output = crop_image(f, 'test', self.crop_data, {'width': 4, 'height': 3}) self.assertEqual(True, status) self.assertEqual(4, output.width) self.assertEqual(3, output.height)
def test_crop_image(self): with open(self.img, mode='rb') as f: status, output = crop_image(f, 'test', self.crop_data) self.assertEqual(True, status) self.assertEqual(8, output.width) self.assertEqual(6, output.height)
def test_crop_image(self): with open(self.img, mode="rb") as f: status, output = crop_image(f, "test", self.crop_data) self.assertEqual(True, status) self.assertEqual(8, output.width) self.assertEqual(6, output.height)