d._pp2.append(400,200) # d._pp1.append(100,300) d._pp2.append(100,300) d._pp1.append(100,400) d._pp2.append(200,300) d._pp1.append(200,400) d._pp2.append(200,400) # d._pp1.append(300,400) d._pp2.append(300,400) d._pp1.append(400,400) d._pp2.append(300,300) d._pp1.append(400,300) d._pp2.append(400,300) else: # Identity transform d._pp1.append(100,100) d._pp2.append(100,100) if d: d.apply() if False: ## a = d._a3 t = a.wobjects[1] im = t._texture1._dataRef vv.imwrite('~/warped.jpg', im[::1,::1]/255)
elif len(image.shape) == 3: if image.shape[2] in [3, 4]: pass # RGB or RGBA else: raise ValueError("Cannot write image: Too many values in third dim.") else: raise ValueError("Cannot write image: Invalid number of dimensions.") # check type -> convert if image.dtype.name == 'uint8': pass # ok elif image.dtype.name in ['float32', 'float64']: image = image.copy() image[image<0] = 0 image[image>1] = 1 image = (image*255).astype(np.uint8) else: image = image.astype(np.uint8) # write image if imageio: imageio.imsave(filename, image, format) elif PIL: pim = PIL.Image.fromarray(image) pim.save(filename, format) if __name__ == '__main__': im = vv.imread('lena.png') vv.imwrite('lena_new.jpg', im)
if image.shape[2] in [3, 4]: pass # RGB or RGBA else: raise ValueError( "Cannot write image: Too many values in third dim.") else: raise ValueError("Cannot write image: Invalid number of dimensions.") # check type -> convert if image.dtype.name == 'uint8': pass # ok elif image.dtype.name in ['float32', 'float64']: image = image.copy() image[image < 0] = 0 image[image > 1] = 1 image = (image * 255).astype(np.uint8) else: image = image.astype(np.uint8) # write image if imageio: imageio.imsave(filename, image, format) elif PIL: pim = PIL.Image.fromarray(image) pim.save(filename, format) if __name__ == '__main__': im = vv.imread('lena.png') vv.imwrite('lena_new.jpg', im)
def screenshot(filename, ob=None, sf=2, bg=None, format=None, tension=-0.25): """ screenshot(filename, ob=None sf=2, bg=None, format=None) Make a screenshot and store it to a file, using cubic interpolation to increase the resolution (and quality) of the image. Parameters ---------- filename : string The name of the file to store the screenshot to. If filename is None, the interpolated image is returned as a numpy array. ob : Axes, AxesContainer, or Figure The object to take the screenshot of. The AxesContainer can be obtained using vv.gca().parent. It can be usefull to take a screeshot of an axes including thickmarks and labels. sf : integer The scale factor. The image is increased in size with this factor, using a high quality interpolation method. A factor of 2 or 3 is recommended; the image quality does not improve with higher factors. If using a sf larger than 1, the image is best saved in the jpg format. bg : 3-element tuple or char The color of the background. If bg is given, ob.bgcolor is set to bg before the frame is captured. format : string The format for the screenshot to be saved in. If not given, the format is deduced from the filename. Notes ----- Uses vv.getframe(ob) to obtain the image in the figure or axes. That image is interpolated with the given scale factor (sf) using bicubic interpolation. Then vv.imwrite(filename, ..) is used to store the resulting image to a file. Rationale --------- We'd prefer storing screenshots of plots as vector (eps) images, but the nature of OpenGl prevents this. By applying high quality interpolation (using a cardinal spline), the resolution can be increased, thereby significantly improving the visibility/smoothness for lines and fonts. Use this to produce publication quality snapshots of your plots. """ # The tension controls the # responsivenes of the filter. The more negative, the more overshoot, # but the more it is capable to make for example font glyphs smooth. # If tension is 0, the interpolator is a Catmull-Rom spline. # Scale must be integer s = int(sf) # Object given? if ob is None: ob = vv.gcf() # Get figure fig = ob if not hasattr(ob, 'DrawNow'): fig = ob.GetFigure() # Get object to set background of bgob = ob # Set background if bg and fig: bgOld = bgob.bgcolor bgob.bgcolor = bg fig.DrawNow() # Obtain image im1 = vv.getframe(ob) shape1 = im1.shape # Return background if bg and fig: bgob.bgcolor = bgOld fig.Draw() # Pad original image, so we have no trouble at the edges shape2 = shape1[0]+2, shape1[1]+2, 3 im2 = np.zeros(shape2, dtype=np.float32) # Also make float im2[1:-1,1:-1,:] = im1 im2[0,:,:] = im2[1,:,:] im2[-1,:,:] = im2[-2,:,:] im2[:,0,:] = im2[:,1,:] im2[:,-1,:] = im2[:,-2,:] # Create empty new image. It is sized by the scaleFactor, # but the last row is not. shape3 = (shape1[0]-1)*s+1, (shape1[1]-1)*s+1, 3 im3 = np.zeros(shape3, dtype=np.float32) # Fill in values! for dy in range(s+1): for dx in range(s+1): # Get interpolation fraction and coefs ty = float(dy)/s tx = float(dx)/s cy = getCardinalSplineCoefs(ty, tension) cx = getCardinalSplineCoefs(tx, tension) # Create tmp image to which we add the contributions # Note that this image is 1 pixel smaller in each dimension. # The last pixel is filled because dy and dx iterate INCLUDING s. shapeTmp = shape1[0]-1, shape1[1]-1, 3 imTmp = np.zeros(shapeTmp, dtype=np.float32) # Collect all 16 neighbours and weight them apropriately for iy in range(4): for ix in range(4): # Get weight w = cy[iy]*cx[ix] if w==0: continue # Get slice. Note that we start at 0,1,2,3 rather than # -1,0,1,2, because we padded the image. D = {0:-3,1:-2,2:-1,3:None} slicey = slice(iy, D[iy]) slicex = slice(ix, D[ix]) # Get contribution and add to temp image imTmp += w * im2[slicey, slicex, :] # Store contributions D = [-1 for tmp in range(s)]; D.append(None) slicey = slice(dy,D[dy],s) slicex = slice(dx,D[dx],s) im3[slicey, slicex, :] = imTmp # Correct for overshoot im3[im3>1]=1 im3[im3<0]=0 # Store image to file if filename is not None: vv.imwrite(filename, im3, format) else: return im3
Write image (numpy array) to file, requires imageio. Parameters ---------- filename : string The name of the file to store the screenshot to. If filename is None, the interpolated image is returned as a numpy array. image : numpy array The image to write. format : string The format for the image to be saved in. If not given, the format is deduced from the filename. Notes ----- * For floating point images, 0 is considered black and 1 is white. * For integer types, 0 is considered black and 255 is white. """ if imageio is None: raise RuntimeError("visvis.imwrite requires the imageio package.") imageio.imwrite(filename, image, format) if __name__ == '__main__': im = vv.imread('astronaut.png') vv.imwrite('astronaut_new.jpg', im)
def screenshot(filename, ob=None, sf=2, bg=None, format=None, tension=-0.25): """ screenshot(filename, ob=None sf=2, bg=None, format=None) Make a screenshot and store it to a file, using cubic interpolation to increase the resolution (and quality) of the image. Parameters ---------- filename : string The name of the file to store the screenshot to. If filename is None, the interpolated image is returned as a numpy array. ob : Axes, AxesContainer, or Figure The object to take the screenshot of. The AxesContainer can be obtained using vv.gca().parent. It can be usefull to take a screeshot of an axes including thickmarks and labels. sf : integer The scale factor. The image is increased in size with this factor, using a high quality interpolation method. A factor of 2 or 3 is recommended; the image quality does not improve with higher factors. If using a sf larger than 1, the image is best saved in the jpg format. bg : 3-element tuple or char The color of the background. If bg is given, ob.bgcolor is set to bg before the frame is captured. format : string The format for the screenshot to be saved in. If not given, the format is deduced from the filename. Notes ----- Uses vv.getframe(ob) to obtain the image in the figure or axes. That image is interpolated with the given scale factor (sf) using bicubic interpolation. Then vv.imwrite(filename, ..) is used to store the resulting image to a file. Rationale --------- We'd prefer storing screenshots of plots as vector (eps) images, but the nature of OpenGl prevents this. By applying high quality interpolation (using a cardinal spline), the resolution can be increased, thereby significantly improving the visibility/smoothness for lines and fonts. Use this to produce publication quality snapshots of your plots. """ # The tension controls the # responsivenes of the filter. The more negative, the more overshoot, # but the more it is capable to make for example font glyphs smooth. # If tension is 0, the interpolator is a Catmull-Rom spline. # Scale must be integer s = int(sf) # Object given? if ob is None: ob = vv.gcf() # Get figure fig = ob if not hasattr(ob, 'DrawNow'): fig = ob.GetFigure() # Get object to set background of bgob = ob # Set background if bg and fig: bgOld = bgob.bgcolor bgob.bgcolor = bg fig.DrawNow() # Obtain image im1 = vv.getframe(ob) shape1 = im1.shape # Return background if bg and fig: bgob.bgcolor = bgOld fig.Draw() # Pad original image, so we have no trouble at the edges shape2 = shape1[0] + 2, shape1[1] + 2, 3 im2 = np.zeros(shape2, dtype=np.float32) # Also make float im2[1:-1, 1:-1, :] = im1 im2[0, :, :] = im2[1, :, :] im2[-1, :, :] = im2[-2, :, :] im2[:, 0, :] = im2[:, 1, :] im2[:, -1, :] = im2[:, -2, :] # Create empty new image. It is sized by the scaleFactor, # but the last row is not. shape3 = (shape1[0] - 1) * s + 1, (shape1[1] - 1) * s + 1, 3 im3 = np.zeros(shape3, dtype=np.float32) # Fill in values! for dy in range(s + 1): for dx in range(s + 1): # Get interpolation fraction and coefs ty = float(dy) / s tx = float(dx) / s cy = getCardinalSplineCoefs(ty, tension) cx = getCardinalSplineCoefs(tx, tension) # Create tmp image to which we add the contributions # Note that this image is 1 pixel smaller in each dimension. # The last pixel is filled because dy and dx iterate INCLUDING s. shapeTmp = shape1[0] - 1, shape1[1] - 1, 3 imTmp = np.zeros(shapeTmp, dtype=np.float32) # Collect all 16 neighbours and weight them apropriately for iy in range(4): for ix in range(4): # Get weight w = cy[iy] * cx[ix] if w == 0: continue # Get slice. Note that we start at 0,1,2,3 rather than # -1,0,1,2, because we padded the image. D = {0: -3, 1: -2, 2: -1, 3: None} slicey = slice(iy, D[iy]) slicex = slice(ix, D[ix]) # Get contribution and add to temp image imTmp += w * im2[slicey, slicex, :] # Store contributions D = [-1 for tmp in range(s)] D.append(None) slicey = slice(dy, D[dy], s) slicex = slice(dx, D[dx], s) im3[slicey, slicex, :] = imTmp # Correct for overshoot im3[im3 > 1] = 1 im3[im3 < 0] = 0 # Store image to file if filename is not None: vv.imwrite(filename, im3, format) else: return im3
def test_im_read_write(): import visvis as vv im = vv.imread('astronaut.png') assert im.shape == (512, 512, 3) vv.imwrite(os.path.expanduser('~/astronaut2.png'), im)
# Create figure with two axes ##label1 = vv.Label(a1, 'This is a Label') ##label1.position = 10, 10 ##label1.bgcolor = (0.5, 1, 0.5) #t1 = vv.Text(a1, 'Visvis text', 0.2, 0, 0, 'mono', 20) #t = vv.imshow(im2, axes=a1) t = vv.imshow(im) t.aa = 2 # more anti-aliasing (default=1) t.interpolate = True # interpolate pixels vv.imwrite('pat.png', im) im4 = Image.open('pat.png') draw = ImageDraw.Draw(im4) fsz = int(20) sz = fsz + int(4) font = ImageFont.truetype("/Program Files/Microsoft Office 15/root/vfs/Fonts/private/arialn.ttf", fsz) for i in range(0, use_len): j = uval[i][1] #im[:y1i+y0i,i*x1i:(i+1)*x1i] = img_lst[j][:y1i+y0i,:x1i] #text = "Display "+bmarkf+" sub-benchmark for interval, BW" if cpu_typ == 0: txt = ("GUOPS: %.3f bill uops/sec" % uval[i][0]) else: txt = ("GIPS: %.3f bill instr/sec" % uval[i][0]) draw.text((i*x1i, fsz), txt, (0,0,0), font=font)