def show_winners(self, num=10, columns=5, threshold=True):
   num = min(len(self.particles), num)
   rows = numpy.ceil(float(num) / columns)
   winners = -numpy.ones((rows * (self.feature_size[-2] + 1), 
                          columns * (self.feature_size[-1]+1) ))
   compressions = self.particle_compressions.copy()
   
   print "Winning compressions:",
   for i in range(num):
     max_part_idx = compressions.argmax()
     print compressions[max_part_idx],
     feat = self.particles[max_part_idx].feature
     if threshold:
       feat = (feat > 0.5).astype('float')
     compressions[max_part_idx] = -1
     
     row = i / columns
     col = i % columns
     winners[row * (self.feature_size[-2]+1) : (row+1) * (self.feature_size[-2]+1) - 1, 
             col * (self.feature_size[-1]+1) : (col+1) * (self.feature_size[-1]+1) - 1] = feat
   
   print
   
   from scipy.misc.pilutil import toimage
   toimage(winners).show()
Example #2
0
def save_img(image, subfolder, imgname, use_JPEG=False):
    """Save image as either .jpeg or .png"""

    if use_JPEG:
        imsave(
            '/data/ILSVRC2012/geirhos_uniform_noise/val_in_folders/' + '/' +
            subfolder + '/' + imgname, img_as_ubyte(image))
    else:
        toimage(image, cmin=0.0, cmax=1.0).save(
            '/data/ILSVRC2012/geirhos_uniform_noise/val_in_folders/' + '/' +
            subfolder + '/' + imgname + ".png")
Example #3
0
def difference(img, dest):
    fiarr = numpy.asarray(Image(img))
    fiarr = fiarr[..., ::-1]

    pilarr = numpy.asarray(pil_open(img))

    fiarr = fiarr.astype(numpy.int16)
    pilarr = pilarr.astype(numpy.int16)
    diff = numpy.absolute(fiarr - pilarr)

    toimage(diff).convert("L").save(dest)
Example #4
0
def generate_whitenedspace(m,numstimuli,fig_num=1):

    rvals = np.random.randn(m.M,numstimuli)
    patches = np.dot(m.dewhitenmatrix,rvals)

    array = display_patches(patches,m.patch_sz,fig_num=fig_num)

    savepath = os.path.join(fig_dir,m.model_name + '_' + m.tstring)
    if not os.path.isdir(savepath): os.makedirs(savepath)
    fname = os.path.join(savepath, 'Whitened_patches.png')
    toimage(np.floor(.5*(array+1)*255)).save(fname)
Example #5
0
def generate_sparsespace(m,numstimuli,sparsity=5.,fig_num=2):

    binomial_p = float(sparsity)/m.NN

    rvals = np.random.randn(m.NN,numstimuli)
    rvals *= np.random.binomial(1,binomial_p,size=rvals.shape)

    patches = np.dot(m.dewhitenmatrix,np.dot(m.A,rvals))

    array = display_patches(patches,m.patch_sz,fig_num=fig_num)

    savepath = os.path.join(fig_dir,m.model_name + '_' + m.tstring)
    if not os.path.isdir(savepath): os.makedirs(savepath)
    fname = os.path.join(savepath, 'Sparse_patches_%d.png'%int(sparsity))
    toimage(np.floor(.5*(array+1)*255)).save(fname)
 def show_features(self, num=10, columns=5):
   num = min(len(self.features), num)
   rows = numpy.ceil(float(num) / columns)
   winners = -numpy.ones((rows * (self.feature_size[-2] + 1), 
                          columns * (self.feature_size[-1]+1) ))
   
   for i in range(num):
     feat = self.features[i]
     row = i / columns
     col = i % columns
     winners[row * (self.feature_size[-2]+1) : (row+1) * (self.feature_size[-2]+1) - 1, 
             col * (self.feature_size[-1]+1) : (col+1) * (self.feature_size[-1]+1) - 1] = feat
   
   from scipy.misc.pilutil import toimage
   toimage(winners).show()
Example #7
0
    def load_gnt_file(filename):
        """
        Load characters and images from a given GNT file.
        :param filename: The file path to load.
        :return: (image: Pillow.Image.Image, character) tuples
        """

        # Thanks to nhatch for the code to read the GNT file, available at https://github.com/nhatch/casia
        with open(filename, "rb") as f:
            while True:
                packed_length = f.read(4)
                if packed_length == b'':
                    break

                length = struct.unpack("<I", packed_length)[0]
                raw_label = struct.unpack(">cc", f.read(2))
                width = struct.unpack("<H", f.read(2))[0]
                height = struct.unpack("<H", f.read(2))[0]
                photo_bytes = struct.unpack("{}B".format(height * width),
                                            f.read(height * width))

                # Comes out as a tuple of chars. Need to be combined. Encoded as gb2312, gotta convert to unicode.
                label = decode(raw_label[0] + raw_label[1], encoding="gb2312")
                # Create an array of bytes for the image, match it to the proper dimensions, and turn it into an image.
                image = toimage(np.array(photo_bytes).reshape(height, width))

                yield image, label
Example #8
0
def nparrayToQPixmap(arrayImage):
    pilImage = toimage(arrayImage.astype('float'))
    qtImage = ImageQt(pilImage.convert("RGBA"))
    qImage =  QtGui.QImage(qtImage)
    qPixmap =  QtGui.QPixmap(qImage)
  
    return qPixmap
Example #9
0
def on_button_press(event):
  global vcmImage

  # get the yuyv image data
  yuyv = vcmImage.get_yuyv();
  # data is actually int32 (YUYV format) not float64
  yuyv.dtype = 'uint32';
  n = yuyv.shape[0];
  # convert to uint8 to seperate out YUYV
  yuyv.dtype = 'uint8';
  # reshape to Nx4
  yuyv_u8 = yuyv.reshape((120, 80, 4));
  # convert to ycbcr (approx.)
  ycbcr = yuyv_u8[0:-1:2, :, [0,1,3]];
  # convert to rgb
  # there is probably a better way to do this...
  rgb = np.asarray(pilutil.toimage(ycbcr, mode='YCbCr').convert('RGB').getdata());
  rgb = rgb.reshape((60, 80, 3))/255.0;

  # Get the labelA data
  labelA = vcmImage.get_labelA();
  # data is actually uint8 (one bit per label)
  labelA.dtype = 'uint8';
  n = yuyv.shape[0];
  labelA = labelA.reshape(  (60,80) );
#  labelA = permute(  labelA, [2 1]   );


  # display image
  draw_data(rgb, labelA)
Example #10
0
def on_button_press(event):
    global vcmImage

    # get the yuyv image data
    yuyv = vcmImage.get_yuyv()
    # data is actually int32 (YUYV format) not float64
    yuyv.dtype = 'uint32'
    n = yuyv.shape[0]
    # convert to uint8 to seperate out YUYV
    yuyv.dtype = 'uint8'
    # reshape to Nx4
    yuyv_u8 = yuyv.reshape((120, 80, 4))
    # convert to ycbcr (approx.)
    ycbcr = yuyv_u8[0:-1:2, :, [0, 1, 3]]
    # convert to rgb
    # there is probably a better way to do this...
    rgb = np.asarray(
        pilutil.toimage(ycbcr, mode='YCbCr').convert('RGB').getdata())
    rgb = rgb.reshape((60, 80, 3)) / 255.0

    # Get the labelA data
    labelA = vcmImage.get_labelA()
    # data is actually uint8 (one bit per label)
    labelA.dtype = 'uint8'
    n = yuyv.shape[0]
    labelA = labelA.reshape((60, 80))
    #  labelA = permute(  labelA, [2 1]   );

    # display image
    draw_data(rgb, labelA)
Example #11
0
 def plot_sampling(self, sampled_windows):
   overlap = numpy.zeros(self.image_shape, 'int')
   centers = numpy.zeros(self.image_shape, 'int')
   for pos in sampled_windows:
     overlap[pos[0] : pos[0] + self.feature_shape[0],
             pos[1] : pos[1] + self.feature_shape[1]] += 1
     centers[pos[0] + self.feature_radius,
             pos[1] + self.feature_radius] += 1
     
   from scipy.misc.pilutil import toimage
   toimage(overlap).show()
   toimage(centers).show()
   
   print "max overlap:", overlap.max()
   print "min overlap:", overlap.min()
   print "avg feature density:", float(overlap.sum()) / overlap.size
Example #12
0
def nparrayToQPixmap(arrayImage):
    pilImage = toimage(arrayImage)
    
    qtImage = ImageQt(pilImage.convert("RGBA"))
    qImage = QImage(qtImage)
    qPixmap = QPixmap(qImage)
    return qPixmap
Example #13
0
    def startCamera(self):
        # capture frames from the camera
        for frame in self.camera.capture_continuous(self.rawCapture,
                                                    format="rgb",
                                                    use_video_port=True):

            # grab the raw NumPy array representing the image, convert to greyscale by averaging RGB
            image = frame.array
            greyscaleImage = np.zeros((self.imageres[1], self.imageres[0]),
                                      dtype=np.float32)
            np.mean(image, axis=2, dtype=np.float32, out=greyscaleImage)

            #OpenCV initialization: needed for video rendering!
            key = cv2.waitKey(1) & 0xFF

            # convert RGB image np array to qPixmap and update video
            #converts nparray to qpixmap
            pilImage = toimage(greyscaleImage)
            qtImage = ImageQt(pilImage)
            qImage = QtGui.QImage(qtImage)
            qPixmap = QtGui.QPixmap(qImage)
            videoy = int(self.screenres[0] / 2.1)
            videox = int(1.333 * videoy)
            self.videowindow.setPixmap(qPixmap.scaled(videox, videoy))

            #Breaks video capture loop if snapshot is being taken
            if (self.snapFlag):
                break

            # clear the stream in preparation for the next frame
            self.rawCapture.truncate(0)

        #Recursively restarts this function on completion
        if (self.snapFlag):
            self.takeSnap()
Example #14
0
def draw(cars,n):
	dat = np.zeros( (n,n,3))
	redcar = cars[:,cars[2,] == 1]
	bluecar = cars[:,cars[2,] == 2]
	dat[redcar[0,:],redcar[1:],] = [255,0,0]
	dat[bluecar[0,:],bluecar[1:],] = [0,0,255]
	img = smp.toimage( dat )
	img.show()
Example #15
0
def convert_profile_numpy_transform(image_np, transform):
    if (not have_pilutil) or (not have_cms):
        return image_np

    in_image_pil = pilutil.toimage(image_np)
    convert_profile_pil_transform(in_image_pil, transform, inPlace=True)
    image_out = pilutil.fromimage(in_image_pil)
    return image_out
Example #16
0
def convert_profile_numpy(image_np, inprof_path, outprof_path):
    if not have_pilutil:
        return image_np

    in_image_pil = pilutil.toimage(image_np)
    out_image_pil = pilutil.convert_profile_pil(in_image_pil, inprof_path,
                                                outprof_path)
    image_np = pilutil.fromimage(out_image_pil)
    return image_out
Example #17
0
def convert_profile_numpy(image_np, inprof_path, outprof_path):
    if (not have_pilutil) or (not have_cms):
        return image_np

    in_image_pil = pilutil.toimage(image_np)
    out_image_pil = convert_profile_pil(in_image_pil,
                                        inprof_path, outprof_path)
    image_out = pilutil.fromimage(out_image_pil)
    return image_out
Example #18
0
 def save_image(self, fname):
     """Captures image and saves to format guessed from filename extension"""
     im = self.capture_image()
     base, ext = os.path.splitext(fname)
     if ext == '.npy':
         numpy.save(fname, im)
     else:
         im = toimage(im)
         im.save(fname)
Example #19
0
def nparrayToQPixmap(array_image):
    """
    Converts an numpy image (W, H, 3) into a Qt pixmap.
    """

    pil_image = toimage(array_image)
    qtImage = ImageQt(pil_image)
    if len(array_image.shape) == 3:
        frm = QtGui.QImage.Format_ARGB32
    else:
        frm = QtGui.QImage.Format_Mono
    q_image = QtGui.QImage(qtImage).convertToFormat(frm)
    q_pixmap = QtGui.QPixmap(q_image)
    return q_pixmap
def out(colour, num):
	global default, pixel_size
	data = default
	for x in range(len(colour)):
		for y in range(len(colour[0])):
			if colour[x][y] == 0:
				for c in range(pixel_size):
					for d in range(pixel_size):
						data[pixel_size*x+c,pixel_size*y+d] = USEDLINE_COLOUR
			elif colour[x][y] == 2:
				for c in range(pixel_size):
					for d in range(pixel_size):
						data[pixel_size*x+c,pixel_size*y+d] = NEWLINE_COLOUR
	img = smp.toimage(data)
	misc.imsave("maze"+str(num).zfill(5)+".jpg", img)
def main():
        from random import randint
        print "Main"
        ps = load_sieve()
        print len(ps)

        # Create a 1024x1024x3 array of 8 bit unsigned integers
        data = np.zeros( (10000,3000,3), dtype=np.uint8 )
        for i in range(0, 10000):
                #print i
                s = itopfi(i,ps)
                #print s
                data[i] = bit_line(s)

        img = smp.toimage( data )       # Create a PIL image
        img.show()                      # View in default viewer
Example #22
0
    def export_palette(self,pal,name='exported_palette.png'):
        """ Given a palette, export it as a png. """
        import scipy.misc.pilutil as smp
        import PIL
        width = len(pal)
        height = self.height
        # make output image data array
        imdata = np.zeros( (height,width,3), dtype=np.uint8 )
        # fill with palette data
        for i in range(width):
            for j in range(height):
                #print "frame " + repr(frame[i][j])
                #print "pal " + repr(pal[i])
                imdata[j,i] = pal[i][0:3]

        img = smp.toimage(imdata)
        img.save(name, 'PNG')
Example #23
0
    def getnextSeispixmap(self):

        print(self.presentline, self.ilines[self.presentline])
        self.scaleFactor_x = 4
        self.scaleFactor_y = 0.3

        # image_data =  np.random.randint(255, size=(200, 400))

        image_data = self.src.iline[self.ilines[self.presentline]]
        print(np.sum(image_data))
        pilImage = toimage(image_data.T)
        qtImage = ImageQt(pilImage)
        # print(image_data)
        image = QImage(qtImage)
        image = image.scaled(self.scaleFactor_x * image.width(),
                             self.scaleFactor_y * image.height())
        return QPixmap.fromImage(image)
    def updateVideo(self):
        frame = self.client.get_frame()

        if frame is not None:
            if self.client.use_color:
                if self.obmp is None:
                    self.obmp = wx.BitmapFromBuffer(self.client.width,
                                                  self.client.height,
                                                  frame
                                                  )
                else:
                    self.obmp.CopyFromBuffer(frame)
                wimg = self.obmp.ConvertToImage()
            else:
#            print frame.dtype, frame.shape
                img = toimage(frame)
                wimg = wx.EmptyImage(img.size[0], img.size[1])

                wimg.SetData(img.convert('RGB').tostring())
            self.bmp = self._set_bitmap_size(wimg)

        self.Refresh()
 def plot_sampling(self, sampled_windows):
   suppressed = numpy.zeros(self.image_shape, 'int')
   overlap = numpy.zeros(self.image_shape, 'int')
   centers = numpy.zeros(self.image_shape, 'int')
   for pos in sampled_windows:
     suppressed[self.get_slice(pos, self.suppression_radius)] += 1
     overlap[self.get_slice(pos, self.feature_radius)] += 1
     centers[pos] += 1
     
   from scipy.misc.pilutil import toimage
   toimage(suppressed).show()
   toimage(overlap).show()
   toimage(centers).show()
   
   print "max overlap:", overlap.max()
   print "avg feature density:", float(overlap.sum()) / overlap.size
   print "avg suppressed density:", float(suppressed.sum()) / suppressed.size
Example #26
0
def crop(folder, shape):
  """ Converts all images in the given dataset folder to grayscale. """

  img_path = os.path.join(DATA_PATH, folder)
  images = [f for f in os.listdir(img_path) \
            if ('.bmp' in f or '.jpg' in f or '.png' in f)]
  
  i = 0
  while images:
    category = images.pop()
    imgfile = os.path.join(img_path, category)
    
    img = numpy.array(Image.open(imgfile))
    if img.shape[-2:] == shape or img.shape[:2] == shape: continue
    
    if len(img.shape)==2:
      left = (img.shape[-2] - shape[0])/2
      top = (img.shape[-1] - shape[1])/2
      cropped = img[left:left+shape[0], top:top+shape[1]]
      
    elif img.shape[2]==3:
      left = (img.shape[0] - shape[0])/2
      top = (img.shape[1] - shape[1])/2
      cropped = img[left:left+shape[0], top:top+shape[1], :]
      
    elif img.shape[0]==3:
      left = (img.shape[1] - shape[0])/2
      top = (img.shape[2] - shape[1])/2
      cropped = img[:, left:left+shape[0], top:top+shape[1]]
    
    cropped = toimage(cropped)
    cropped.save(imgfile)
    
    i += 1
    if i%100 == 0: print i
  print "Total converted:", i
  
  print colored("Conversion complete", "green")
 def plot_sampling(self, sampled_windows):
   suppressed = numpy.zeros(self.image_shape, 'int')
   overlap = numpy.zeros(self.image_shape, 'int')
   centers = numpy.zeros(self.image_shape, 'int')
   supstart = self.feature_radius - self.suppression_radius
   for pos in sampled_windows:
     suppressed[pos[0] + supstart : pos[0] + supstart + 1 + 2*self.suppression_radius,
                pos[1] + supstart : pos[1] + supstart + 1 + 2*self.suppression_radius] += 1
     overlap[pos[0] : pos[0] + self.feature_shape[0],
             pos[1] : pos[1] + self.feature_shape[1]] += 1
     centers[pos[0] + self.feature_radius,
             pos[1] + self.feature_radius] += 1
     
   from scipy.misc.pilutil import toimage
   toimage(suppressed).show()
   toimage(overlap).show()
   toimage(centers).show()
   
   print "max overlap:", overlap.max()
   print "min overlap:", overlap.min()
   print "avg feature density:", float(overlap.sum()) / overlap.size
   print "avg suppressed density:", float(suppressed.sum()) / suppressed.size
Example #28
0
def spectrogram(request, recordingId):
    recording = Recording.objects.get(pk=int(recordingId))
    audioFile = "/data/django/openmir/audio/%s.wav" % (str(recording.name))
    startSec = float(request.GET.get('startSec', '0'))
    endSec = float(request.GET.get('endSec', '1.000'))
    lowHz = int(request.GET.get('lowHz', '0'))
    highHz = int(request.GET.get('highHz', 44100 / 2))

    # Variables from request
    winSize = int(request.GET.get('winSize', '1024'))
    # TODO(sness) - Make hopSize work
    hopSize = winSize
    #    hopSize = int(request.GET.get('hopSize', '1024'))
    width = request.GET.get('width', 'native')
    height = request.GET.get('height', 'native')
    spectrumType = request.GET.get('spectrumType', 'decibels')
    #spectrumType = request.GET.get('spectrumType', 'magnitude')

    # Marsyas network
    mng = marsyas.MarSystemManager()

    net = mng.create("Series","series")
    net.addMarSystem(mng.create("SoundFileSource", "src"))
    net.addMarSystem(mng.create("Stereo2Mono", "s2m"));
    net.addMarSystem(mng.create("ShiftInput", "si"));
    net.addMarSystem(mng.create("Windowing", "win"));
    net.addMarSystem(mng.create("Spectrum","spk"));
    net.addMarSystem(mng.create("PowerSpectrum","pspk"))

    # Update Marsyas controls
    net.updControl("PowerSpectrum/pspk/mrs_string/spectrumType",
                   marsyas.MarControlPtr.from_string(str(spectrumType)))
    net.updControl("SoundFileSource/src/mrs_string/filename",
                   marsyas.MarControlPtr.from_string(audioFile))
    net.updControl("SoundFileSource/src/mrs_natural/inSamples", hopSize)
    net.updControl("ShiftInput/si/mrs_natural/winSize", winSize)
    net.updControl("mrs_natural/inSamples", int(hopSize))

    # Sample rate and samples per tick
    networkSampleRate = net.getControl("mrs_real/osrate").to_real()
    soundFileSampleRate = net.getControl("SoundFileSource/src/mrs_real/osrate").to_real()
    insamples = net.getControl("SoundFileSource/src/mrs_natural/inSamples").to_natural()
    
    # Calculate values
    samplesToSkip = int(soundFileSampleRate * (startSec))
    durationSec = (endSec - startSec)
    ticksToRun = int(durationSec * networkSampleRate)
    _height = winSize / 2

    # Move to the correct position in the file
    net.updControl("SoundFileSource/src/mrs_natural/moveToSamplePos", samplesToSkip)

    # The array to be displayed to the user
    out = np.zeros( (_height,ticksToRun), dtype=np.double )

    # Tick the network until we are done
    for x in range(0,ticksToRun):
        net.tick()
        data = net.getControl("mrs_realvec/processedData").to_realvec()
        for y in range(0,_height):
            out[(_height - y - 1),x] = data[y]

    # Normalize and make black on white    
    out /= np.max(np.abs(out))
    out = 1.0 - out

    nyquist = 44100 / 2.;
    bins = out.shape[0]
    lowBin = int((bins / nyquist) * lowHz);
    highBin = int((bins / nyquist) * highHz);

    halfWinSize = int(hopSize / 2)
    out = out[halfWinSize - highBin:halfWinSize - lowBin, :]

    # Resize and convert the array to an image
    if (height == "native") and (width == "native"):
        height = winSize / 2
        width = hopSize * durationSec

    if (height != "native") and (width == "native"):
        pxPerItem = int(height) / float(winSize / 2.)
        # TODO(sness) - Why do we have to multiply this by 4?  Check the math above
        width = int(ticksToRun * pxPerItem) * 4

    out = smp.imresize(out,(int(height),int(width)))
    im = smp.toimage(out)

    # Output a png
    response = HttpResponse(mimetype="image/png")
    im.save(response, "PNG")

    return response
Example #29
0
                /cumsum_pdf[iidash],scalar)
        h1[ii] = np.log(numpy.sum(temp1)+eps)
        temp2 = np.power(pdf[iidash+1:255]
                /(1-cumsum_pdf[iidash]),
                scalar)
        h2[ii] = np.log(numpy.sum(temp2)+eps)

    T = h1+h2
    # Entropy value is calculated
    T = -T*scalar
    # location where the maximum entropy 
    # occurs is the threshold for the renyi entropy
    location = T.argmax(axis=0) 
    # location value is used as the threshold
    thresh = location 
    return thresh
	

# Main program
# opening the image and converting it to grayscale
a = Image.open('CT.png').convert('L')
# a is converted to an ndarray
a = fromimage(a)
# computing the threshold by calling the function
thresh = renyi_seg_fn(a,3)
b = a > thresh
# b is converted from an ndarray to an image 
b = pilutil.toimage(b)  
# saving the image as renyi_output.png
b.save('figures/renyi_output.png')
        for j in range(shape[1]):
            if world[i][j] < -0.5:
                color_world[i][
                    j] = orange  # Sets all of the areas of the noise map that are less than -0.5 to orange

            elif world[i][j] > 0.35:
                color_world[i][
                    j] = red  # Sets all of the areas of the noise map that are greater than -0.35 to red

            elif world[i][j] < 1.0:
                color_world[i][
                    j] = green  # Sets all of the areas of the noise map that are less than 1 to green

    return color_world


if user_input == 1:  # This gathers the user input for what type of tile they want
    world_output = wet_grass(world)

elif user_input == 2:
    world_output = snowy_grass(world)

elif user_input == 3:
    world_output = autumn_grass(world)
else:
    print("Error, invalid input. Please try again"
          )  # This is some basic input validation

toimage(world_output).show(
)  # This opens the image file once it has been generated
Example #31
0
 def show_points(self, imsize):
     """ Auxiliary function to visualize sampled points """
     mask = np.zeros(imsize)
     for t in self.points:
         mask[t] = 1
     toimage(mask).show()
Example #32
0
from django.core.management import setup_environ
import clouds.settings
setup_environ(clouds.settings)
import datetime
from clouds.models import Image, RealPoint, SidPoint, Line, SidTime 
from django.db.models import Sum, Max, Count, Avg 
import os, sys
from django.db import reset_queries, connection

from process import open_fits
import numpy
import scipy.misc.pilutil as smp

imgs = numpy.dstack(
    [
        open_fits(os.path.join('out', 'fits_filtered', image.get_file()+'.fits'))
        for image in Image.objects.filter(moon=False)[:1000]
    ])

median = numpy.percentile(imgs, 10, axis=2) 
img = smp.toimage(median)#, cmax=3000)
img.save('pixelimage.png')
Example #33
0
 def nparrayToQPixmap(self, arrayImage):
 	pilImage = toimage(arrayImage)
 	qtImage = ImageQt(pilImage)
 	qImage = QtGui.QImage(qtImage)
 	qPixmap = QtGui.QPixmap(qImage)
 	return qPixmap
Example #34
0
reStreams = re.compile(r".*Number of streams: (\d+).*")

for line in sys.stdin:
    matchDone = reDone.match(line)
    matchBlocks = reBlocks.match(line)
    matchStreams = reStreams.match(line)
    if matchDone:
        who = int(matchDone.group(1)) - 2
        num = int(matchDone.group(2))
        data[num % streams, num / streams] = [50*who,255-(50*who),100+40*who]
        progress += 1
        newProgress = (progress * 100.0) / (blocks * 1.0)
        if (newProgress - prevProgress > 10.0):
            print "%02f%%" % newProgress
            prevProgress = newProgress
            img = smp.toimage( data )
            pylab.imshow(img)
            pylab.show()
    changed = False
    if matchBlocks:
        blocks = int(matchBlocks.group(1))
        changed = True
    if matchStreams:
        streams = int(matchStreams.group(1))
        changed = True
    if changed:
        width = streams
        height = blocks / streams
        data = np.zeros( (width,height,3), dtype=np.uint8 )

Example #35
0
def spectrogram(request, recordingId):
    recording = Recording.objects.get(pk=int(recordingId))
    audioFile = "/data/django/openmir/audio/%s.wav" % (str(recording.name))
    startSec = float(request.GET.get('startSec', '0'))
    endSec = float(request.GET.get('endSec', '1.000'))
    lowHz = int(request.GET.get('lowHz', '0'))
    highHz = int(request.GET.get('highHz', 44100 / 2))

    # Variables from request
    winSize = int(request.GET.get('winSize', '1024'))
    # TODO(sness) - Make hopSize work
    hopSize = winSize
    #    hopSize = int(request.GET.get('hopSize', '1024'))
    width = request.GET.get('width', 'native')
    height = request.GET.get('height', 'native')
    spectrumType = request.GET.get('spectrumType', 'decibels')
    #spectrumType = request.GET.get('spectrumType', 'magnitude')

    # Marsyas network
    mng = marsyas.MarSystemManager()

    net = mng.create("Series", "series")
    net.addMarSystem(mng.create("SoundFileSource", "src"))
    net.addMarSystem(mng.create("Stereo2Mono", "s2m"))
    net.addMarSystem(mng.create("ShiftInput", "si"))
    net.addMarSystem(mng.create("Windowing", "win"))
    net.addMarSystem(mng.create("Spectrum", "spk"))
    net.addMarSystem(mng.create("PowerSpectrum", "pspk"))

    # Update Marsyas controls
    net.updControl("PowerSpectrum/pspk/mrs_string/spectrumType",
                   marsyas.MarControlPtr.from_string(str(spectrumType)))
    net.updControl("SoundFileSource/src/mrs_string/filename",
                   marsyas.MarControlPtr.from_string(audioFile))
    net.updControl("SoundFileSource/src/mrs_natural/inSamples", hopSize)
    net.updControl("ShiftInput/si/mrs_natural/winSize", winSize)
    net.updControl("mrs_natural/inSamples", int(hopSize))

    # Sample rate and samples per tick
    networkSampleRate = net.getControl("mrs_real/osrate").to_real()
    soundFileSampleRate = net.getControl(
        "SoundFileSource/src/mrs_real/osrate").to_real()
    insamples = net.getControl(
        "SoundFileSource/src/mrs_natural/inSamples").to_natural()

    # Calculate values
    samplesToSkip = int(soundFileSampleRate * (startSec))
    durationSec = (endSec - startSec)
    ticksToRun = int(durationSec * networkSampleRate)
    _height = winSize / 2

    # Move to the correct position in the file
    net.updControl("SoundFileSource/src/mrs_natural/moveToSamplePos",
                   samplesToSkip)

    # The array to be displayed to the user
    out = np.zeros((_height, ticksToRun), dtype=np.double)

    # Tick the network until we are done
    for x in range(0, ticksToRun):
        net.tick()
        data = net.getControl("mrs_realvec/processedData").to_realvec()
        for y in range(0, _height):
            out[(_height - y - 1), x] = data[y]

    # Normalize and make black on white
    out /= np.max(np.abs(out))
    out = 1.0 - out

    nyquist = 44100 / 2.
    bins = out.shape[0]
    lowBin = int((bins / nyquist) * lowHz)
    highBin = int((bins / nyquist) * highHz)

    halfWinSize = int(hopSize / 2)
    out = out[halfWinSize - highBin:halfWinSize - lowBin, :]

    # Resize and convert the array to an image
    if (height == "native") and (width == "native"):
        height = winSize / 2
        width = hopSize * durationSec

    if (height != "native") and (width == "native"):
        pxPerItem = int(height) / float(winSize / 2.)
        # TODO(sness) - Why do we have to multiply this by 4?  Check the math above
        width = int(ticksToRun * pxPerItem) * 4

    out = smp.imresize(out, (int(height), int(width)))
    im = smp.toimage(out)

    # Output a png
    response = HttpResponse(mimetype="image/png")
    im.save(response, "PNG")

    return response
 def __call__(self, x: np.array):
     x = x.astype(np.float32) / 255
     x = np.stack([x] * 3, axis=2)
     return toimage(x, channel_axis=2)
def reset():
    img = ImageTk.PhotoImage(smp.toimage( map_vis ))
    #-------------------------------------------------
    canvas.create_image(0,0,image=img,anchor="nw")
Example #38
0
import numpy as np

class ColorPalette:
    palette = [
    ]

    def __init__(self, startColor = None, endColor = None):
        if startColor != None and endColor != None:
            self.palette = self.generatePalette(startColor, endColor)
        print self.palette
        self.packed = np.array(self.palette).astype(np.int8).tostring()

    def generatePalette(self, startColor, endColor):
       r = np.linspace(startColor[0], endColor[0], 256) 
       g = np.linspace(startColor[1], endColor[1], 256) 
       b = np.linspace(startColor[2], endColor[2], 256)
       return np.array([r,g,b]).T
        

startColor = [0, 128, 40]
endColor = [255, 255, 0]
mainPalette = ColorPalette(startColor, endColor)

import scipy.misc.pilutil as smp
bitmap=np.zeros([256, 256, 3])
for i in range(256):
    bitmap[:,i,:]= mainPalette.palette[i]
img = smp.toimage(bitmap )       
img.show() 
Example #39
0
 def plot_masks(self):
   """ Displays all of the masks involved in this transform. """
   from scipy.misc.pilutil import toimage
   for mask in self.masks:
     toimage(mask).show()
Example #40
0
import numpy as np
import scipy.misc.pilutil as smp

# Create a 1024x1024x3 array of 8 bit unsigned integers
data = np.zeros((1024, 1024, 3), dtype=np.uint8)

data[512, 512] = [254, 0, 0]  # Makes the middle pixel red
data[512, 513] = [0, 0, 255]  # Makes the next pixel blue

img = smp.toimage(data)  # Create a PIL image
img.show()  # View in default viewer
Example #41
0
 def nparrayToQPixmap(self, arrayImage):
     pilImage = toimage(arrayImage)
     qtImage = ImageQt(pilImage)
     qImage = QtGui.QImage(qtImage)
     qPixmap = QtGui.QPixmap(qImage)
     return qPixmap
Example #42
0
width, height = im.size
print(width)
print(height)
#load image, build mask filled with 0
img = cv2.imread("data/orig3.jpg")
mask = np.zeros(img.shape[:2], np.uint8)
#creat background model and foreground model
bgdModel = np.zeros((1, 65), np.float64)
fgdModel = np.zeros((1, 65), np.float64)
#user define a rectangle
rect = (300, 500, 500, 800)
cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5,
            cv2.GC_INIT_WITH_RECT)  #5 is iteration
mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype("uint8")
img = img * mask2[:, :, np.newaxis]

# visualize the image
img = pilutil.toimage(img).convert('LA')  #转换为灰度图,十分重要!
width = img.size[0]  #长度
height = img.size[1]  #宽度
for i in range(0, width):  #遍历所有长度的点
    for j in range(0, height):  #遍历所有宽度的点
        data = (img.getpixel((i, j)))  #打印该图片的所有点
        print(data)  #打印每个像素点的颜色RGBA的值(r,g,b,alpha)
        print(data[0])  #打印RGBA的r值
        # RGB的r值不等于0的,改为白色
        if (data[0] != 0 and data[1] != 0):
            img.putpixel((i, j), (255, 255))

img.save('grabcut-iter5.png')
img.show()
 def show_points(self, imsize):
     """ Auxiliary function to visualize sampled points """
     mask = np.zeros(imsize)
     for t in self.points:
         mask[t] = 1
     toimage(mask).show()
Example #44
0
 def save(self, filename, data):
     im = toimage(data)
     im.save(filename)
    #yscroll.config(command=canvas.yview)
    frame.pack(fill=BOTH,expand=1)

    #adding the image
    #File = askopenfilename(parent=root, initialdir="C:/",title='Choose an image.')
    #img = ImageTk.PhotoImage(Image.open(File))
    # ------------------------------------------------
    map_vis, map_info = open_files()
    #create_robot_goal(goal, map_vis)
    #create_robot_origin(origin, map_vis)
    map_info, map_vis = mapBuffer(map_info, map_vis)

    #store_changes(map_info, map_vis)

    #show_map(map_vis)
    img = ImageTk.PhotoImage(smp.toimage( map_vis ))
    img2 = ImageTk.PhotoImage(smp.toimage( map_vis ))
    #-------------------------------------------------
    canvas.create_image(0,0,image=img,anchor="nw",tag='image')
    #canvas.config(scrollregion=canvas.bbox(ALL))

    #function to be called when mouse is clicked
    def printcoords(event):
        #outputting x and y coords to console
        #canvas.delete('image')
        #create_robot_goal((3,3), map_vis)
        #create_robot_origin((5,5), map_vis)
        #img = ImageTk.PhotoImage(smp.toimage( map_vis ))
        #canvas.create_image(0,0,image=img,anchor="nw",tag='image')
        
        #canvas.create_image(0,0,image=img2,anchor="nw",tag='image')
Example #46
0
 def png(self, path, data, image_filter=flatten_max(5000)):
     img = smp.toimage(image_filter(data))
     img.save(join(self.outdir, "png", path + ".png"))
Example #47
0
import numpy as np


class ColorPalette:
    palette = []

    def __init__(self, startColor=None, endColor=None):
        if startColor != None and endColor != None:
            self.palette = self.generatePalette(startColor, endColor)
        print self.palette
        self.packed = np.array(self.palette).astype(np.int8).tostring()

    def generatePalette(self, startColor, endColor):
        r = np.linspace(startColor[0], endColor[0], 256)
        g = np.linspace(startColor[1], endColor[1], 256)
        b = np.linspace(startColor[2], endColor[2], 256)
        return np.array([r, g, b]).T


startColor = [0, 128, 40]
endColor = [255, 255, 0]
mainPalette = ColorPalette(startColor, endColor)

import scipy.misc.pilutil as smp
bitmap = np.zeros([256, 256, 3])
for i in range(256):
    bitmap[:, i, :] = mainPalette.palette[i]
img = smp.toimage(bitmap)
img.show()
Example #48
0
def run(audioFile, outFile, startSec, endSec, lowHz, highHz, winSize, hopSize,
        spectrumType, widthPx, heightPx):

    # Marsyas network
    mng = marsyas.MarSystemManager()

    net = mng.create("Series", "series")
    net.addMarSystem(mng.create("SoundFileSource", "src"))
    net.addMarSystem(mng.create("Stereo2Mono", "s2m"))
    net.addMarSystem(mng.create("ShiftInput", "si"))
    net.addMarSystem(mng.create("Windowing", "win"))
    net.addMarSystem(mng.create("Spectrum", "spk"))
    net.addMarSystem(mng.create("PowerSpectrum", "pspk"))

    # Update Marsyas controls
    net.updControl("PowerSpectrum/pspk/mrs_string/spectrumType",
                   marsyas.MarControlPtr.from_string(str(spectrumType)))
    net.updControl("SoundFileSource/src/mrs_string/filename",
                   marsyas.MarControlPtr.from_string(audioFile))
    net.updControl("SoundFileSource/src/mrs_natural/inSamples", hopSize)
    net.updControl("ShiftInput/si/mrs_natural/winSize", winSize)
    net.updControl("mrs_natural/inSamples", int(hopSize))

    # Sample rate and samples per tick
    networkSampleRate = net.getControl("mrs_real/osrate").to_real()
    soundFileSampleRate = net.getControl(
        "SoundFileSource/src/mrs_real/osrate").to_real()
    insamples = net.getControl(
        "SoundFileSource/src/mrs_natural/inSamples").to_natural()

    # Calculate values
    samplesToSkip = int(soundFileSampleRate * (startSec))
    durationSec = (endSec - startSec)
    ticksToRun = int(durationSec * networkSampleRate)
    _height = winSize / 2

    # Move to the correct position in the file
    net.updControl("SoundFileSource/src/mrs_natural/moveToSamplePos",
                   samplesToSkip)

    # The array to be displayed to the user
    out = np.zeros((_height, ticksToRun), dtype=np.double)

    # Tick the network until we are done
    for x in range(0, ticksToRun):
        net.tick()
        data = net.getControl("mrs_realvec/processedData").to_realvec()
        for y in range(0, _height):
            out[(_height - y - 1), x] = data[y]

    # Normalize and make black on white
    out /= np.max(np.abs(out))
    out = 1.0 - out

    nyquist = 44100 / 2.
    bins = out.shape[0]
    lowBin = int((bins / nyquist) * lowHz)
    highBin = int((bins / nyquist) * highHz)

    halfWinSize = int(hopSize / 2)
    out = out[halfWinSize - highBin:halfWinSize - lowBin, :]

    # Resize and convert the array to an image
    if (heightPx == 0) and (widthPx == 0):
        heightPx = winSize / 2
        widthPx = hopSize * durationSec

    if (heightPx != 0) and (widthPx == 0):
        pxPerItem = int(heightPx) / float(winSize / 2.)
        # TODO(sness) - Why do we have to multiply this by 4?  Check the math above
        widthPx = int(ticksToRun * pxPerItem) * 4

    out = smp.imresize(out, (int(heightPx), int(widthPx)))
    im = smp.toimage(out)
    im.save(outFile, "PNG")
def show_map(map_vis):
	img = smp.toimage( map_vis )
	img.show()