def augment_image(self, image): ''' Turns one image into a list of 160, 32x32 greyscale images. Inputs: image: The image to be augmented as a PIL Image object. Outputs: A list of 160 PIL Image objects, each 32x32 and greyscale. ''' # downsample the image img_resized = image.resize((96, 96), Image.BILINEAR) # reverse the image imgs_reversed = [img_resized.copy(), mirror(img_resized.copy())] # brighten and darken the image imgs_enhanced = [] enhance_amounts = [0.5, 0.75, 1.0, 1.25, 1.5] for img_reversed in imgs_reversed: img_enhancer = Brightness(img_reversed) for enhance_amount in enhance_amounts: imgs_enhanced.append(img_enhancer.enhance(enhance_amount)) # scan along the image to create several sub-images at 32x32 imgs_scanned = [] for img_enhanced in imgs_enhanced: for i in range(4): for j in range(4): imgs_scanned.append( img_enhanced.crop( (16 * i, 16 * j, 32 + 16 * i, 32 + 16 * j))) return imgs_scanned
def _onBrightnessChange(value): img = editWindow.controlImg(False) try: val = float(value) bright = Brightness(img) img = bright.enhance(val) editWindow.updateImage(img) except: pass
def write_brightness_normalized(self, file: SkyPiFile, stream): if self.target_brightness is None: stream.write(file.path.read_bytes()) return with Image.open(file.path) as im: crop_area = [ self.BRIGHTNESS_BORDER_SIZE[0] * im.width, self.BRIGHTNESS_BORDER_SIZE[1] * im.height, (1 - self.BRIGHTNESS_BORDER_SIZE[0]) * im.width, (1 - self.BRIGHTNESS_BORDER_SIZE[1]) * im.height, ] stat = ImageStat.Stat(im.crop(crop_area)) r, g, b = stat.mean brightness = math.sqrt(0.299 * (r**2) + 0.587 * (g**2) + 0.114 * (b**2)) factor = self.target_brightness / brightness enhancer = Brightness(im) enhancer.enhance(factor).save(stream, format='JPEG', quality=90)
def _do_set_brightness(vals): #b, c): img = editWindow.controlImg() if not vals: # rollback editWindow.updateImage(editWindow.savedImg) if img and vals and vals != (1,1): if vals[0] != 1: bright = Brightness(img) img = bright.enhance(vals[0]) if vals[1] != 1: contr = Contrast(img) img = contr.enhance(vals[0]) editWindow.updateImage(img) #win.update_img(img, True, label='set_brightness %f, %f'%(b, c), operation=do_set_brightness, params=(b,c)) editWindow.unsetCtrl()
def _adjust(img, alpha, etype): if alpha == 0.0: return img pil_img = toimage(img) enhancer = None if etype == "brightness": enhancer = Brightness(pil_img) elif etype == "color": enhancer = Color(pil_img) elif etype == "contrast": enhancer = Contrast(pil_img) return fromimage(enhancer.enhance(alpha))
total = 60 * 20 #for i in range(60*3-30, 60*30+30) : for i in range(total): #f =Image.new(mode="RGBA", size=[720,480], color=(0xff, 0xff, 0xff, 1)) #f =Image.new(mode="RGBA", size=[1280,720], color=(0xff, 0xff, 0xff, 1)) f = mai.crop(box=(int(x), int(y), int(x + 1280), int(y + 720))) x += deplacement[0] y += deplacement[1] sz = mai.size[0] * 0.999, mai.size[1] * 0.999 if i % 5 == 0: mai = mai.resize((int(sz[0]), int(sz[1]))) a = i % (60 * 3) if a < 20: a = abs(a - 10) g = Brightness(f) g = g.enhance(a / 10) #g.show() #g =Image.new(mode="RGBA", size=[1280,720], color=(0x02, 0x02, 0x02, int(0xff-0xff*a/30))) #f.paste(g,box=(0,0,1280,720)) g.save(open("cache/myabout/myabout%04d.png" % i, "bw")) continue #dr =ImageDraw(f) #s,ms =divmod(i,60) #dr.text(xy=[10,10], text="%02d:%02d.%04d"%(0,s,ms%60), fill=0) #dr.text(xy=[10,30], text="(%02d/%02d)"%(i,total), fill=0) f.save(open("cache/myabout/myabout%04d.png" % i, "bw")) print("%d/%d" % (i, total)) exit()