def makeDepthText(text, depth=50,fontsize=50, font=DEFAULT_DEPTHTEXT_FONT): """ Makes a text depthmap :param text: Text :param depth: Desired depth :param fontsize: Size of text :param font: Further font specification :return: new depthmap image with text """ import PIL.ImageFont as imf if depth<0: depth=0 if depth>100: depth=100 fontroot="/usr/share/fonts/truetype" fontdir="{}/{}.ttf".format(fontroot,font) # Create image (grayscale) i=im.new('L',SIZE, "black") # Draw text with appropriate gray level fnt=imf.truetype(fontdir,fontsize) imd.Draw(i).text( ((SIZE[0]/2 - fnt.getsize(text)[0]/2, SIZE[1]/2 - fnt.getsize(text)[1]/2)), text,font=fnt, fill=((int)(255.0*depth/100))) return i
def __call__(self, img): img = img.filter(ImageFilter.GaussianBlur(random.normal(0.0, 0.5, 1))) return img
# Displays the rotated image img_flip.show() img_flip.save("bike3.jpg") # Make Black and White image img_bw = Image.open("exercise.jpg") img_bw.convert(mode='L').save('exercise2.jpg') img_bw.show() # Blur the Images from PIL import Image, ImageFilter img_blur = Image.open("exercise.jpg") # 15 is the radius, default is 2 so it doesn’t show too much img_blur.filter(ImageFilter.GaussianBlur(4)).save('exercise3.jpg') img_blur = Image.open('exercise3.jpg') img_blur.show() ################################################################ """ Playing with csv files """ """ For example, you might export the results of a data mining program to a CSV file and then import that into a spreadsheet to analyze the data, generate graphs for a presentation, or prepare a report for publication. A CSV file (Comma Separated Values file) is a type of plain text file that uses specific structuring to arrange tabular data.
def get_name_image(image_w=110, image_h=40): card_info = {} address_len = random.randint(5, 8) # 最低5个词条,最多8个 card_info['uuid'] = uuid.uuid4().hex card_info['addr'] = "".join(random.sample(address_list, address_len))[:12 * 3] # 最多3行 # ------------------------随机生成 位置信息(角度)--------------------------- # 随机透明化(0.4~0.85) card_info['front_stamp_alpha'] = random.randint(51, 216) # 旋转减低(模拟偏差) # card_info['front_angle'] = random_rotate(max_rate=1) card_info['front_angle'] = random_rotate(max_rate=2) # 填充文字 front_bg_img = Image.fromarray(pre_image['front']) target = Image.new('RGBA', (front_bg_img.size[0], front_bg_img.size[1]), (0, 0, 0, 0)) target.paste(front_bg_img, (0, 0, front_bg_img.size[0], front_bg_img.size[1])) draw = ImageDraw.Draw(target) # 随机字的颜色(24-》30) font_color = random.randint(0, 30) card_info['font_color'] = font_color # 文字填充 # 身份证字体 font = ImageFont.truetype(word_css, random.choice([16, 17, 18])) # 多行文字填充 每行12个字 max_line = 12 adddress_line = [] for col in ['addr']: multi_text = "" for i in range(math.ceil(len(card_info[col]) / max_line)): adddress_line.append( [card_info[col][i * max_line:(i + 1) * max_line]]) multi_text += card_info[col][i * max_line:(i + 1) * max_line] + '\n' draw.multiline_text(text_loc[col], multi_text, (font_color, font_color, font_color), font=font, spacing=4, align='left') # 印章图片 front_stamp_name = random.choice(stamp_list) sign_stamp(target, Image.fromarray(pre_image[front_stamp_name]).copy(), 0, (0, 0), card_info['front_stamp_alpha'], 0) target = target.rotate(card_info['front_angle'], Image.BICUBIC, expand=0) # 随机 图片亮度(80~120) image_brightness_rate = random.randint(60, 108) / 100 enh_col = ImageEnhance.Brightness(target) target = enh_col.enhance(image_brightness_rate) # # 随机 高斯模糊滤波(0.2~1.3) card_info['gaussian_blur_radius'] = 0 if random.random() < 0.9: card_info['gaussian_blur_radius'] = random.choice( [0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3, 1.4, 1.5]) target = target.filter( ImageFilter.GaussianBlur(radius=card_info['gaussian_blur_radius'])) # 随机偏移 pand_w = random.choice([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 8, -1, -2, -3, -4, -6, -8 ]) pand_h = random.choice([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, -1, -2, -3]) # # 住址 # addr_img = main_front_img[130: 130 + 105 # , 68: 68 + 250] return target.convert('L').crop( [68 + pand_w, 130 + pand_h, 68 + 250 + pand_w, 130 + 105 + pand_h]), adddress_line, card_info['uuid']
# -*- coding: utf-8 -*- from PIL import Image, ImageFilter from pytesseract import image_to_string import urllib import numpy as np import scipy.misc from string import ascii_uppercase import random import logging import cv2 import imutils VOCABULARY = filter(lambda letter: letter != "O", ascii_uppercase); UNSHARP_FILTER = ImageFilter.UnsharpMask(radius=3, threshold=1); CAPTCHA_LENGTH = 6; THRESHOLD = 255 - 30; logger = logging.getLogger(__name__) def isPossible(captchaSolution): """Amazon always uses 6 uppercase Latin letters with no accents""" if len(captchaSolution) == CAPTCHA_LENGTH: for letter in captchaSolution: if letter not in VOCABULARY: return False; return True; return False;
def test(args, generator, test_csv, stitching=False): print('loading ImageJ, please wait') ij = imagej.init('fiji/fiji/Fiji.app/') shutil.rmtree('output') os.makedirs('output', exist_ok=True) os.makedirs('output/lr', exist_ok=True) os.makedirs('output/hr', exist_ok=True) os.makedirs('output/sr', exist_ok=True) os.makedirs('output/temp_patch', exist_ok=True) os.makedirs('output/temp_patch_target', exist_ok=True) os.makedirs('output/temp_channel', exist_ok=True) step = 192 test_files = pd.read_csv(test_csv) avg_fid = 0 avg_psnr = 0 for k in range(len(test_files)): img = Image.open(test_files.iloc[k, 0]) img_hr_array = img_as_float(np.array(img)) img_lr = img.resize((int(img.size[1] / args.up_scale), int(img.size[0] / args.up_scale))) img_lr = img_lr.resize(img.size, Image.BILINEAR) img_lr = img_lr.filter( ImageFilter.GaussianBlur(radius=((args.up_scale - 1) / 2))) img_lr_array = img_as_float(np.array(img_lr)) pad_h = int((np.floor(img_lr_array.shape[0] / step) * step + args.patch_size) - img_lr_array.shape[0]) pad_w = int((np.floor(img_lr_array.shape[1] / step) * step + args.patch_size) - img_lr_array.shape[1]) img_lr_array_padded = pad(img_lr_array, ((0, pad_h), (0, pad_w), (0, 0)), mode='reflect') img_lr_wd = view_as_windows(img_lr_array_padded, (args.patch_size, args.patch_size, 3), step=step) img_lr_wd = np.squeeze(img_lr_wd) img_hr_array_padded = pad(img_hr_array, ((0, pad_h), (0, pad_w), (0, 0)), mode='reflect') img_hr_wd = view_as_windows(img_hr_array_padded, (args.patch_size, args.patch_size, 3), step=step) img_hr_wd = np.squeeze(img_hr_wd) with open('output/temp_patch/TileConfiguration.txt', 'w') as text_file: print('dim = {}'.format(2), file=text_file) with torch.no_grad(): generator.eval() for i in range(0, img_lr_wd.shape[1]): for j in range(0, img_lr_wd.shape[0]): target = img_hr_wd[j, i] patch = img_lr_wd[j, i].transpose((2, 0, 1))[None, :] patch_tensor = torch.from_numpy(patch).float().cuda() prediction = generator(patch_tensor) io.imsave( 'output/temp_patch/{}_{}.tiff'.format(j, i), img_as_ubyte( np.clip(prediction.cpu().numpy()[0], 0, 1))) io.imsave( 'output/temp_patch_target/{}_{}.tiff'.format(j, i), img_as_ubyte(target)) print('{}_{}.tiff; ; ({}, {})'.format( j, i, i * step, j * step), file=text_file) fid = fid_score.calculate_fid_given_paths( ('output/temp_patch', 'output/temp_patch_target'), 8, 'cuda:0', 2048) avg_fid = avg_fid + fid if stitching: sys.stdout.write('\r{}/{} stitching, please wait...'.format( k + 1, len(test_files))) params = { 'type': 'Positions from file', 'order': 'Defined by TileConfiguration', 'directory': 'output/temp_patch', 'ayout_file': 'TileConfiguration.txt', 'fusion_method': 'Linear Blending', 'regression_threshold': '0.30', 'max/avg_displacement_threshold': '2.50', 'absolute_displacement_threshold': '3.50', 'compute_overlap': False, 'computation_parameters': 'Save computation time (but use more RAM)', 'image_output': 'Write to disk', 'output_directory': 'output/temp_channel' } plugin = "Grid/Collection stitching" ij.py.run_plugin(plugin, params) list_channels = [f for f in os.listdir('output/temp_channel')] c1 = io.imread( os.path.join('output/temp_channel', list_channels[0])) c2 = io.imread( os.path.join('output/temp_channel', list_channels[1])) c3 = io.imread( os.path.join('output/temp_channel', list_channels[2])) c1 = c1[:img.size[1], :img.size[0]] c2 = c2[:img.size[1], :img.size[0]] c3 = c3[:img.size[1], :img.size[0]] img_to_save = np.clip( np.stack((c1, c2, c3)).transpose((1, 2, 0)), 0, 1) io.imsave( os.path.join( 'output/sr', os.path.basename(test_files.iloc[k, 0]).replace( '.jpg', '.tiff')), img_as_ubyte(img_to_save)) io.imsave( os.path.join( 'output/lr', os.path.basename(test_files.iloc[k, 0]).replace( '.jpg', '.tiff')), img_as_ubyte(img_lr_array)) io.imsave( os.path.join( 'output/hr', os.path.basename(test_files.iloc[k, 0]).replace( '.jpg', '.tiff')), img_as_ubyte(img)) else: psnr = p_snr('output/temp_patch', 'output/temp_patch_target') avg_psnr = avg_psnr + psnr if stitching: psnr = p_snr('output/sr', 'output/hr') else: psnr = avg_psnr / len(test_files) fid = avg_fid / len(test_files) return fid, psnr
def process_image(img_name): img = Image.open(img_name) img = img.filter(ImageFilter.GaussianBlur(15)) img.thumbnail(size) img.save(f'processed/{img_name}') print(f'{img_name} was processed...')
org_path, img)).convert('RGB').resize( (IMG_SIZE, IMG_SIZE)) # opening original file low_qual = os.path.join( low_qual_path, img.split('.')[0] + '.jpg') # path of corresponding low quality image original.save(low_qual, 'JPEG', quality=100) # resave the original image # Enhance the orginal image original = ImageEnhance.Color( ImageEnhance.Sharpness(Image.open(os.path.join( org_path, img))).enhance(0.1)).enhance(0.5).convert('RGB').resize( (IMG_SIZE, IMG_SIZE)) original = original.filter(ImageFilter.EDGE_ENHANCE_MORE).filter( ImageFilter.DETAIL).filter(ImageFilter.GaussianBlur(1)) # Enhance the resaved image temporary = ImageEnhance.Color( ImageEnhance.Sharpness( Image.open(low_qual)).enhance(0.1)).enhance(0.5).convert('RGB') temporary = temporary.filter(ImageFilter.EDGE_ENHANCE_MORE).filter( ImageFilter.DETAIL).filter(ImageFilter.GaussianBlur(1)) # find the difference between the 2 images ela_image = ImageChops.difference(original, temporary) extrema = ela_image.getextrema() # enhance the ela image max_difference = max([ex[1] for ex in extrema]) if max_difference == 0: max_difference = 1 ela_image = ImageEnhance.Color( ImageEnhance.Brightness(ela_image).enhance(
Read images and corresponding labels. """ import numpy as np import os import json # import skimage # from skimage import io from PIL import Image, ImageDraw, ImageFont, ImageFilter from torch.utils.data import Dataset import time filters = [ ImageFilter.SMOOTH, # 平滑,大于16可以用 ImageFilter.SMOOTH_MORE, # 平滑,大于16可以用 ImageFilter.GaussianBlur(radius=1), # 大于16可以用 ImageFilter.GaussianBlur(radius=2), # 大于32可以用 ImageFilter.BLUR, # 大于32可以用 ] def histeq(im, nbr_bins=256): # 对一副灰度图像进行直方图均衡化 #该函数有两个输入参数,一个是灰度图像,一个是直方图中使用小区间的数目 #函数返回直方图均衡化后的图像,以及用来做像素值映射的累计分布函数 # 计算图像的直方图 imhist, bins = np.histogram(im.flatten(), nbr_bins, normed=True) cdf = imhist.cumsum() #cumulative distribution function cdf = 255 * cdf / cdf[-1] #归一化,函数中使用累计分布函数的最后一个元素(下标为-1,目标是 # 将其归一化到0-1范围 ) # 使用累计分布函数的线性插值,计算新的像素值
def FrameConnection(source_path, dest_path, dest_name='num', split=100, width=5000, mode="col"): # connect the key frames to a long picture. # source_path: the directory of key frames. # dest_path: the directory of the long picture. # dest_name: the name of the long picture, an optional argument in main program. # spilt: frames per picture, an optional argument in main program. # width: the width of the long picture, an optional argument in main program. # mode: the treatment of frames. modes = { 'norm': MedianFilter, 'row': PicAvgRow, 'col': PicAvgCol, 'max': PicMaxColor, 'non': DoNothing } # the options in argument [mode]: 'norm', 'row', 'col', 'max', 'non' images = [] for root, dirs, files in os.walk(source_path): for f in sorted(files): images.append(f) pic_num = len(images) print("There are " + str(pic_num) + " images.") example = Image.open(source_path + '\\' + images[0]) # unit_width = example.size[0] unit_width = int(width / split) target_height = example.size[1] target_width = split * unit_width seg_num = int(pic_num / split) print("Frames will be connect into " + str(seg_num) + " segments.") print("Target Segment Size: " + str(target_width) + " × " + str(target_height) + '\n') quality_value = 100 for i in range(seg_num): new_left = 0 print('connecting segment ' + str(i + 1) + ' of ' + str(seg_num) + '...') target = Image.new(mode=example.mode, size=(target_width, target_height)) for j in range(0, split): print("Segment " + str(i + 1) + " of " + str(seg_num) + ", Picture " + str(j + 1) + " of " + str(split)) # print(str('%.2f' % (((i*split + j) / pic_num)*100)) + ' % ...') this_image = Image.open(source_path + '\\' + images[i * split + j]) # this_image = modes.get(mode)(this_image) re_image = this_image.resize((unit_width, target_height), Image.ANTIALIAS) re_image = modes.get(mode)(re_image) target.paste(re_image, (new_left, 0)) # paste resized-image into target new_left += unit_width del this_image del re_image gc.collect() target = target.filter(ImageFilter.GaussianBlur(1)) # target = target.filter(ImageFilter.SMOOTH_MORE) target = MedianFilter(target) if dest_name == 'num': target.save(dest_path + str('%04d' % i) + '.jpg', quality=quality_value) else: target.save(dest_path + dest_name + '.jpg', quality=quality_value) del target gc.collect() print("picture connected.") return seg_num
def MedianFilter(image): # if you choose 'norm' in the argument [mode] # use a median filter on the image. image = image.filter(ImageFilter.MedianFilter(5)) return image
netDown = 32 gen = Gen().to(device) try: if torch.cuda.is_available(): gen.load_state_dict(torch.load(saveFileGen)) else: gen.load_state_dict( torch.load(saveFileGen, map_location=torch.device('cpu'))) except: print("No weights found, generator initialized") imgs_names = os.listdir(dirin) for img in imgs_names: imginit = Image.open(os.path.join(dirin, img)).convert("RGB") size = imginit.size #Resize so it is compatible imginit = imginit.resize((size[0] + netDown - size[0] % netDown, size[1] + netDown - size[1] % netDown), resample=Image.BILINEAR) #Light blur leads to a smoother result imginit = imginit.filter(ImageFilter.GaussianBlur(radius=2)) imgin = preprocess(imginit).unsqueeze(0).to(device) gen.eval() with torch.no_grad(): imgout = gen(imgin) imgout = unNormalize(imgout[0, :, :, :].detach().cpu()) imgout = toimage(torch.flatten(imgout, start_dim=0, end_dim=1)) #Back to original size imgout = imgout.resize((size[0], 3 * size[1]), resample=Image.BILINEAR) imgout.save(os.path.join(dirout, img))
#Converting image to gray gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) #Applying Threshold thresh = cv2.threshold(gray,100,200,cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1] #Setting the kernel kernel = np.ones((5,5),np.uint8) #Dilating thresh_dilated = cv2.dilate(thresh, kernel, iterations=0) #Displaying the window for camera cv2.imshow('window',thresh_dilated) #Saving the image cv2.imwrite(r'/home/vatsalbabel/Desktop/capture.png',thresh_dilated) #Reading the image im=Image.open(r'/home/vatsalbabel/Desktop/capture.png') #Filtering the noise and enhancing the image im=im.filter(ImageFilter.MedianFilter()) enhancer=ImageEnhance.Contrast(im) im=enhancer.enhance(5) im=im.convert('1') #Saving the image im.save(r'/home/vatsalbabel/Desktop/final.jpg') #Reading the image toString_image = Image.open(r'/home/vatsalbabel/Desktop/final.jpg') #Getting the text from the image using pytesseract recogs = pytesseract.image_to_string(toString_image) sys.stdout.write(recogs) #Using TextBlob list_of_words = str(recogs).split() confidence_per = 0 count = 0 if len(str(recogs))!=0:
from PIL import Image from PIL import ImageFilter original_image = "noise.png" original_image = Image.open(original_image) blurred_image = original_image.filter(ImageFilter.GaussianBlur(radius=5)) blurred_image.show() blurImage('noise.png')
def GaussianBlurImage(input_path, output_path): im = Image.open(input_path) im = im.filter(ImageFilter.GaussianBlur(4)) im.save(output_path, 'JPEG')
def process(self, img): return img.filter(ImageFilter.GaussianBlur(radius=self.radius))
def generate_image(idx, image, no_aug, dataset): ''' args.model == 'resnet' 的时候只是用于训练分割网络,大部分augmentation都不用 这里的注释,默认参数是 image_size [512, 64] rotate_range [-5, 5] font_range [8,32] ''' word_index_dict = dataset.word_index_dict class_num = dataset.class_num args = dataset.args image_size = dataset.image_size font_range = dataset.font_range rotate_range = dataset.rotate_range epoch = args.epoch margin = dataset.margin # 选择文字背景 image = image.resize((1024, 1024)) h, w = image.size # 随机crop一个部分,resize成固定大小,会对文字有一定的水平竖直方向拉伸 h_crop = int(get_random(idx + 10) * image_size[0] * 2 / 8) + image_size[0] * 6 / 8 # 长度范围 [374, 512] w_crop = int(get_random(idx + 11) * image_size[1] * 2 / 8) + image_size[1] * 6 / 8 # 宽度范围 [48, 64] if args.model == 'resnet' or no_aug or epoch < 60: # resnet: 分割网络采用固定大小crop # epoch<60: 网络训练初期采用固定大小,加速收敛 h_crop = image_size[0] w_crop = image_size[1] # 选择文字背景,随机选择crop起始位置 x = int(get_random(idx + 12) * (h - h_crop)) y = int(get_random(idx + 13) * (w - w_crop)) image = image.crop((x, y, x + h_crop, y + w_crop)) # 字体大小是最容易引起错误的变量,字体大小不能超出图片中心区域大小 size = font_range[0] + int( get_random(idx + 20) * (font_range[1] - font_range[0])) size = min(size, h_crop - 2 * margin - 2, w_crop - 2 * margin - 2) # 字体数量,超过可容纳数量的一半以上,至少包含一个字符 large_num = max(0, (h_crop - 2 * margin) / size - 1) word_num = int( min(large_num / 2, 5) + get_random(idx + 21) * large_num / 2) + 1 # word_num = int(large_num / 2 + get_random(idx+21) * large_num / 2) + 1 word_num = max(1, word_num) # 添加字体位置,并生成label信息 place_x = int(get_random(idx + 22) * (h_crop - word_num * size - margin)) + margin if margin == 0: # 用于添加两排文字 place_y = int(get_random(idx + 23) * (w_crop / 2 - size - margin)) + margin else: place_y = int(get_random(idx + 23) * (w_crop - size - margin)) + margin place = (place_x, place_y) label = np.zeros(class_num).astype(np.float32) text = u'' words = word_index_dict.keys() if margin == 0: # 两排文字 word_num *= 2 while len(text) < word_num: np.random.shuffle(words) w = words[len(text)] if w in u'"(),': # 部分字符不建议生成 continue text = text + w index = word_index_dict[w] label[index] = 1 # 得到bbox_label if args.model == 'resnet': bbox_label, seg_label = generate_bbox_label(image, place, size, word_num, args, image_size) else: bbox_label, seg_label = 0, 0 # 字体,可以添加其他字体 fonts = ['../../files/ttf/simsun.ttf'] np.random.shuffle(fonts) font = fonts[0] # 颜色 r = get_random(idx + 24) if no_aug or r < 0.7: # 选择不同程度的黑色 if r < 0.3: c = int(get_random(idx + 25) * 64) color = (c, c, c) else: rgb = 64 r = int(get_random(idx + 27) * rgb) g = int(get_random(idx + 28) * rgb) b = int(get_random(idx + 29) * rgb) color = (r, g, b) else: # 随机颜色,但是选择较暗的颜色 rgb = 256 r = int(get_random(idx + 27) * rgb) g = int(get_random(idx + 28) * rgb) b = int(get_random(idx + 29) * rgb) ra = get_random(idx + 30) if ra < 0.5: ra = int(1000 * ra) % 3 if ra == 0: r = 0 elif ra == 1: g = 0 else: b = 0 color = (r, g, b) # 增加文字到图片 if margin == 0: image = add_text_to_img(image, text[:word_num / 2], size, font, color, place) image = add_text_to_img(image, text[word_num / 2:], size, font, color, (place[0], place[1] + image_size[1] / 2)) else: image = add_text_to_img(image, text, size, font, color, place) ''' # 随机翻转,增加泛化程度 if args.model != 'resnet': if get_random(idx+130) < 0.3: image = image.transpose(Image.FLIP_LEFT_RIGHT) if get_random(idx+131) < 0.3: image = image.transpose(Image.FLIP_TOP_BOTTOM) # 先做旋转,然后在拉伸图片 h,w = image.size max_hw, min_hw = float(max(h,w)), float(min(h,w)) if max_hw / min_hw >= 5: rotate_size = 5 elif max_hw / min_hw >= 3: rotate_size = 10 elif max_hw / min_hw >= 1.5: rotate_size = 30 else: rotate_size = 50 if args.model != 'resnet' and not no_aug and epoch>70 and get_random(idx+50) < 0.8: theta = int(rotate_size * 2 * get_random(idx+32)) - rotate_size image = image.rotate(theta) else: theta = 0 ''' # 还原成 [512, 64] 的大小 image = image.resize(image_size) # 最后生成图片后再一次旋转,图片模糊化 if args.model == 'resnet' or (get_random(idx + 50) < 0.8 and not no_aug): # 旋转 if args.model == 'resnet': rotate_size = 10 else: rotate_size = rotate_range[0] + int( get_random(idx + 32) * (rotate_range[1] - rotate_range[0])) theta = int(rotate_size * 2 * get_random(idx + 33)) - rotate_size image = image.rotate(theta) if args.model == 'resnet': # 作分割的时候,标签信息也需要一起旋转 seg_label = np.array([seg_label, seg_label, seg_label]) * 255 seg_label = np.array( Image.fromarray( seg_label.transpose([1, 2, 0]).astype(np.uint8)).rotate(theta)) seg_label = (seg_label[:, :, 0] > 128).astype(np.float32) filters = [ ImageFilter.SMOOTH, # 平滑,大于16可以用 ImageFilter.SMOOTH_MORE, # 平滑,大于16可以用 ImageFilter.GaussianBlur(radius=1), # 大于16可以用 ImageFilter.GaussianBlur(radius=2), # 大于32可以用 ImageFilter.BLUR, # 大于32可以用 ImageFilter.GaussianBlur(radius=2), # 多来两次 ImageFilter.BLUR, # 多来两次 ] # 当文字比较大的时候,增加一些模糊 if size > 16: if size < 32: filters = filters[:3] np.random.shuffle(filters) image = image.filter(filters[idx % len(filters)]) if args.model == 'resnet': # add noise noise_level = 32 image = np.array(image) noise = np.random.random(image.shape) * noise_level - noise_level / 2. image = image + noise image = image.astype(np.uint8) image = Image.fromarray(image) # 有时候需要低分辨率的图片 resize_0, resize_1 = get_resize_para(size, idx) image = image.resize([image_size[0] / resize_0, image_size[1] / resize_1]) # 还原成 [512, 64] 的大小 image = image.resize(image_size) return image, label, bbox_label, seg_label, size
'photo-1513938709626-033611b8cc03.jpg', 'photo-1507143550189-fed454f93097.jpg', 'photo-1493976040374-85c8e12f0c0e.jpg', 'photo-1504198453319-5ce911bafcde.jpg', 'photo-1530122037265-a5f1f91d3b99.jpg', 'photo-1516972810927-80185027ca84.jpg', 'photo-1550439062-609e1531270e.jpg', 'photo-1549692520-acc6669e2f0c.jpg' ] t1 = time.perf_counter() size = (1200, 1200) for img_name in img_names: img = Image.open(img_name) img = img.filter(ImageFilter.GaussianBlur(15)) img.thumbnail(size) img.save(f'processed/{img_name}') print(f'{img_name} was processed...') t2 = time.perf_counter() print(f'Finished in {t2-t1} seconds') # ============================== # With using multiprocessing import concurrent.futures import time from PIL import Image, ImageFilter img_names = [
def __call__(self, image: Image, **kwargs): radius = np.random.uniform(low=self.low, high=self.high) image = image.filter(ImageFilter.GaussianBlur(radius)) return image, kwargs
def generate_image(quote, hashtag="#justgirlthings"): images = glob.glob('modules/brittbot/pil/images/*.jpg') images += glob.glob('modules/brittbot/pil/images/*.png') # Initialize Image Object and get sizes img = random.choice(images) img = Image.open(img) imgX, imgY = img.size # Initialize Draw Object for drawing all the things draw = ImageDraw.Draw(img) # Initialize Fonts justfontthings = ImageFont.truetype( 'modules/brittbot/pil/UCU charles script.ttf', 18) quotefont = ImageFont.truetype( 'modules/brittbot/pil/LibreBaskerville-Regular.ttf', 28) # What are we going to say? justtextthings = hashtag quote = quote.strip() xpadding = 40 # Get size of font and set positions for drawing x, y = draw.textsize(justtextthings, font=justfontthings) draw.text((imgX - x - 25, imgY - y - 20), justtextthings, (255, 255, 255), font=justfontthings) x, y = draw.textsize(quote, font=quotefont) maxy = 0 lines = [] words = quote.split(' ') currentLine = [] for word in words: testLine = currentLine[:] testLine.append(word) textx, texty = draw.textsize(' '.join(testLine), font=quotefont) if texty > maxy: maxy = texty if textx + xpadding > imgX: lines.append(currentLine) currentLine = [word] else: currentLine.append(word) lines.append(currentLine) # Create filter for blurring images blur = Image.new('RGBA', img.size, (0, 0, 0, 0)) blurdraw = ImageDraw.Draw(blur) for index, line in enumerate(lines): line = ' '.join(line) lines_offset = 1 if len(lines) % 2 == 0: lines_offset = 0 ypos = (maxy * (len(lines) - lines_offset) / 2) yposmid = (len(lines) - lines_offset) * ypos / 2 ypos = (index * ypos) - yposmid # print len(lines), maxy * len(lines), ypos x, y = draw.textsize(line, font=quotefont) blurdraw.text(((imgX / 2) - (x / 2), (imgY / 2) - ((maxy * len(lines)) / 2) + ypos), line, (0, 0, 0), font=quotefont) blur = blur.filter(ImageFilter.GaussianBlur(radius=3)) crispdraw = ImageDraw.Draw(blur) for index, line in enumerate(lines): line = ' '.join(line) lines_offset = 1 if len(lines) % 2 == 0: lines_offset = 0 ypos = (maxy * (len(lines) - lines_offset) / 2) yposmid = (len(lines) - lines_offset) * ypos / 2 ypos = (index * ypos) - yposmid # print len(lines), maxy * len(lines), ypos x, y = draw.textsize(line, font=quotefont) crispdraw.text(((imgX / 2) - (x / 2), (imgY / 2) - ((maxy * len(lines)) / 2) + ypos), line, (255, 255, 255), font=quotefont) img = img.convert('RGBA') img = Image.composite(img, blur, ImageChops.invert(blur)) imagepath = '/var/www/htdocs/brittbot/' imagefile = "%s.png" % str(uuid.uuid4()).replace('-', '')[0:8] img.save(imagepath + imagefile) return imagefile
args = parser.parse_args() model = FastStyleNet() serializers.load_npz(args.model, model) if args.gpu >= 0: cuda.get_device(args.gpu).use() model.to_gpu() xp = np if args.gpu < 0 else cuda.cupy start = time.time() image = np.asarray(Image.open(args.input).convert('RGB'), dtype=np.float32).transpose(2, 0, 1) image = image.reshape((1,) + image.shape) if args.padding > 0: image = np.pad(image, [[0, 0], [0, 0], [args.padding, args.padding], [args.padding, args.padding]], 'symmetric') image = xp.asarray(image) x = Variable(image) y = model(x) result = cuda.to_cpu(y.data) if args.padding > 0: result = result[:, :, args.padding:-args.padding, args.padding:-args.padding] result = np.uint8(result[0].transpose((1, 2, 0))) med = Image.fromarray(result) if args.median_filter > 0: med = med.filter(ImageFilter.MedianFilter(args.median_filter)) print(time.time() - start, 'sec') med.save(args.out)
def gaussian_blur(img): img = img.filter(ImageFilter.GaussianBlur(radius=2.5)) return img
def gaussianBlur3(self, x): if np.random.random_sample() > 0.08: return x else: return x.filter(ImageFilter.GaussianBlur(radius=3))
# copy and resize patch from sphere and paste it into result sphereDiameter = int(random.random() * 90 + 10) # random integer value in [10, 100] sphereCenter = (random.random() * 100 + 50, random.random() * 100 + 50) sphere = sphere.resize((sphereDiameter, sphereDiameter)) pasteBox = (int(sphereCenter[0] - math.floor(sphereDiameter / 2.0)), int(sphereCenter[1] - math.floor(sphereDiameter / 2.0)), \ int(sphereCenter[0] + math.ceil(sphereDiameter / 2.0)), int(sphereCenter[1] + math.ceil(sphereDiameter / 2.0))) # random brightness sphereEnhanced = sphere.convert('RGB') enhancer = ImageEnhance.Brightness(sphereEnhanced) sphereEnhanced = enhancer.enhance(random.random() + 0.5) # random blur blur = ImageFilter.GaussianBlur( int(random.random() * sphereDiameter / 20)) sphereEnhanced = sphereEnhanced.filter(blur) blurredAlpha = sphere.split()[-1].filter(blur) sphere.putalpha(blurredAlpha) result.paste(sphereEnhanced, \ pasteBox, sphere) # the second sphere is used as alpha mask # crop to 100x100 result = result.crop((50, 50, 150, 150)) # random brightness enhancer = ImageEnhance.Brightness(result) result = enhancer.enhance(random.random() + 0.5)
def adjust_image(self, image, preview=True): """Applies all current editing opterations to an image. Arguments: image: A PIL image. preview: Generate edit image in preview mode (faster) Returns: A PIL image. """ if not preview: orientation = self.photoinfo[13] if orientation == 3 or orientation == 4: image = image.transpose(PIL.Image.ROTATE_180) elif orientation == 5 or orientation == 6: image = image.transpose(PIL.Image.ROTATE_90) elif orientation == 7 or orientation == 8: image = image.transpose(PIL.Image.ROTATE_270) if orientation in [2, 4, 5, 7]: image = image.transpose(PIL.Image.FLIP_LEFT_RIGHT) size_multiple = self.size_multiple else: size_multiple = 1 if self.sharpen != 0: enhancer = ImageEnhance.Sharpness(image) image = enhancer.enhance(self.sharpen + 1) if self.median_blur != 0 and opencv: max_median = 10 * size_multiple median = int(self.median_blur * max_median) if median % 2 == 0: median = median + 1 open_cv_image = cv2.cvtColor(numpy.array(image), cv2.COLOR_RGB2BGR) open_cv_image = cv2.medianBlur(open_cv_image, median) open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_BGR2RGB) image = Image.fromarray(open_cv_image) if self.bilateral != 0 and self.bilateral_amount != 0 and opencv: diameter = int(self.bilateral * 10 * size_multiple) if diameter < 1: diameter = 1 sigma_color = self.bilateral_amount * 100 * size_multiple if sigma_color < 1: sigma_color = 1 sigma_space = sigma_color open_cv_image = cv2.cvtColor(numpy.array(image), cv2.COLOR_RGB2BGR) open_cv_image = cv2.bilateralFilter(open_cv_image, diameter, sigma_color, sigma_space) open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_BGR2RGB) image = Image.fromarray(open_cv_image) if self.vignette_amount > 0 and self.vignette_size > 0: vignette = Image.new(mode='RGB', size=image.size, color=(0, 0, 0)) filter_color = int((1 - self.vignette_amount) * 255) vignette_mixer = Image.new(mode='L', size=image.size, color=filter_color) draw = ImageDraw.Draw(vignette_mixer) shrink_x = int((self.vignette_size * (image.size[0] / 2)) - (image.size[0] / 4)) shrink_y = int((self.vignette_size * (image.size[1] / 2)) - (image.size[1] / 4)) draw.ellipse([ 0 + shrink_x, 0 + shrink_y, image.size[0] - shrink_x, image.size[1] - shrink_y ], fill=255) vignette_mixer = vignette_mixer.filter( ImageFilter.GaussianBlur(radius=(self.vignette_amount * 60) + 60)) image = Image.composite(image, vignette, vignette_mixer) if self.edge_blur_amount > 0 and self.edge_blur_intensity > 0 and self.edge_blur_size > 0: blur_image = image.filter( ImageFilter.GaussianBlur(radius=(self.edge_blur_amount * 30))) filter_color = int((1 - self.edge_blur_intensity) * 255) blur_mixer = Image.new(mode='L', size=image.size, color=filter_color) draw = ImageDraw.Draw(blur_mixer) shrink_x = int((self.edge_blur_size * (image.size[0] / 2)) - (image.size[0] / 4)) shrink_y = int((self.edge_blur_size * (image.size[1] / 2)) - (image.size[1] / 4)) draw.ellipse([ 0 + shrink_x, 0 + shrink_y, image.size[0] - shrink_x, image.size[1] - shrink_y ], fill=255) blur_mixer = blur_mixer.filter( ImageFilter.GaussianBlur(radius=(self.edge_blur_amount * 30))) image = Image.composite(image, blur_image, blur_mixer) if self.crop_top != 0 or self.crop_bottom != 0 or self.crop_left != 0 or self.crop_right != 0: if preview: overlay = Image.new(mode='RGB', size=image.size, color=(0, 0, 0)) divisor = self.original_width / image.size[0] draw = ImageDraw.Draw(overlay) draw.rectangle( [0, 0, (self.crop_left / divisor), image.size[1]], fill=(255, 255, 255)) draw.rectangle( [0, 0, image.size[0], (self.crop_top / divisor)], fill=(255, 255, 255)) draw.rectangle([(image.size[0] - (self.crop_right / divisor)), 0, (image.size[0]), image.size[1]], fill=(255, 255, 255)) draw.rectangle([ 0, (image.size[1] - (self.crop_bottom / divisor)), image.size[0], image.size[1] ], fill=(255, 255, 255)) bright = ImageEnhance.Brightness(overlay) overlay = bright.enhance(.333) image = ImageChops.subtract(image, overlay) else: if self.crop_left >= image.size[0]: crop_left = 0 else: crop_left = int(self.crop_left) if self.crop_top >= image.size[1]: crop_top = 0 else: crop_top = int(self.crop_top) if self.crop_right >= image.size[0]: crop_right = image.size[0] else: crop_right = int(image.size[0] - self.crop_right) if self.crop_bottom >= image.size[1]: crop_bottom = image.size[1] else: crop_bottom = int(image.size[1] - self.crop_bottom) if self.video: #ensure that image size is divisible by 2 new_width = crop_right - crop_left new_height = crop_bottom - crop_top if new_width % 2 == 1: if crop_right < image.size[0]: crop_right = crop_right + 1 else: crop_right = crop_right - 1 if new_height % 2 == 1: if crop_bottom < image.size[1]: crop_bottom = crop_bottom + 1 else: crop_bottom = crop_bottom - 1 image = image.crop( (crop_left, crop_top, crop_right, crop_bottom)) if self.flip_horizontal: image = image.transpose(PIL.Image.FLIP_LEFT_RIGHT) if self.flip_vertical: image = image.transpose(PIL.Image.FLIP_TOP_BOTTOM) if self.rotate_angle != 0: if self.rotate_angle == 90: image = image.transpose(PIL.Image.ROTATE_270) if self.rotate_angle == 180: image = image.transpose(PIL.Image.ROTATE_180) if self.rotate_angle == 270: image = image.transpose(PIL.Image.ROTATE_90) if self.fine_angle != 0: total_angle = -self.fine_angle * 10 angle_radians = math.radians(abs(total_angle)) width, height = rotated_rect_with_max_area(image.size[0], image.size[1], angle_radians) x = int((image.size[0] - width) / 2) y = int((image.size[1] - height) / 2) if preview: image = image.rotate(total_angle, expand=False) else: image = image.rotate(total_angle, resample=PIL.Image.BICUBIC, expand=False) image = image.crop((x, y, image.size[0] - x, image.size[1] - y)) if self.autocontrast: image = ImageOps.autocontrast(image) if self.equalize != 0: equalize_image = ImageOps.equalize(image) image = Image.blend(image, equalize_image, self.equalize) temperature = int(round(abs(self.temperature) * 100)) if temperature != 0: temperature = temperature - 1 if self.temperature > 0: kelvin = negative_kelvin[99 - temperature] else: kelvin = positive_kelvin[temperature] matrix = ((kelvin[0] / 255.0), 0.0, 0.0, 0.0, 0.0, (kelvin[1] / 255.0), 0.0, 0.0, 0.0, 0.0, (kelvin[2] / 255.0), 0.0) image = image.convert('RGB', matrix) if self.brightness != 0: enhancer = ImageEnhance.Brightness(image) image = enhancer.enhance(1 + self.brightness) if self.shadow != 0: if self.shadow < 0: floor = int(abs(self.shadow) * 128) table = [0] * floor remaining_length = 256 - floor for index in range(0, remaining_length): value = int(round((index / remaining_length) * 256)) table.append(value) lut = table * 3 else: floor = int(abs(self.shadow) * 128) table = [] for index in range(0, 256): percent = 1 - (index / 255) value = int(round(index + (floor * percent))) table.append(value) lut = table * 3 image = image.point(lut) if self.gamma != 0: if self.gamma == -1: gamma = 99999999999999999 elif self.gamma < 0: gamma = 1 / (self.gamma + 1) elif self.gamma > 0: gamma = 1 / ((self.gamma + 1) * (self.gamma + 1)) else: gamma = 1 lut = [pow(x / 255, gamma) * 255 for x in range(256)] lut = lut * 3 image = image.point(lut) if self.contrast != 0: enhancer = ImageEnhance.Contrast(image) image = enhancer.enhance(1 + self.contrast) if self.saturation != 0: enhancer = ImageEnhance.Color(image) image = enhancer.enhance(1 + self.saturation) if self.tint != [1.0, 1.0, 1.0, 1.0]: matrix = (self.tint[0], 0.0, 0.0, 0.0, 0.0, self.tint[1], 0.0, 0.0, 0.0, 0.0, self.tint[2], 0.0) image = image.convert('RGB', matrix) if self.curve: lut = self.curve * 3 image = image.point(lut) if self.denoise and not preview and opencv: open_cv_image = cv2.cvtColor(numpy.array(image), cv2.COLOR_RGB2BGR) open_cv_image = cv2.fastNlMeansDenoisingColored( open_cv_image, None, self.luminance_denoise, self.color_denoise, self.search_window, self.block_size) open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_BGR2RGB) image = Image.fromarray(open_cv_image) if self.adaptive_clip > 0 and opencv: open_cv_image = cv2.cvtColor(numpy.array(image), cv2.COLOR_RGB2Lab) channels = cv2.split(open_cv_image) clahe = cv2.createCLAHE(clipLimit=(self.adaptive_clip * 4), tileGridSize=(8, 8)) clahe_image = clahe.apply(channels[0]) channels[0] = clahe_image open_cv_image = cv2.merge(channels) open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_Lab2RGB) image = Image.fromarray(open_cv_image) if self.border_image: image_aspect = image.size[0] / image.size[1] closest_aspect = min(self.border_image[1], key=lambda x: abs(x - image_aspect)) index = self.border_image[1].index(closest_aspect) image_file = os.path.join('borders', self.border_image[2][index]) if preview: resample = PIL.Image.NEAREST else: resample = PIL.Image.BICUBIC border_image = Image.open(image_file) border_crop_x = int(border_image.size[0] * ((self.border_x_scale + 1) / 15)) border_crop_y = int(border_image.size[1] * ((self.border_y_scale + 1) / 15)) border_image = border_image.crop( (border_crop_x, border_crop_y, border_image.size[0] - border_crop_x, border_image.size[1] - border_crop_y)) border_image = border_image.resize(image.size, resample) if os.path.splitext(image_file)[1].lower() == '.jpg': alpha_file = os.path.splitext(image_file)[0] + '-mask.jpg' if not os.path.exists(alpha_file): alpha_file = image_file alpha = Image.open(alpha_file) alpha = alpha.convert('L') alpha = alpha.crop((border_crop_x, border_crop_y, alpha.size[0] - border_crop_x, alpha.size[1] - border_crop_y)) alpha = alpha.resize(image.size, resample) else: alpha = border_image.split()[-1] border_image = border_image.convert('RGB') if self.border_tint != [1.0, 1.0, 1.0, 1.0]: matrix = (self.border_tint[0], 0.0, 0.0, 1.0, 0.0, self.border_tint[1], 0.0, 1.0, 0.0, 0.0, self.border_tint[2], 1.0) border_image = border_image.convert('RGB', matrix) enhancer = ImageEnhance.Brightness(alpha) alpha = enhancer.enhance(self.border_opacity) image = Image.composite(border_image, image, alpha) return image
def tranfun(self, image): image = getpilimage(image) image = image.filter(ImageFilter.GaussianBlur(radius=1)) return image
#%% from PIL import Image, ImageFilter im1 = Image.open(r"Lab2\rednose.jpg") im2 = im1.filter(ImageFilter.GaussianBlur(radius=5)) im2 # I made this picture blurrier than it was before with a gaussian blur filter #with a radius of 5. I went and changed the radius a few mroe times to see the outcome #below. #%% from PIL import Image, ImageFilter im1 = Image.open(r"Lab2\rednose.jpg") im2 = im1.filter(ImageFilter.GaussianBlur(radius=1)) im2 #%% from PIL import Image im = Image.open(r"Lab2\rednose.jpg") im.rotate(45).show() #I also experimented with rotating the image as well and printing it on the computer screen #not having it show in the python terminal #This was an interesting assignment in exploring ways I can manipulate images #whihc i work with most because of photography so this was interesting #I would be more excited to learn more wasy to distort and create abstract art #while still resembling the original photo and put it to use on a digital display #%% from PIL import Image import PIL.ImageOps image = Image.open(r"Lab2\rednose.jpg") if image.mode == 'RGBA': r, g, b, a = image.split()
def __call__(self, x): sigma = random.uniform(self.sigma[0], self.sigma[1]) x = x.filter(ImageFilter.GaussianBlur(radius=sigma)) return x
im.save('C:/Dhaval/Photos/rotation&grayscale.jpg') elif n==2: #Import required image library from PIL import Image, ImageFilter #Open image OriImage = Image.open('C:/Dhaval/Photos/lykan.jpg') #Display original image OriImage.show() #Applying boxblur filter boxImage = OriImage.filter(ImageFilter.BoxBlur(20)) #Save boxblur image boxImage.save('C:/Dhaval/Photos/boxblur.jpg') #left, upper, right, lower #Crop cropped = boxImage.crop((700,20,1500,900)) #Display the cropped & blurred image cropped.show() #Save the cropped image cropped.save('C:/Dhaval/Photos/cropped.jpg')
def test_usm_accuracy(self): src = self.snakes.convert("RGB") i = src.filter(ImageFilter.UnsharpMask(5, 1024, 0)) # Image should not be changed because it have only 0 and 255 levels. self.assertEqual(i.tobytes(), src.tobytes())
def medianfilter(self, image, size=3): return image.filter(ImageFilter.MedianFilter(size=size))