Пример #1
0
def NumpytoIM(img, usm=None, verbose=False):
    if verbose:
        print "Converting numpy array to ImageMagick"

    out_img = PMImage()
    if img.dtype == 'uint16':
        out_img.depth(16)
    else:
        out_img.depth(8)
    out_img.magick('RGB')
    h,w,c = img.shape
    size_str = str(w)+'x'+str(h)
    out_img.size(size_str)

    b = Blob()
    b.data = img.tostring()
    out_img.read(b)
    out_img.magick('PNG')

    # Check if USM sharpening should be used
    if usm != None:
        if verbose:
            print "Running unsharp mask filter"
        r,s,a,t = (usm)
        out_img.unsharpmask(r,s,a,t)

    return out_img
Пример #2
0
    def thumbnail(self, infile, outfile, size, quality, no_upscale=False):
        #image = Image(infile)
        #image.resize(size)
        #image.write(outfile)

        quality = str(quality)
        if infile.endswith('.gif') or no_upscale:
            size = size+'>'
        resize = run(['/usr/bin/convert', '-interlace', "Plane", '-quality', quality, '-strip',  '-thumbnail', size, infile, outfile])
        image = Image(outfile)

        return { 'width': image.size().width(), \
                 'height': image.size().height() }
Пример #3
0
    def thumbnail(self, infile, outfile, size, quality, no_upscale=False):
        #image = Image(infile)
        #image.resize(size)
        #image.write(outfile)

        quality = str(quality)
        if infile.endswith('.gif') or no_upscale:
            size = size + '>'
        resize = run([
            '/usr/bin/convert', '-interlace', "Plane", '-quality', quality,
            '-strip', '-thumbnail', size, infile, outfile
        ])
        image = Image(outfile)

        return { 'width': image.size().width(), \
                 'height': image.size().height() }
Пример #4
0
def to_imagemagick(img, bits=16):
    '''Convert numpy array to Imagemagick format.

    :param img: image to convert
    :type img: Numpy ndarray
    :rtype: PythonMagick.Image
    '''

    if not isinstance(img, PMImage):

        img = _scale(img, bits=bits)

        LOGGER.debug("Converting from Numpy to ImageMagick.")
        out_img = PMImage()
        if img.dtype == np.uint8:
            out_img.depth(8)
        else:
            out_img.depth(16)

        shape = img.shape
        # Convert also B&W images to 3-channel arrays
        if len(shape) == 2:
            tmp = np.empty((shape[0], shape[1], 3), dtype=img.dtype)
            tmp[:, :, 0] = img
            tmp[:, :, 1] = img
            tmp[:, :, 2] = img
            img = tmp
        out_img.magick('RGB')
        out_img.size(str(shape[1]) + 'x' + str(shape[0]))
        blob = Blob()
        blob.data = img.tostring()

        out_img.read(blob)
        out_img.magick('PNG')

        return out_img
    return img
Пример #5
0
    def resize(self, req, width, height):
        file_path = req.cfg.attachments_path + '/' + self.webname()
        if self.mime_type.startswith('image') \
                and guess_extension(self.mime_type) in _image_exts:

            img = Image(file_path.encode('utf-8'))
            img.fileName(
                'image.'+self.file_name.encode('utf-8').split('.')[-1])
            size = img.size()

            blob = Blob()
            if width > size.width() and height > size.height():
                img.write(blob)
            else:
                img.scale(Geometry(width, height))
                img.write(blob)
            return blob.data
        elif guess_extension(self.mime_type) == '.svg':
            with open(file_path.encode('utf-8'), 'rb') as svg:
                return svg.read()
        else:
            req.log_error(self.mime_type)
            return None
Пример #6
0
# Set the named file as first entry in the image list
# This file will be the base for alignment and intensity normalization
if base_image is not None:
    idx = frames.index(base_image)
    f = frames.pop(idx)
    frames.insert(0,f)

if verbose:
    print "%s will be used as base image" % frames[0]

# Determine image size
base_img_fname = frames[0]
if verbose:
    print "Reading %s" % base_img_fname
img = PMImage(base_img_fname)
w,h = img.size().width(), img.size().height()
if img.monochrome():
    c = 1
else:
    c = 3

print "Detected image size is %d x %d (width x height)" % (w,h)

print "%s will be used as base image" % frames[0]
img = IMtoNumpy(img,pre_usm,verbose=verbose)

# Get data used for aligning images
align_ref_data = None
if align_ref_loc is not None:
    xc = align_ref_loc[0]
    yc = align_ref_loc[1]