Search in sources :

Example 1 with WrappedBufferedImageServer

use of qupath.lib.images.servers.WrappedBufferedImageServer in project qupath by qupath.

the class ProjectImportImagesCommand method getThumbnailRGB.

// /**
// * Add a single ImageServer to a project, without considering sub-images.
// * <p>
// * This includes an optional attempt to request a thumbnail; if this fails, the image will not be added.
// *
// * @param project the project to which the entry should be added
// * @param server the server to add
// * @param type the ImageType that should be set for each entry being added
// * @return
// */
// public static ProjectImageEntry<BufferedImage> addSingleImageToProject(Project<BufferedImage> project, ImageServer<BufferedImage> server, ImageType type) {
// ProjectImageEntry<BufferedImage> entry = null;
// try {
// var img = getThumbnailRGB(server, null);
// entry = project.addImage(server);
// if (entry != null) {
// // Write a thumbnail if we can
// entry.setThumbnail(img);
// // Initialize an ImageData object with a type, if required
// if (type != null) {
// var imageData = new ImageData<>(server, type);
// entry.saveImageData(imageData);
// }
// }
// } catch (IOException e) {
// logger.warn("Error attempting to add " + server, e);
// }
// return entry;
// }
public static BufferedImage getThumbnailRGB(ImageServer<BufferedImage> server, ImageDisplay imageDisplay) throws IOException {
    var img2 = server.getDefaultThumbnail(server.nZSlices() / 2, 0);
    // Try to write RGB images directly
    boolean success = false;
    if (imageDisplay == null && (server.isRGB() || img2.getType() == BufferedImage.TYPE_BYTE_GRAY)) {
        return resizeForThumbnail(img2);
    }
    if (!success) {
        // Try with display transforms
        if (imageDisplay == null) {
            // By wrapping the thumbnail, we avoid slow z-stack/time series requests & determine brightness & contrast just from one plane
            var wrappedServer = new WrappedBufferedImageServer("Dummy", img2, server.getMetadata().getChannels());
            imageDisplay = new ImageDisplay(new ImageData<>(wrappedServer));
        // imageDisplay = new ImageDisplay(new ImageData<>(server));
        }
        for (ChannelDisplayInfo info : imageDisplay.selectedChannels()) {
            imageDisplay.autoSetDisplayRange(info);
        }
        img2 = imageDisplay.applyTransforms(img2, null);
        return resizeForThumbnail(img2);
    }
    return img2;
}
Also used : WrappedBufferedImageServer(qupath.lib.images.servers.WrappedBufferedImageServer) ImageData(qupath.lib.images.ImageData) ChannelDisplayInfo(qupath.lib.display.ChannelDisplayInfo) ImageDisplay(qupath.lib.display.ImageDisplay)

Example 2 with WrappedBufferedImageServer

use of qupath.lib.images.servers.WrappedBufferedImageServer in project qupath by qupath.

the class ImageWriterTools method writeImage.

/**
 * Write a 2D image using the default writer based on the file path.
 * @param img
 * @param path
 * @return
 * @throws IOException
 */
public static boolean writeImage(final BufferedImage img, final String path) throws IOException {
    String ext = GeneralTools.getExtension(new File(path)).orElse(null);
    List<ImageWriter<BufferedImage>> compatibleWriters = ImageWriterTools.getCompatibleWriters(new WrappedBufferedImageServer(UUID.randomUUID().toString(), img), ext);
    // If we have a path, use the 'best' writer we have, i.e. the first one that supports pixel sizes
    for (ImageWriter<BufferedImage> writer : compatibleWriters) {
        try {
            writer.writeImage(img, path);
            return true;
        } catch (Exception e) {
            logger.warn("Unable to write image", e);
        }
    }
    throw new IOException("Unable to write " + path + "!  No compatible writer found.");
}
Also used : WrappedBufferedImageServer(qupath.lib.images.servers.WrappedBufferedImageServer) IOException(java.io.IOException) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException)

Aggregations

WrappedBufferedImageServer (qupath.lib.images.servers.WrappedBufferedImageServer)2 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 IOException (java.io.IOException)1 ChannelDisplayInfo (qupath.lib.display.ChannelDisplayInfo)1 ImageDisplay (qupath.lib.display.ImageDisplay)1 ImageData (qupath.lib.images.ImageData)1