def extract_label(labelfile): accession = image_to_string(iopen(labelfile)) accession = " ".join(accession.split()) # normalize spaces accession = "".join(x for x in accession if x in string.printable) if not accession: accession = "none" return accession
def convert_background(pngfile, new_background=(37, 90, 223)): '''Replace the background color with the specified background color, default is blue ''' _name, _ext = op.splitext(op.basename(pngfile)) _name += '_bgxform' newfile = op.join(op.dirname(pngfile), _name + _ext) if not new_background: new_background = (37, 90, 223) meanr = 115 meang = 150 meanb = 135 img = iopen(pngfile) pixels = list(img.getdata()) h, w = img.size # Get Standard Deviation of RGB rgbArray = [] for x in range(255): rgbArray.append(x) stdRGB = np.std(rgbArray) # Change Background Color for i in range(len(pixels)): r, g, b = pixels[i] if (r >= (meanr - stdRGB) and r <= (meanr + stdRGB)): if (g >= (meang - stdRGB) and g <= (meang + stdRGB)): if (b >= (meanb - stdRGB) and b <= (meanb + stdRGB)): pixels[i] = new_background img.putdata(pixels) img.save(newfile) return newfile
def load_texture(self, filename): """ Loads the texture from a file """ texture_file = iopen(filename) # Opens the image file texture_size = texture_file.size texture_data = texture_file.tostring() return self.create_texture(texture_data, texture_size)
def getImageSize(img): """ determine the dimensions for the given image file """ if hasPIL: try: return iopen(img).size except IOError: return 0, 0 else: data = img.read(32) return getImageInfo(data)[1:]
def load_to_mem(self, data_path): print('Loading testing data into memory') data = {} idx = 0 for alpha_path in listdir(data_path): for char_path in listdir(join(data_path, alpha_path)): data[idx] = [] for sample_path in listdir( join(data_path, alpha_path, char_path)): filepath = join(data_path, alpha_path, char_path, sample_path) data[idx].append(iopen(filepath).convert('L')) idx += 1 print('Test data in memory') return data, idx
def load_to_mem(self, data_path): print('Loading training data into memory') data = {} degrees = [0, 90, 180, 270] idx = 0 for degree in degrees: for alpha_path in listdir(data_path): for char_path in listdir(join(data_path, alpha_path)): data[idx] = [] for sample_path in listdir( join(data_path, alpha_path, char_path)): filepath = join(data_path, alpha_path, char_path, sample_path) data[idx].append( iopen(filepath).rotate(degree).convert('L')) idx += 1 print('Dataset in memory') return data, idx
def convert_background(pngfile, new_background): """Replace the background color with the specified background color, default is blue""" if new_background: _name, _ext = op.splitext(op.basename(pngfile)) _name += "_bgxform" newfile = op.join(op.dirname(pngfile), _name + _ext) img = iopen(pngfile) pixels = list(img.getdata()) h, w = img.size # Get Standard Deviation of RGB rgbArray = [] for x in range(255): rgbArray.append(x) stdRGB = np.std(rgbArray) * 0.8 # Get average color obcolor = [None, None, None] pixel_values = [] for t in range(3): pixel_color = img.getdata(band=t) for pixel in pixel_color: if pixel > (stdRGB): pixel_values.append(pixel) obcolor[t] = sum(pixel_values) / len(pixel_values) # Get background color using average color and standard deviation for t in range(3): pixel_color = img.getdata(band=t) seed_pixel_values = [] for i in pixel_color: if (i > (obcolor[t] - stdRGB)) and (i < (obcolor[t] + stdRGB)): seed_pixel_values.append(i) obcolor[t] = sum(seed_pixel_values) / len(seed_pixel_values) # Selection of colors based on option parser nbcolor = [None, None, None] if new_background == "INVERSE": nbcolor = [None, None, None] for t in range(3): nbcolor[t] = 255 - obcolor[t] elif new_background == "red": nbcolor = [255, 0, 0] elif new_background == "green": nbcolor = [0, 255, 0] elif new_background == "blue": nbcolor = [0, 0, 255] elif new_background == "yellow": nbcolor = [255, 255, 0] elif new_background == "purple": nbcolor = [255, 0, 255] elif new_background == "orange": nbcolor = [255, 165, 0] # Change Background Color obcolor = tuple(obcolor) nbcolor = tuple(nbcolor) for i in range(len(pixels)): r, g, b = pixels[i] if r >= (obcolor[0] - stdRGB) and r <= (obcolor[0] + stdRGB): if g >= (obcolor[1] - stdRGB) and g <= (obcolor[1] + stdRGB): if b >= (obcolor[2] - stdRGB) and b <= (obcolor[2] + stdRGB): pixels[i] = nbcolor img.putdata(pixels) img.save(newfile, "PNG") return newfile return pngfile
def test_paletted_image_transparent(self): img = new("P", (256, 256), 0) img.save("test1.png", "PNG", transparency="\x00") img = iopen("test1.png", "r") assert img_has_transparency(img)
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)