''' Created on Jul 17, 2016 @author: Brett Paufler (c) Copyright Brett Paufler Makes Ropey and Cloud-like color images Using a Perlin Static Technique (more or less) color_shebang is the main output of this module the rest are helper functions use tester() (run as __main__) to determine settings 7-19-16 Tried to use the shebang filter on an image as apposed to random input, but got nowhere aborting for the day at least On other hand, color_shebang works wonderfully 2018-07-25 - renamed duplicate normalize, appears to work ''' from skimage.io import imsave import numpy as np from scipy.ndimage.morphology import grey_closing from scipy.ndimage.filters import gaussian_filter from skimage.feature import canny from scipy.signal import resample #works as of 2018-07-24 from Img import Img #in img_work directory folder #import scipy.signal as signal #from scipy import signal def seed(width=25, height=25): '''Random seed img.''' img = np.random.randint( low=0, high=256, size=(height, width, 3), dtype=np.uint8) return img def gauss(img): '''Pre-treatment, blur effect.''' img = gaussian_filter(img, sigma=(1, 1, 0), mode='wrap') return img def close(img): '''Pre-treatment, blur effect.''' img = grey_closing(img, SIZE=(2,2,0), mode='wrap') return img def stretch_complex(img, hor=6, vert=6): '''Does multiple stretches as one. hor: how many horizontal stretches vert: how many vertical if miss-matched, tends to be 'ropey'.''' verticals = [0 for _ in range(vert)] horizontals = [1 for _ in range(hor)] axis = verticals + horizontals for a in axis: img = normalize(img, a) return img def normalize(img, axis, factor=2): img = resample(img, num=factor*img.shape[axis], axis=axis) return img def stretch_vertical(img, factor): '''Neither of these are actually used (vertical, horizontal).''' img = normalize(img, axis=0, factor=factor) return img def stretch_horizontal(img, factor): img = normalize(img, axis=1, factor=factor) return img def report(img): text = 'REPORT IMG: Shape%s, Min:%s, Max: %s\N' % ( str(img.shape), np.min(img), np.max(img)) text += str(img[10:15, 10:20, 0]) return text def normalize_img(img): '''Forces img values back into 0-255 range.''' img = (img - np.min(img)) img = img * 255.0 / np.max(img) img = np.array(img, dtype=np.uint8) return img def color_shebang( width=1000, height=1000, horizontal_stretch=6, vertical_stretch=6, blur_gauss=True, blur_close=True, normal=True): '''The main output, creates colorized images of a pleasing nature. width & height: an approximate (do not exceed) SIZE of the final image out blur_gauss: softens the contrast blur_close: provides more of a pastel effect horizontal_stretch & vertical_stretch: determine degree of 'ropey' effect normal: forces values to 0-255 range without normal, splotches occur, which can be pleasing but the images are more 'soothing' without the splotches. ''' w = width / pow(2, horizontal_stretch) h = height / pow(2, vertical_stretch) assert w >= 0 and h >= 0 #if not, array is zero in one or more dimensions img = seed(width=w, height=h) print report(img) if blur_gauss: img = gauss(img) if blur_close: img = close(img) print report(img) img = stretch_complex(img, horizontal_stretch, vertical_stretch) print report(img) if normal: img = normalize_img(img) else: img = np.array(img, dtype=np.uint8) print report(img) save_name = './output/shebang_%dx%d_hor%d_ver%d_gau%d_clo%d_nor%d.png' % ( width, height, horizontal_stretch, vertical_stretch, blur_gauss, blur_close, normal) print save_name imsave(save_name, img) def tester(): '''Runs a bunch of color_shebangs, but not all possibilities.''' for horizontal_stretch in [4, 8]: for vertical_stretch in range(0, horizontal_stretch + 1, horizontal_stretch / 2): for blur_gauss in [True, False]: for blur_close in [True, False]: for normal in [True, False]: color_shebang( width=2000, height=2000, horizontal_stretch=horizontal_stretch, vertical_stretch=vertical_stretch, blur_gauss=blur_gauss, blur_close=blur_close, normal=normal) def curated(): '''Select outcomes from the full tester.''' nice_combos = [ (3,3,0,0,1), (3,3,0,1,1), (3,3,1,1,1), (4,2,0,1,1), (4,2,1,0,1), (4,4,0,0,1), (4,4,0,1,1), (4,4,1,1,1), (6,3,0,0,1), (6,3,1,1,1), (6,6,0,0,0), (6,6,0,0,1), (6,6,0,1,1), (6,6,1,0,1), (6,6,1,1,1), (8,4,0,0,1), (8,4,1,0,1), (8,4,1,1,1), (8,8,0,0,1), (8,8,0,1,1)] for h, v, g, c, N in nice_combos: color_shebang( width=2000, height=1000, horizontal_stretch=h, vertical_stretch=v, blur_gauss=g, blur_close=c, normal=N) #This doesn't work, results unpromising class Shebang(Img): '''This was an attempt to port the wallpaper into an image, but as of this writing, the results are not promising.''' def __init__(self, file_in): Img.__init__(self, file_in=file_in, as_grey=False) self.file_out = self.file_out[:-3] + 'png' self.shebang() def shebang(self): self.report() self.img = np.dstack([canny(self.img[:, :, N]) for N in [0, 1, 2]]) * 255 self.tag_name('canny') self.report() self.save() self.img = gauss(self.img) self.ing = close(self.img) self.report() self.img = np.array(self.img[5::5, 5::5, :], dtype=np.int64) self.report() #rand = np.random.randint(low=0, high=256, SIZE=self.img.shape) #self.img += rand self.report() self.img = stretch_horizontal(self.img, factor=5) self.img = stretch_vertical(self.img, factor=5) self.report() self.img = normalize_img(self.img) self.img = np.array(self.img, dtype=np.uint8) self.report() self.tag_name('shebang') self.save() if __name__ == '__main__': '''Either tester or curated outputs a nice selection. color_shebang is the output of this module. Nice, pleasuing, color abstractions.''' #sampling of wallpappers #tester() #a curated list of wallpapers #curated() #Single Wallpaper color_shebang( width=2000, height=1000, horizontal_stretch=6, vertical_stretch=6, blur_gauss=0, blur_close=0, normal=0) #Having Done Wallpapers, probably don't want that anymore '''Experiments to use images as inspiration, uninspired so far.''' #for test_image in IMAGE_LIST: # img = Shebang(file_in=test_image) # img.save()