def make_solarize(self, image): if image == 1: self.img1.image = ImageOps.solarize(self.img1.image, random.randint(50,150)) elif image == 2: self.img2.image = ImageOps.solarize(self.img2.image, random.randint(50,150)) elif image == 3: self.img3.image = ImageOps.solarize(self.img3.image, random.randint(50,150))
def test_sanity(): ImageOps.autocontrast(lena("L")) ImageOps.autocontrast(lena("RGB")) ImageOps.autocontrast(lena("L"), cutoff=10) ImageOps.autocontrast(lena("L"), ignore=[0, 255]) ImageOps.colorize(lena("L"), (0, 0, 0), (255, 255, 255)) ImageOps.colorize(lena("L"), "black", "white") ImageOps.crop(lena("L"), 1) ImageOps.crop(lena("RGB"), 1) ImageOps.deform(lena("L"), deformer) ImageOps.deform(lena("RGB"), deformer) ImageOps.equalize(lena("L")) ImageOps.equalize(lena("RGB")) ImageOps.expand(lena("L"), 1) ImageOps.expand(lena("RGB"), 1) ImageOps.expand(lena("L"), 2, "blue") ImageOps.expand(lena("RGB"), 2, "blue") ImageOps.fit(lena("L"), (128, 128)) ImageOps.fit(lena("RGB"), (128, 128)) ImageOps.fit(lena("RGB").resize((1, 1)), (35, 35)) ImageOps.flip(lena("L")) ImageOps.flip(lena("RGB")) ImageOps.grayscale(lena("L")) ImageOps.grayscale(lena("RGB")) ImageOps.invert(lena("L")) ImageOps.invert(lena("RGB")) ImageOps.mirror(lena("L")) ImageOps.mirror(lena("RGB")) ImageOps.posterize(lena("L"), 4) ImageOps.posterize(lena("RGB"), 4) ImageOps.solarize(lena("L")) ImageOps.solarize(lena("RGB")) success()
def convert(action,image_name): # # actions = Actions() # image_path = gray(image_name) # # return image_path # return (render_template('core/convert.html', path=image_path, name=image_name)) # return action if not image_name: return (redirect('/')) else: if action == "gray": img = Image.open(UPLOAD_FOLDER + '/' + image_name).convert('L') elif action == "invert": img = Image.open(UPLOAD_FOLDER + '/' + image_name) img = ImageChops.invert(img) elif action == "sharpen": img = Image.open(UPLOAD_FOLDER + '/' + image_name).filter(ImageFilter.UnsharpMask(radius=2, percent=150, threshold=3)) elif action == "contrast": img = Image.open(UPLOAD_FOLDER + '/' + image_name) img = ImageOps.autocontrast(img, cutoff=5, ignore=None) elif action == "equalize": img = Image.open(UPLOAD_FOLDER + '/' + image_name) img = ImageOps.equalize(img, mask=None) elif action == "solarize": img = Image.open(UPLOAD_FOLDER + '/' + image_name) img = ImageOps.solarize(img, threshold=128) url = "/convert/"+action+"/"+image_name filename = str(time.time()) + image_name img.save(SAVE_FOLDER + '/' + filename) image_path = 'results/' + filename return (render_template('core/index.html', path=image_path, name=image_name, url=url))
def apply_solarize(pixbuf,threshold): ''' inverts all the pixel under the threshold ''' width,height = pixbuf.get_width(),pixbuf.get_height() y = ImageOps.solarize(Image.frombytes(K.ImageConstants.RGB_SHORT_NAME,(width,height),pixbuf.get_pixels() ),threshold) return I.fromImageToPixbuf(y)
def solarize(image, treshold, amount=100): """Apply a filter - amount: 0-1""" image = imtools.convert_safe_mode(image) solarized = image.convert('RGB') solarized = ImageOps.solarize(solarized, treshold) if imtools.has_alpha(image): imtools.put_alpha(solarized, imtools.get_alpha(image)) if amount < 100: return imtools.blend(image, solarized, amount / 100.0) return solarized
def image_Solarize(self, value): if not self.pixbufs_stack: return im = pixbuftoImage(self.temp_pixbuf.copy()) im = ImageOps.solarize(im, threshold=128) pixbuf = imagetopixbuf(im) self.add_pixbuf_stack(pixbuf) self.window.invalidate_rect(self.get_allocation(), True) self.window.process_updates(True)
def image_Solarize(self, value): pixbuf = self.pixbuf self._image_changed_flag = True self._optimal_zoom_flag = True if self.window: im = self.pixbuftoImage(pixbuf) self.undo = im.copy() im = ImageOps.solarize(im, threshold=128) self.redo = im.copy() pix = self.imagetopixbuf(im) self.set_pixbuf(pix) self.window.process_updates(True)
def modImg(self, method): if method == 1: # Helps identify trees easier; changes img to grayscale, soalrizes, and smooths self.img = ImageOps.grayscale(self.img) self.img = ImageOps.solarize(self.img, 85) self.img = self.img.filter(ImageFilter.SMOOTH_MORE) elif method == 2: # Todo: using a different library such as scipy for img processing pass self.dir_plus_img = self.out_dir + self.file_name self.saveImg(self.img, self.dir_plus_img) self.openImg(self.dir_plus_name)
def _solarize_impl(pil_img, level): """Applies PIL Solarize to `pil_img`. Translate the image in the vertical direction by `level` number of pixels. Args: pil_img: Image in PIL object. level: Strength of the operation specified as an Integer from [0, `PARAMETER_MAX`]. Returns: A PIL Image that has had Solarize applied to it. """ level = int_parameter(level, 256) return ImageOps.solarize(pil_img.convert('RGB'), 256 - level).convert('RGBA')
def applyfilter(pk, preset): post = get_object_or_404(Post, pk=pk) infile = post.images.url f = infile.split('.') newfile = f[0] + '-new.' + f[1] f = newfile.split('/') newfilename = f[-1] infile.replace("/","//") im = Image.open(infile) if preset == 'gray': im = ImageOps.grayscale(im) if preset == 'edge': im = ImageOps.grayscale(im) im = im.filter(ImageFilter.FIND_EDGES) if preset == 'poster': im = ImageOps.posterize(im,3) if preset == 'solar': im = ImageOps.solarize(im, threshold=80) if preset == 'blur': im = im.filter(ImageFilter.BLUR) if preset == 'sepia': sepia=[] #sepia2=[] r,g,b=(239,224,185) for i in range(255): sepia.extend((r*i//255,g*i//255,b*i//255)) #for i in sepia: # if i>0: # n=i-i%i # else: n=0 # sepia2.extend(n) #print (sepia2) im = im.convert("L") im.putpalette(sepia) im = im.convert("RGB") im.save(newfile) return newfilename
def apply_effects(image, effects): """method to apply effects to original image from list of effects """ for effect in effects: gray = ImageOps.grayscale(image) # dictionary with all the availble effects all_effects = { 'BLUR': image.filter(ImageFilter.BLUR), 'CONTOUR': image.filter(ImageFilter.CONTOUR), 'EMBOSS': image.filter(ImageFilter.EMBOSS), 'SMOOTH': image.filter(ImageFilter.SMOOTH), 'HULK': ImageOps.colorize(gray, (0, 0, 0, 0), '#00ff00'), 'FLIP': ImageOps.flip(image), 'MIRROR': ImageOps.mirror(image), 'INVERT': ImageOps.invert(image), 'SOLARIZE': ImageOps.solarize(image), 'GREYSCALE': ImageOps.grayscale(image), } phedited = all_effects[effect] image = phedited return phedited
def gif(path): # create some more images for some super cool gif business im = Image.open(path) solarize1 = ImageOps.solarize(im, threshold=32) solarize2 = ImageOps.solarize(im, threshold=64) solarize3 = ImageOps.solarize(im, threshold=96) solarize4 = ImageOps.solarize(im, threshold=128) solarize5 = ImageOps.solarize(im, threshold=160) solarize6 = ImageOps.solarize(im, threshold=192) solarize7 = ImageOps.solarize(im, threshold=224) images = [im, solarize1, solarize2, solarize3, solarize4, solarize5, solarize6, solarize7,] filename = "cool.gif" writeGif(filename, images, duration=0.05)
def solarize(self, threshold=128): """ Invert all pixel values above a threshold """ self.convert() actual_value = float(threshold)/100 * 256 self.output = ImageOps.solarize(self.output, int(actual_value)) self.add_effect('solarize')
def result(treshold): return ImageOps.solarize(img, treshold)
def transformImage(self, img): return ImageOps.solarize(img, self.args["threshold"])
from PIL import ImageFont # The demo folder demo_dir = moon3d.getDemoDir() imageW = 0 imageH = 0 img_filename = "%s/../data/tarte_fruits.jpg" % demo_dir font_filename = "%s/timR24.pil" % demo_dir im = None ft = ImageFont.load(font_filename) im0 = Image.open(img_filename) imageW = im0.size[0] imageH = im0.size[1] im = ImageOps.solarize(im0, 128) draw = ImageDraw.Draw(im) draw.line((0, 0) + im.size, fill=(255, 255, 255)) draw.line((0, im.size[1], im.size[0], 0), fill=(255, 255, 255)) wh = ft.getsize("GLSL Hacker") draw.text((im.size[0]/2 - wh[0]/2, im.size[1]/2 + 20), "GLSL Hacker", fill=(255, 255, 0), font=ft) draw.text((im.size[0]/2 - wh[0]/2, im.size[1]/2 - 60), "GLSL Hacker", fill=(255, 255, 0), font=ft) del draw tex01 = moon3d.image.create2dRgbU8(imageW, imageH) moon3d.image.updatePixmap(tex01, im.tostring())
def __call__(self, img, ranges): if random.random() < 0.6: img = ImageOps.solarize(img, ranges["solarize"][5]) if random.random() < 0.6: img = ImageOps.autocontrast(img) return img
def __call__(self, img, ranges): if random.random() < 0.2: img = img.rotate(ranges["rotate"][3]) if random.random() < 0.6: img = ImageOps.solarize(img, ranges["solarize"][8]) return img
def pillow(self, img): return ImageOps.solarize(img)
def __init__(self, p1, operation1, magnitude_idx1, p2, operation2, magnitude_idx2, fillcolor=(128, 128, 128)): ranges = { "shearX": np.linspace(0, 0.3, 10), "shearY": np.linspace(0, 0.3, 10), "translateX": np.linspace(0, 150 / 331, 10), "translateY": np.linspace(0, 150 / 331, 10), "rotate": np.linspace(0, 30, 10), "color": np.linspace(0.0, 0.9, 10), "posterize": np.round(np.linspace(8, 4, 10), 0).astype(np.int), "solarize": np.linspace(256, 0, 10), "contrast": np.linspace(0.0, 0.9, 10), "sharpness": np.linspace(0.0, 0.9, 10), "brightness": np.linspace(0.0, 0.9, 10), "autocontrast": [0] * 10, "equalize": [0] * 10, "invert": [0] * 10 } # from https://stackoverflow.com/questions/5252170/specify-image-filling-color-when-rotating-in-python-with-pil-and-setting-expand def rotate_with_fill(img, magnitude): rot = img.convert("RGBA").rotate(magnitude) return Image.composite(rot, Image.new("RGBA", rot.size, (128, ) * 4), rot).convert(img.mode) func = { "shearX": lambda img, magnitude: img.transform(img.size, Image.AFFINE, (1, magnitude * random.choice( [-1, 1]), 0, 0, 1, 0), Image.BICUBIC, fillcolor=fillcolor), "shearY": lambda img, magnitude: img.transform(img.size, Image.AFFINE, (1, 0, 0, magnitude * random. choice([-1, 1]), 1, 0), Image.BICUBIC, fillcolor=fillcolor), "translateX": lambda img, magnitude: img.transform( img.size, Image.AFFINE, (1, 0, magnitude * img.size[0] * random.choice( [-1, 1]), 0, 1, 0), fillcolor=fillcolor), "translateY": lambda img, magnitude: img.transform( img.size, Image.AFFINE, (1, 0, 0, 0, 1, magnitude * img.size[1] * random. choice([-1, 1])), fillcolor=fillcolor), "rotate": lambda img, magnitude: rotate_with_fill(img, magnitude), "color": lambda img, magnitude: ImageEnhance.Color(img).enhance( 1 + magnitude * random.choice([-1, 1])), "posterize": lambda img, magnitude: ImageOps.posterize(img, magnitude), "solarize": lambda img, magnitude: ImageOps.solarize(img, magnitude), "contrast": lambda img, magnitude: ImageEnhance.Contrast(img).enhance( 1 + magnitude * random.choice([-1, 1])), "sharpness": lambda img, magnitude: ImageEnhance.Sharpness(img).enhance( 1 + magnitude * random.choice([-1, 1])), "brightness": lambda img, magnitude: ImageEnhance.Brightness(img).enhance( 1 + magnitude * random.choice([-1, 1])), "autocontrast": lambda img, magnitude: ImageOps.autocontrast(img), "equalize": lambda img, magnitude: ImageOps.equalize(img), "invert": lambda img, magnitude: ImageOps.invert(img) } self.p1 = p1 self.operation1 = func[operation1] self.magnitude1 = ranges[operation1][magnitude_idx1] self.p2 = p2 self.operation2 = func[operation2] self.magnitude2 = ranges[operation2][magnitude_idx2]
def solarize(img, magnitude): magnitudes = np.linspace(0, 256, 11) img = ImageOps.solarize( img, random.uniform(magnitudes[magnitude], magnitudes[magnitude + 1])) return img
def __call__(self, img: Image.Image) -> Image.Image: if random.random() < self.p: return ImageOps.solarize(img) return img
def __init__(self, Numbers=None, max_Magnitude=None): self.transforms = [ 'autocontrast', 'equalize', 'rotate', 'solarize', 'color', 'posterize', 'contrast', 'brightness', 'sharpness', 'shearX', 'shearY', 'translateX', 'translateY' ] if Numbers is None: self.Numbers = len(self.transforms) // 2 else: self.Numbers = Numbers if max_Magnitude is None: self.max_Magnitude = 10 else: self.max_Magnitude = max_Magnitude fillcolor = 128 self.ranges = { # these Magnitude range , you must test it yourself , see what will happen after these operation , # it is no need to obey the value in autoaugment.py "shearX": np.linspace(0, 0.3, 10), "shearY": np.linspace(0, 0.3, 10), "translateX": np.linspace(0, 0.2, 10), "translateY": np.linspace(0, 0.2, 10), "rotate": np.linspace(0, 360, 10), "color": np.linspace(0.0, 0.9, 10), "posterize": np.round(np.linspace(8, 4, 10), 0).astype(np.int), "solarize": np.linspace(256, 231, 10), "contrast": np.linspace(0.0, 0.5, 10), "sharpness": np.linspace(0.0, 0.9, 10), "brightness": np.linspace(0.0, 0.3, 10), "autocontrast": [0] * 10, "equalize": [0] * 10, "invert": [0] * 10 } self.func = { "shearX": lambda img, magnitude: img.transform(img.size, Image.AFFINE, (1, magnitude * random.choice( [-1, 1]), 0, 0, 1, 0), Image.BICUBIC, fill=fillcolor), "shearY": lambda img, magnitude: img.transform(img.size, Image.AFFINE, (1, 0, 0, magnitude * random. choice([-1, 1]), 1, 0), Image.BICUBIC, fill=fillcolor), "translateX": lambda img, magnitude: img.transform( img.size, Image.AFFINE, (1, 0, magnitude * img.size[0] * random.choice( [-1, 1]), 0, 1, 0), fill=fillcolor), "translateY": lambda img, magnitude: img.transform( img.size, Image.AFFINE, (1, 0, 0, 0, 1, magnitude * img.size[1] * random. choice([-1, 1])), fill=fillcolor), "rotate": lambda img, magnitude: self.rotate_with_fill(img, magnitude), # "rotate": lambda img, magnitude: img.rotate(magnitude * random.choice([-1, 1])), "color": lambda img, magnitude: ImageEnhance.Color(img).enhance( 1 + magnitude * random.choice([-1, 1])), "posterize": lambda img, magnitude: ImageOps.posterize(img, magnitude), "solarize": lambda img, magnitude: ImageOps.solarize(img, magnitude), "contrast": lambda img, magnitude: ImageEnhance.Contrast(img).enhance( 1 + magnitude * random.choice([-1, 1])), "sharpness": lambda img, magnitude: ImageEnhance.Sharpness(img).enhance( 1 + magnitude * random.choice([-1, 1])), "brightness": lambda img, magnitude: ImageEnhance.Brightness(img).enhance( 1 + magnitude * random.choice([-1, 1])), "autocontrast": lambda img, magnitude: ImageOps.autocontrast(img), "equalize": lambda img, magnitude: img, "invert": lambda img, magnitude: ImageOps.invert(img) }
def showImageOps(): im = ImageOps.invert(dino) im1 = ImageOps.grayscale(dino) im2 = ImageOps.solarize(dino,threshold = 128) pylab.imshow(im2) pylab.show()
def transform(self, pil_img, label, **kwargs): degree = categorize_value(self.level, self.value_range, "float") return ImageOps.solarize(pil_img, degree), label
def __call__(self, img): if random.random() < self.p: return ImageOps.solarize(img) else: return img
def __call__(self, img, ranges): if random.random() < 0.6: img = ImageOps.solarize(img, ranges["solarize"][3]) if random.random() < 0.6: img = ImageOps.equalize(img) return img
def glitch_from_url(url_string): """This is the thumbnail generating function. Convert to low-quality JPEG, adjust, solarize, convert to 1-bit color """ # get the image from the net headers = {'User-Agent': 'Mozilla/5.0'} response = requests.get(url_string, headers=headers) image_file = StringIO(response.content) # open and tweak the image # open, resize... tweaked_image = Image.open(image_file) tweaked_image.thumbnail([app.config['THUMB_MAX_WIDTH'], app.config['THUMB_MAX_HEIGHT']]) # add artifacts/save as low quality jpeg # save as low quality jpg tweaked_image_io = StringIO() tweaked_image.save(tweaked_image_io, format="JPEG", quality=random.randint(5, 20)) # Seek back to beginning tweaked_image_io.seek(0) tweaked_image = Image.open(tweaked_image_io) # auto-contrast tweaked_image = ImageOps.autocontrast(tweaked_image) tweaked_image = ImageOps.equalize(tweaked_image) # solarize tweaked_image = ImageOps.solarize(tweaked_image, random.randint(1, 200)) # random chance to flip if random.randint(0, 4): tweaked_image = ImageOps.mirror(tweaked_image) if random.randint(0, 4): tweaked_image = ImageOps.equalize(tweaked_image) max_colors = random.randint(app.config['MIN_COLORS'], app.config['MAX_COLORS']) tweaked_image = tweaked_image.convert(mode='P', palette=Image.ADAPTIVE, colors=max_colors) tweaked_image = atkinson_dither(tweaked_image) # we have a 1-bit image because of the atkinson dither, # now we must generate a random color and get its inverse first_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) inverse_color = (abs(first_color[0] - 255), abs(first_color[1] - 255), abs(first_color[2] - 255)) tweaked_image = ImageOps.colorize(tweaked_image, first_color, inverse_color) # save the image as base64 HTML image glitch_image = StringIO() tweaked_image.save(glitch_image, "PNG", optimize=True) glitch_string = glitch_image.getvalue() base64_string = base64.b64encode(glitch_string) return base64_string
pylab.imshow(im4) # <codecell> from PIL import ImageOps im5 = ImageOps.invert(dino) pylab.imshow(im5) # <codecell> im6 = ImageOps.grayscale(dino) pylab.imshow(im6) # <codecell> im7 = ImageOps.solarize(dino, threshold=128) pylab.imshow(im7) # <codecell> im8 = land.transpose(Image.ROTATE_270) pylab.imshow(im8) # <codecell> im8 = land.transpose(Image.FLIP_TOP_BOTTOM) pylab.imshow(im8) # <codecell> im9 = land.crop((20, 20, 200, 100))
def solarize(cls): return ImageOps.solarize(cls.image)
def solarize(img, thresh, **__): return ImageOps.solarize(img, thresh)
def process(self, command, args): # autocontrast if command == 'autocontrast': return ImageOps.autocontrast(self.im) # blur if command == 'blur': return self.im.filter(ImageFilter.BLUR) # brightness if command == 'brightness': try: return ImageEnhance.Brightness(self.im).enhance(float(args[0])) except: raise Exception('Invalid brightness argument') # color if command == 'color': try: return ImageEnhance.Color(self.im).enhance(float(args[0])) except: raise Exception('Invalid color argument') # colorize if command == 'colorize': try: return ImageOps.colorize( ImageOps.grayscale(self.im), black='%s' % args[0], white='%s' % args[1] ) except: raise Exception('Invalid colorize arguments') # contour if command == 'contour': return self.im.filter(ImageFilter.CONTOUR) # contrast if command == 'contrast': try: return ImageEnhance.Contrast(self.im).enhance(float(args[0])) except: raise Exception('Invalid contrast argument') # crop if command == 'crop': try: return self.im.crop(tuple([int(a) for a in args])) except: raise Exception('Invalid crop arguments') # cropratio if command == 'cropratio': try: # get sizes width = float(self.im.size[0]) height = float(self.im.size[1]) orig_ratio = width / height target_ratio = float(args[0]) # crop if orig_ratio > target_ratio: # same height, change width target_width = int(round(height * target_ratio)) left = int(round((width / 2) - (target_width / 2))) return self.im.crop(( left, 0, left + target_width, int(height), )) elif target_ratio > orig_ratio: # same width, change height target_height = int(round(width / target_ratio)) top = int(round((height / 2) - (target_height / 2))) return self.im.crop(( 0, top, int(width), top + target_height )) else: return self.im except: raise Exception('Invalid cropratio arguments') # emboss if command == 'emboss': return self.im.filter(ImageFilter.EMBOSS) # equalize if command == 'equalize': return ImageOps.equalize(self.im) # fliphoriz if command == 'fliphoriz': return self.im.transpose(Image.FLIP_LEFT_RIGHT) # flipvert if command == 'flipvert': return self.im.transpose(Image.FLIP_TOP_BOTTOM) # gblur if command == 'gblur': try: return self.im.filter(ImageFilter.GaussianBlur(radius=int(args[0]))) except: raise Exception('Invalid gblur argument') # grayscale if command == 'grayscale': return ImageOps.grayscale(self.im) # invert if command == 'invert': return ImageOps.invert(self.im) # posterize if command == 'posterize': try: return ImageOps.posterize(self.im, int(args[0])) except: raise Exception('Invalid posterize argument') # resize if command == 'resize': try: return self.im.resize( tuple([int(a) for a in args]), resample=Image.ANTIALIAS ) except: raise Exception('Invalid resize arguments') # resizepc if command == 'resizepc': try: x, y = self.im.size return self.im.resize( (int(x * float(args[0])), int(y * float(args[1]))), resample=Image.ANTIALIAS ) except: raise Exception('Invalid resize arguments') # rotate if command == 'rotate': try: return self.im.rotate(int(args[0])) except: raise Exception('Invalid rotate argument') # sharpness if command == 'sharpness': try: return ImageEnhance.Sharpness(self.im).enhance(float(args[0])) except: raise Exception('Invalid sharpness argument') # solarize if command == 'solarize': try: return ImageOps.solarize(self.im, int(args[0])) except: raise Exception('Invalid solarize argument') # fallback raise Exception('Invalid function name "%s"' % command)
def __call__(self, img, ranges): # print("Called Policy 1: (Solarize,0.6,5) (AutoContrast,0.6,5)") if random.random() < 0.6: img = ImageOps.solarize(img, ranges["solarize"][5]) if random.random() < 0.6: img = ImageOps.autocontrast(img) return img
def process(self, img): return ImageOps.solarize(img, threshold=self.threshold)
def gen_bands(path, filename, images=[]): im = Image.open(path+filename) im = im.convert('RGBA') #red data = np.array(im) red, green, blue, alpha = data.T green.fill(0) blue.fill(0) im2 = Image.fromarray(data) fname = 'red_' + filename im2.save(path + fname) images.append(fname) #green data = np.array(im) red, green, blue, alpha = data.T red.fill(0) blue.fill(0) im2 = Image.fromarray(data) fname = 'green_' + filename im2.save(path + fname) images.append(fname) #blue data = np.array(im) red, green, blue, alpha = data.T red.fill(0) green.fill(0) im2 = Image.fromarray(data) fname = 'blue_' + filename im2.save(path + fname) images.append(fname) #alpha data = np.array(im) red, green, blue, alpha = data.T for y in range(len(alpha)): for x in range(len(alpha[0])): red[y][x] = alpha[y][x] green[y][x] = alpha[y][x] blue[y][x] = alpha[y][x] im2 = Image.fromarray(data) fname = 'alpha_' + filename im2.save(path + fname) images.append(fname) #inverted im2 = im.convert('RGB') im2 = ImageOps.invert(im2) fname = 'inverted_' + filename im2.save(path + fname) images.append(fname) #posterized im2 = im.convert('RGB') im2 = ImageOps.posterize(im2, 1) fname = 'posterized_' + filename im2.save(path + fname) images.append(fname) #solarized im2 = im.convert('RGB') im2 = ImageOps.solarize(im2, threshold=128) fname = 'solarized_' + filename im2.save(path + fname) images.append(fname) #randomized for n in range(3): im2 = Image.new(im.mode, im.size) pix = im.load() pix2 = im2.load() lista = [i for i in range(256)] listb = [i for i in lista] shuffle(listb) pixdict = dict(zip(lista, listb)) for y in range(im.size[1]): for x in range(im.size[0]): rgb = pix[x,y] tmp = [] for i in rgb: tmp.append(pixdict[i]) pix2[x,y] = tuple(tmp) fname = 'random' + str(n + 1) + '_' + filename im2.save(path + fname) images.append(fname) return images
from PIL import Image, ImageOps mode=int(raw_input('Input:')) size = 100,100 img = Image.open('1.jpg') img_with_border = ImageOps.expand(img,border=mode,fill='black') img_with_border = ImageOps.solarize(img_with_border, threshold=128) if(mode > 0): img_with_border.thumbnail(size, Image.ANTIALIAS) else: img_with_border = img_with_border.resize(size, Image.ANTIALIAS) print("Image 1:",img.size) print("Image 2:",img_with_border.size) img_with_border.save('imaged-with-border.png')
def solarize(pil_img, level): level = int_parameter(sample_level(level), 256) return ImageOps.solarize(pil_img, 256 - level)
def glitch_from_url(url_string): """This is the thumbnail generating function. """ # get the image from the net headers = {'User-Agent': 'Mozilla/5.0'} req = urllib2.Request(url_string, None, headers) urlopen_result = urllib2.urlopen(req) urlopen_result_io = io.BytesIO(urlopen_result.read()) # open and tweak the image # open, resize... tweaked_image = Image.open(urlopen_result_io) tweaked_image.thumbnail( [app.config['THUMB_MAX_WIDTH'], app.config['THUMB_MAX_HEIGHT']]) # add artifacts/save as low quality jpeg # save as low quality jpg tweaked_image_io = StringIO() tweaked_image.save(tweaked_image_io, format="JPEG", quality=random.randint(5, 20)) tweaked_image = Image.open(tweaked_image_io) # autocontrast tweaked_image = ImageOps.autocontrast(tweaked_image) tweaked_image = ImageOps.equalize(tweaked_image) # solarize tweaked_image = ImageOps.solarize(tweaked_image, random.randint(1, 200)) # random chance to flip if random.randint(0, 4): tweaked_image = ImageOps.mirror(tweaked_image) if random.randint(0, 4): tweaked_image = ImageOps.equalize(tweaked_image) max_colors = random.randint(app.config['MIN_COLORS'], app.config['MAX_COLORS']) tweaked_image = tweaked_image.convert(mode='P', palette=Image.ADAPTIVE, colors=max_colors) tweaked_image = atkinson_dither(tweaked_image) # we have a 1-bit image because of the atkinson dither, # now we must generate a random color and get its inverse first_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) inverse_color = (abs(first_color[0] - 255), abs(first_color[1] - 255), abs(first_color[2] - 255)) tweaked_image = ImageOps.colorize(tweaked_image, first_color, inverse_color) # save the image as base64 HTML image glitch_image = StringIO() tweaked_image.save(glitch_image, "PNG", optimize=True) glitch_string = glitch_image.getvalue() base64_string = base64.b64encode(glitch_string) return base64_string
def solarize(self, threshold=128): return Imager(image=ImageOps.solarize(self.image, threshold))
def __init__(self, num_layers=2, magnitude=5, fillcolor=(128, 128, 128)): self.num_layers = num_layers self.magnitude = magnitude self.max_level = 10 abso_level = self.magnitude / self.max_level self.level_map = { "shearX": 0.3 * abso_level, "shearY": 0.3 * abso_level, "translateX": 150.0 / 331 * abso_level, "translateY": 150.0 / 331 * abso_level, "rotate": 30 * abso_level, "color": 0.9 * abso_level, "posterize": int(4.0 * abso_level), "solarize": 256.0 * abso_level, "contrast": 0.9 * abso_level, "sharpness": 0.9 * abso_level, "brightness": 0.9 * abso_level, "autocontrast": 0, "equalize": 0, "invert": 0 } # from https://stackoverflow.com/questions/5252170/ # specify-image-filling-color-when-rotating-in-python-with-pil-and-setting-expand def rotate_with_fill(img, magnitude): rot = img.convert("RGBA").rotate(magnitude) return Image.composite(rot, Image.new("RGBA", rot.size, (128, ) * 4), rot).convert(img.mode) rnd_ch_op = random.choice self.func = { "shearX": lambda img, magnitude: img.transform( img.size, Image.AFFINE, (1, magnitude * rnd_ch_op([-1, 1]), 0, 0, 1, 0), Image.BICUBIC, fillcolor=fillcolor), "shearY": lambda img, magnitude: img.transform( img.size, Image.AFFINE, (1, 0, 0, magnitude * rnd_ch_op([-1, 1]), 1, 0), Image.BICUBIC, fillcolor=fillcolor), "translateX": lambda img, magnitude: img.transform( img.size, Image.AFFINE, (1, 0, magnitude * img.size[0] * rnd_ch_op([-1, 1]), 0, 1, 0), fillcolor=fillcolor), "translateY": lambda img, magnitude: img.transform( img.size, Image.AFFINE, (1, 0, 0, 0, 1, magnitude * img.size[1] * rnd_ch_op([-1, 1])), fillcolor=fillcolor), "rotate": lambda img, magnitude: rotate_with_fill(img, magnitude), "color": lambda img, magnitude: ImageEnhance.Color(img).enhance( 1 + magnitude * rnd_ch_op([-1, 1])), "posterize": lambda img, magnitude: ImageOps.posterize(img, magnitude), "solarize": lambda img, magnitude: ImageOps.solarize(img, magnitude), "contrast": lambda img, magnitude: ImageEnhance.Contrast(img).enhance( 1 + magnitude * rnd_ch_op([-1, 1])), "sharpness": lambda img, magnitude: ImageEnhance.Sharpness(img).enhance( 1 + magnitude * rnd_ch_op([-1, 1])), "brightness": lambda img, magnitude: ImageEnhance.Brightness(img).enhance( 1 + magnitude * rnd_ch_op([-1, 1])), "autocontrast": lambda img, magnitude: ImageOps.autocontrast(img), "equalize": lambda img, magnitude: ImageOps.equalize(img), "invert": lambda img, magnitude: ImageOps.invert(img) }
def func_solarize(img, magnitude): return ImageOps.solarize(img, magnitude)
def solarize(img: Image.Image, threshold: int) -> Image.Image: if not _is_pil_image(img): raise TypeError(f"img should be PIL Image. Got {type(img)}") return ImageOps.solarize(img, threshold)
def invertViaSolarize(path): im = Image.open(path) invertSolarize = ImageOps.solarize(im, threshold=0) invertSolarize.save('invertSolarize.jpg')
def test_sanity(self): ImageOps.autocontrast(hopper("L")) ImageOps.autocontrast(hopper("RGB")) ImageOps.autocontrast(hopper("L"), cutoff=10) ImageOps.autocontrast(hopper("L"), ignore=[0, 255]) ImageOps.autocontrast_preserve(hopper("L")) ImageOps.autocontrast_preserve(hopper("RGB")) ImageOps.autocontrast_preserve(hopper("L"), cutoff=10) ImageOps.autocontrast_preserve(hopper("L"), ignore=[0, 255]) ImageOps.colorize(hopper("L"), (0, 0, 0), (255, 255, 255)) ImageOps.colorize(hopper("L"), "black", "white") ImageOps.crop(hopper("L"), 1) ImageOps.crop(hopper("RGB"), 1) ImageOps.deform(hopper("L"), self.deformer) ImageOps.deform(hopper("RGB"), self.deformer) ImageOps.equalize(hopper("L")) ImageOps.equalize(hopper("RGB")) ImageOps.expand(hopper("L"), 1) ImageOps.expand(hopper("RGB"), 1) ImageOps.expand(hopper("L"), 2, "blue") ImageOps.expand(hopper("RGB"), 2, "blue") ImageOps.fit(hopper("L"), (128, 128)) ImageOps.fit(hopper("RGB"), (128, 128)) ImageOps.flip(hopper("L")) ImageOps.flip(hopper("RGB")) ImageOps.grayscale(hopper("L")) ImageOps.grayscale(hopper("RGB")) ImageOps.invert(hopper("L")) ImageOps.invert(hopper("RGB")) ImageOps.mirror(hopper("L")) ImageOps.mirror(hopper("RGB")) ImageOps.posterize(hopper("L"), 4) ImageOps.posterize(hopper("RGB"), 4) ImageOps.solarize(hopper("L")) ImageOps.solarize(hopper("RGB"))