def generate_tiled_image(self): num_pictures = 0 if len(self.sources) >= 4: num_pictures = 4 elif len(self.sources) >= 1: num_pictures = 1 else: return None images = [ config.get_storage_path(f.get_filename()) for f in self.sources[:num_pictures] ] key = "TILED {}".format("+".join(sorted(images))) if not config.UPDATE and FILECACHE.get(key): return FILECACHE.get(key).decode('utf-8') config.LOGGER.info("\tTiling thumbnail for {}".format(self.node.title)) with tempfile.NamedTemporaryFile( suffix=".{}".format(file_formats.PNG)) as tempf: tempf.close() create_tiled_image(images, tempf.name) filename = "{}.{}".format(get_hash(tempf.name), file_formats.PNG) copy_file_to_storage(filename, tempf.name) FILECACHE.set(key, bytes(filename, "utf-8")) return filename
def test_raises_for_wrong_number_of_files(self, tmpdir): input_file = os.path.join(files_dir, 'file_that_does_not_exist.png') assert not os.path.exists(input_file) input_files = [input_file, input_file, input_file, input_file] output_file = tmpdir.join('tiled.png').strpath with pytest.raises(images.ThumbnailGenerationError): images.create_tiled_image(input_files, output_file)
def test_generates_brazil_thumbnail(self, tmpdir): input_file = os.path.join(files_dir, 'thumbnails', 'BRAlogo1.png') assert os.path.exists(input_file) input_files = [input_file, input_file, input_file, input_file] output_file = tmpdir.join('tiled.png').strpath images.create_tiled_image(input_files, output_file) self.check_16_9_format(output_file)
def test_generates_kolibris_thumbnail(self, tmpdir): filenames = ['BRAlogo1.png', 'toosquare.png', 'tootall.png', 'toowide.png'] input_files = [] for filename in filenames: input_file = os.path.join(files_dir, 'thumbnails', filename) assert os.path.exists(input_file) input_files.append(input_file) output_file = tmpdir.join('tiled.png').strpath images.create_tiled_image(input_files, output_file) self.check_16_9_format(output_file)
def create_tiled_image_wrapper(files, preset_id, node=None): ext = file_formats.PNG random.shuffle(files) if len(files) >= 4: files = files[:4] elif len(files) >= 1: files = files[:1] with tempfile.NamedTemporaryFile(suffix=".{}".format(ext)) as tempf: tempf.close() create_tiled_image(files, tempf.name) with open(tempf.name, 'rb') as tf: return create_file_from_contents(tf.read(), ext=ext, node=node, preset_id=preset_id)
def create_tiled_image_wrapper(files, preset_id, node=None): ext = file_formats.PNG random.shuffle(files) if len(files) >= 4: files = files[:4] elif len(files) >= 1: files = files[:1] with tempfile.NamedTemporaryFile(suffix=".{}".format(ext)) as tempf: tempf.close() create_tiled_image(files, tempf.name) with open(tempf.name, 'rb') as tf: return create_file_from_contents(tf.read(), ext=ext, node=node, preset_id=preset_id)
def create_tiled_image_wrapper(files, preset_id, node=None): ext = file_formats.PNG random.shuffle(files) if len(files) >= 4: files = files[:4] elif len(files) >= 1: files = files[:1] with tempfile.NamedTemporaryFile(suffix=".{}".format(ext)) as tempf: tempf.close() create_tiled_image(files, tempf.name) user = (len(files) > 0 and not isinstance(files[0], basestring) and files[0].uploaded_by) or None with open(tempf.name, 'rb') as tf: return create_file_from_contents(tf.read(), ext=ext, node=node, preset_id=preset_id, uploaded_by=user)
def create_tiled_image_wrapper(files, preset_id, node=None): ext = file_formats.PNG random.shuffle(files) if len(files) >= 4: files = files[:4] elif len(files) >= 1: files = files[:1] with tempfile.NamedTemporaryFile(suffix=".{}".format(ext)) as tempf: tempf.close() create_tiled_image(files, tempf.name) user = (len(files) > 0 and not isinstance(files[0], basestring) and files[0].uploaded_by) or None with open(tempf.name, 'rb') as tf: return create_file_from_contents(tf.read(), ext=ext, node=node, preset_id=preset_id, uploaded_by=user)
def generate_tiled_image(self): num_pictures = 0 if len(self.sources) >= 4: num_pictures = 4 elif len(self.sources) >= 1: num_pictures = 1 else: return None config.LOGGER.info("\tGenerating tiled thumbnail.") images = [ config.get_storage_path(f.get_filename()) for f in self.sources[:num_pictures] ] with tempfile.NamedTemporaryFile( suffix=".{}".format(file_formats.PNG)) as tempf: tempf.close() create_tiled_image(images, tempf.name) filename = "{}.{}".format(get_hash(tempf.name), file_formats.PNG) copy_file_to_storage(filename, tempf.name) return filename
def test_raises_for_invalid_png(self, tmpdir, bad_png_file): input_file = bad_png_file.name input_files = [input_file, input_file, input_file, input_file] output_file = tmpdir.join('tiled.png').strpath with pytest.raises(images.ThumbnailGenerationError): images.create_tiled_image(input_files, output_file)