def _save(self, filename): if type(filename) is not str: raise ValueError("file name must be a string") with open(filename, 'wb') as f: rows, cols = len(self.image_data), len(self.image_data[0]) writer = Writer(cols, rows) pixels = _unbox(self.image_data) writer.write(f, pixels) print(filename, "saved.")
def imwrite(path, img, dtype=None, cmap=None, **kwargs): ''' accepted file types are... for 8 and 16 bit grayscale and RGB images: Windows bitmaps - *.bmp, *.dib JPEG files - *.jpeg, *.jpg, *.jpe JPEG 2000 files - *.jp2 Portable Network Graphics - *.png WebP - *.webp Portable image format - *.pbm, *.pgm, *.ppm Sun rasters - *.sr, *.ras TIFF files - *.tiff, *.tif for binary (bool) masks: *.png for 32 bit float images: *.tiff dtype = (None, float,bool) all other dtypes are converted to either uint8 or uint16 cmap = display grayscale as rgb using colormap: 'flame', 'gnuplot2' etc. ''' if dtype in (float, np.float64, np.float32): assert path.endswith('.tiff') or path.endswith( '.tif'), 'float arrays can only be saved as TIFF/TIF images' from PIL import Image Image.fromarray(np.asfarray(img)).save(path) # ... PIL is about 3x faster than tifffile.imwrite elif dtype in (bool, np.bool): # this method at about 10x slower than cv2.imwrite # however, it generates 8x smaller images (1bit vs 8bit) assert path.endswith( '.png'), 'boolean masks can only be saved as PNG images' assert img.ndim == 2, 'can only save grayscale images as type(bool)' from png import Writer with open(path, 'wb') as f: s0, s1 = img.shape w = Writer(s1, s0, greyscale=True, bitdepth=1) if not img.dtype == np.uint16: img = img.astype(np.uint8) w.write(f, img) else: img = toUIntArray(img, dtype=dtype, **kwargs) if cmap is not None: assert img.ndim == 2, '<cmap> can only be used for grayscale images' img = applyColorMap(img, cmap) img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) return cv2.imwrite(path, img)
def write(self): slope = (1.0 * self.height) / self.width pixels = numpy.zeros((self.height, self.width), dtype=int) # # Something similar to http://en.wikipedia.org/wiki/Xiaolin_Wu's_line_algorithm # but special cased, since I know the lines are mirrored thrice. # actualY = 0 for leftX in range(0, (self.width / 2) + 1): # Precalculating. Math! frac = actualY - int(actualY) topColor = self.getColor(1 - frac) bottomColor = self.getColor(frac) topY = int(actualY) bottomY = self.height - topY - 1 rightX = self.width - leftX - 1 # Actual Line (top-left) pixels[topY, leftX] = topColor pixels[topY + 1, leftX] = bottomColor # Horizontal Flip (top-right) pixels[topY, rightX] = topColor pixels[topY + 1, rightX] = bottomColor # Vertical Flip (bottom-left) pixels[bottomY, leftX] = topColor pixels[bottomY - 1, leftX] = bottomColor # 180-degree Rotation pixels[bottomY, rightX] = topColor pixels[bottomY - 1, rightX] = bottomColor # Increment `actualY` actualY += slope # Worry about the border (avoids another loop) if self.border: pixels[0, leftX] = Placeholder.BACKGROUND pixels[self.height - 1, leftX] = Placeholder.BACKGROUND pixels[0, rightX] = Placeholder.BACKGROUND pixels[self.height - 1, rightX] = Placeholder.BACKGROUND if leftX > 1: pixels[1, leftX] = Placeholder.FOREGROUND pixels[self.height - 2, leftX] = Placeholder.FOREGROUND pixels[1, rightX] = Placeholder.FOREGROUND pixels[self.height - 2, rightX] = Placeholder.FOREGROUND if leftX == 1: for y in range(1, self.height - 1): pixels[y, leftX] = Placeholder.FOREGROUND pixels[y, rightX] = Placeholder.FOREGROUND if self.metadata: self.addMetadata(pixels) with open(self.out, 'wb') as f: w = Writer(self.width, self.height, background=self.colors[0], palette=self.colors, bitdepth=8) w.write(f, pixels)
from numpy import zeros, ones, sin, cos, uint8, pi from sys import argv from tqdm import tqdm from png import Writer try: from PIL.Image import open as iopen except ImportError: from Image import open as iopen class DoublePendulum(): def __init__(self, thetas): self.t, self.td, self.l, self.m = thetas, zeros(thetas.shape), ones(thetas.shape), ones(thetas.shape) def step(self): self.td[:, 0] += (-10 * (2 * self.m[:, 0] + self.m[:, 1]) * sin(self.t[:, 0]) - self.m[:, 1] * 10 * sin(self.t[:, 0] - 2 * self.t[:, 1]) - 2 * sin(self.t[:, 0] - self.t[:, 1]) * self.m[:, 1] * (self.td[:, 1] ** 2 * self.l[:, 1] + self.td[:, 0] ** 2 * self.l[:, 0] * cos(self.t[:, 0] - self.t[:, 1]))) / (2000 * self.l[:, 0] * (2 * self.m[:, 0] + self.m[:, 1] * (1 - cos(2 * (self.t[:, 0] - self.t[:, 1]))))) self.td[:, 1] += (2 * sin(self.t[:, 0] - self.t[:, 1]) * (self.td[:, 0] ** 2 * self.l[:, 0] * (self.m[:, 0] + self.m[:, 1]) + 10 * (self.m[:, 0] + self.m[:, 1]) * cos(self.t[:, 0]) + self.td[:, 1] ** 2 * self.l[:, 1] * self.m[:, 1] * cos(self.t[:, 0] - self.t[:, 1]))) / (2000 * self.l[:, 1] * (2 * self.m[:, 0] + self.m[:, 1] * (1 - cos(2 * (self.t[:, 0] - self.t[:, 1]))))) self.t += self.td / 2000 color = lambda x, y: (127 * cos(x / 4 - y / 4), 127 * (cos(x / 4 - y / 4) - sin(x / 4 + y / 4)), 127 * (sin(x / 4 - y / 4) + cos(x / 4 + y / 4))) thetas = ones((893025, 2)) for i in range(893025): thetas[i, 0], thetas[i, 1] = pi * (2 * (i % 945) / 945 - 1), pi * (2 * (i // 945) / 945 - 1) p, frames, FRAMES = DoublePendulum(thetas), [], int(argv[1]) for frame in tqdm(range(FRAMES)): fractal, file, writer = uint8([[a for i in range(945) for a in color(p.t[j * 945 + i, 0], p.t[j * 945 + i, 1])] for j in range(945)]), open(f'frames/frame{frame}.png', 'wb+'), Writer(945, 945, greyscale = False) writer.write(file, fractal) frames += [iopen(file)] for _ in range(33): p.step() frames[0].save('output.gif', format = 'GIF', append_images = frames[1:], save_all = True, duration = FRAMES / 60)
def main(): comm = MPI.COMM_WORLD id= comm.Get_rank() wsize= comm.Get_size() tstart = MPI.Wtime() fsky = open("skymap.png","r") reader = Reader(fsky) skypixelwidth, skypixelheight, skypixels, metadata=reader.read_flat() pixelwidth = int(argv[1]) pixelheight = int(argv[2]) tskymapstart = MPI.Wtime() telepixels = np.zeros((pixelwidth*pixelheight*3),dtype=np.uint8) colorpixels = np.zeros((pixelwidth*pixelheight),dtype=np.uint8) skystartall = np.zeros((pixelwidth*pixelheight),dtype=np.uint32) telestartall = np.zeros((pixelwidth*pixelheight),dtype=np.uint32) colorall = np.zeros((pixelwidth*pixelheight),dtype=np.uint8) totnstepsall=np.zeros((wsize),dtype=np.uint32) tskymapend = MPI.Wtime() tskymap = tskymapend-tskymapstart tmin = 1.e6 tpercparmin=1.e6 hinit=1.e-1 #h=1.e-4 Router = 1000. Rplane = 700. Rs = 2. every = 1 deltalamb = 1.e-1 imagewidth = 50; imageheight = 50; tiny = 1.e-30 epsilon=1.e-8 eccentricity = 0.2 Rfac = 1.+1.e-10 heps = 1.e-14 semilatusr = 10.0 tstartpp=MPI.Wtime() #percent parallelized numperprocess = pixelheight*pixelwidth/wsize skystart=np.zeros((numperprocess),dtype=np.int32) telestart=np.zeros((numperprocess),dtype=np.int32) color = np.zeros((numperprocess),dtype=np.int8) totnsteps=np.zeros((numperprocess),dtype=np.int32) trk4all=np.zeros((numperprocess),dtype=np.float) ttelestop = MPI.Wtime() ttele = ttelestop-tstartpp trk4=float("inf") for index in range(numperprocess): ypix = int((id*numperprocess+index)/pixelwidth) xpix = (id*numperprocess+index)%pixelwidth tstartrk4=MPI.Wtime() totnsteps[index],skystart[index],telestart[index],color[index]=integrateNullGeodesic(xpix, ypix, pixelheight,pixelwidth, skypixelheight,skypixelwidth,imagewidth,imageheight,Rs,Router,Rplane,eccentricity, semilatusr, epsilon, tiny, hinit,Rfac,heps) tendrk4=MPI.Wtime() trk4=min(trk4,(tendrk4-tstartrk4)/float(totnsteps[index])) totnstepsmax=max(totnsteps) tstoppp = MPI.Wtime() tpercpar=tstoppp-tstartpp comm.Barrier() if id==0: totnstepsmaxall=0 else: totnstepsmaxall=None comm.Barrier() totnstepsmaxall=comm.reduce(totnstepsmax,op=MPI.MAX,root=0) tskymapall = comm.reduce(tskymap, op=MPI.MAX, root=0) tteleall = comm.reduce(ttele,op=MPI.MAX,root=0) comm.Gatherv(skystart,skystartall,root=0) comm.Gatherv(telestart, telestartall, root=0) comm.Gatherv(color,colorall, root=0) trk4min=comm.reduce(trk4,op=MPI.MIN,root=0) comm.Barrier() tend = MPI.Wtime() tall = tend-tstart if id==0: tindexstart = MPI.Wtime() for index in range(pixelheight*pixelwidth): if(colorall[index]==1): telepixels[telestartall[index]:telestartall[index]+3]=skypixels[skystartall[index]:skystartall[index]+3] else: telepixels[telestartall[index]]=255 #leave other two indices zero,red tindexend = MPI.Wtime() tindex = tindexend-tindexstart if id==0: twritestart = MPI.Wtime() ftele = open('teleview_{pw}_{ph}_{ws}.png'.format(pw=pixelwidth,ph=pixelheight,ws=wsize), "w") telewrite=Writer(width=pixelwidth,height=pixelheight,greyscale=False,alpha=False) telewrite.write_array(ftele,telepixels) ftele.close() twriteend=MPI.Wtime() twrite = twriteend-twritestart fsky.close() comm.Barrier() tmax = comm.reduce(tall,MPI.MAX,root=0) tpercparmin = comm.reduce(tpercpar/tall,op=MPI.MIN,root=0) comm.Barrier() if (id==0): # print("Telescope dimensions in M", 2.*imagewidth, 2.*imageheight) # print("Telescope resolution", pixelwidth, pixelheight) # print("Skymap resolution", skypixelwidth, skypixelheight) # print("Schwarzschild radius in M", 2.*Rs) # print("Outer radius in M", 2.*Router) # print("Telescope radius in M", 2.*Rplane) # print("Number of processes = ",wsize) # print("Maximum number of integration steps taken is",totnstepsmaxall) # print("The time for a single step of the RK4 is",trk4min) # print("Total runtime = ",tmax) # print("Fraction parallel = ", tpercparmin) print pixelwidth,pixelheight,wsize,totnstepsmaxall,trk4min,tmax,tpercparmin, tindex, twrite, tskymapall, tteleall MPI.Finalize()
def compress_image(img,w,h,f): from png import Writer w = Writer(w, h, greyscale = False) w.write(f,img)
def _preprocessing(self, sequence): """Preprocessing of SYNTHIA data. Performs several steps: - Original images are of size 1280x760. To make training and data loading faster, we resize them to a width of 640 (factor 2). However, picture dimensions need to be a multiple of 16 to pass through 4 levels of pooling in VGG16, therefore we have to crop the image a little bit in height and end up at a size of 640x368 - Resizing is done with boilinear interpolation for RGB and taking the upper- left corner of every group of pixels for depth and label. - Labels are stored in a crude version of the png format. We open this, extract the label integer from the first channel (we omit the item ID as we do not use it) and store it as numpy file. - From the available images, we select a random 20% sample as a test-set and store the indexes of the train/test split so they are consistent between training runs. """ print('INFO: Preprocessing started for {}. This may take a while.'. format(sequence)) sequence_basepath = path.join(self.base_path, sequence) # First we create directories for the downsamples images. for modality, direction in itertools.product( ['RGB', 'Depth', 'labels'], ['F', 'B', 'L', 'R']): new_images = path.join( sequence_basepath, 'resized_{}_{}'.format(modality.lower(), direction)) if path.exists(new_images): # We are doing a fresh preprocessing, so delete old data. shutil.rmtree(new_images) mkdir(new_images) # RGB and Depth Images can simply be resized by PIL. if modality in ['RGB', 'Depth']: original_images = path.join( sequence_basepath, '{}/Stereo_Right/Omni_{}'.format(modality, direction)) # As we have to handle different bitdepths and cropped images, we use # pypng for writing the new files. bitdepth = 8 if modality == 'RGB' else 16 writer = Writer(width=640, height=368, bitdepth=bitdepth, greyscale=(modality == 'Depth')) for filename in listdir(original_images): if modality == 'RGB': image = Image.open(path.join(original_images, filename)) resized = image.resize([640, 380], resample=Image.BILINEAR) resized = np.asarray(resized, dtype=np.uint8) elif modality == 'Depth': image = one_channel_image_reader( path.join(original_images, filename), np.uint16) resized = zoom(image, 0.5, mode='nearest', order=0) # The image has to get cropped, see docstring of crop method cropped = crop_resized_image(resized) # Now save again accoding to different formats with open(path.join(new_images, filename), 'wb') as f: # The writer is expecting a consecutive list of RGBRGBR... values if modality == 'RGB': val_list = cropped.reshape(368, 640 * 3).tolist() elif modality == 'Depth': val_list = cropped.tolist() writer.write(f, val_list) continue # Label image has a weird two-channel format where the 1st channel is the # 8bit label integer and the second channel is the instance ID we do not use. # We save the extracted label integer as numpy array to make things easier. original_images = path.join( sequence_basepath, 'GT/LABELS/Stereo_Right/Omni_{}'.format(direction)) for filename in listdir(original_images): # Labels are stored in the 1st channel of the png file array = one_channel_image_reader( path.join(original_images, filename), np.uint8) resized = zoom(array, 0.5, mode='nearest', order=0) # The image has to get cropped, se docstring of crop method cropped = crop_resized_image(resized) # Now we can save it as a numpy array np.save(path.join(new_images, filename.split('.')[0]), cropped) filenames = [ filename.split('.')[0] for filename in listdir( path.join(sequence_basepath, 'resized_rgb_F')) ] trainset, testset = train_test_split(filenames, test_size=0.2) with open('{}/train_test_split.json'.format(sequence_basepath), 'w') as f: json.dump({'trainset': trainset, 'testset': testset}, f) print('INFO: Preprocessing finished.')