def proc_img(target_size, squarecrop, is_string=False, compute_global=False, imgfile=None, ): imgfile = StringIO(imgfile) if is_string else imgfile im = PILImage.open(imgfile) scale_factor = target_size / np.float32(min(im.size)) if scale_factor == 1 and im.size[0] == im.size[1] and is_string is False: return np.fromfile(imgfile, dtype=np.uint8) (wnew, hnew) = map(lambda x: int(round(scale_factor * x)), im.size) if scale_factor != 1: filt = PILImage.BICUBIC if scale_factor > 1 else PILImage.ANTIALIAS im = im.resize((wnew, hnew), filt) if squarecrop is True: (cx, cy) = map(lambda x: (x - target_size) // 2, (wnew, hnew)) im = im.crop((cx, cy, cx+target_size, cy+target_size)) buf = StringIO() im.save(buf, format='JPEG', subsampling=0, quality=95) if compute_global: nim = np.array(im).mean(axis=0).mean(axis=0) with r.get_lock(): r.value += nim[0] with g.get_lock(): g.value += nim[1] with b.get_lock(): b.value += nim[2] return buf.getvalue()
def proc_img(target_size, imgfile=None): im = Image.open(imgfile) scale_factor = target_size / np.float32(min(im.size)) filt = Image.BICUBIC if scale_factor > 1 else Image.ANTIALIAS im = im.resize((target_size, target_size), filt) buf = StringIO() im.save(buf, format='JPEG', quality=95) return buf.getvalue()
def proc_img(target_size, squarecrop, is_string=False, imgfile=None): imgfile = StringIO(imgfile) if is_string else imgfile im = PILImage.open(imgfile) # This part does the processing scale_factor = target_size / np.float32(min(im.size)) (wnew, hnew) = map(lambda x: int(round(scale_factor * x)), im.size) if scale_factor != 1: filt = PILImage.BICUBIC if scale_factor > 1 else PILImage.ANTIALIAS im = im.resize((wnew, hnew), filt) if squarecrop is True: (cx, cy) = map(lambda x: (x - target_size) // 2, (wnew, hnew)) im = im.crop((cx, cy, cx+target_size, cy+target_size)) buf = StringIO() im.save(buf, format='JPEG', subsampling=0, quality=95) return buf.getvalue()
def proc_img(imgfile, is_string=False): from PIL import Image if is_string: imgfile = StringIO(imgfile) im = Image.open(imgfile) # This part does the processing scale_factor = TARGET_SIZE / np.float32(min(im.size)) (wnew, hnew) = map(lambda x: int(round(scale_factor * x)), im.size) if scale_factor != 1: filt = Image.BICUBIC if scale_factor > 1 else Image.ANTIALIAS im = im.resize((wnew, hnew), filt) if SQUARE_CROP is True: (cx, cy) = map(lambda x: (x - TARGET_SIZE) // 2, (wnew, hnew)) im = im.crop((cx, cy, cx+TARGET_SIZE, cy+TARGET_SIZE)) buf = StringIO() im.save(buf, format='JPEG', subsampling=0, quality=95) return buf.getvalue()
def proc_img(imgfile, is_string=False): from PIL import Image if is_string: imgfile = StringIO(imgfile) im = Image.open(imgfile) # This part does the processing scale_factor = TARGET_SIZE / np.float32(min(im.size)) (wnew, hnew) = map(lambda x: int(round(scale_factor * x)), im.size) if scale_factor != 1: filt = Image.BICUBIC if scale_factor > 1 else Image.ANTIALIAS im = im.resize((wnew, hnew), filt) if SQUARE_CROP is True: (cx, cy) = map(lambda x: (x - TARGET_SIZE) / 2, (wnew, hnew)) im = im.crop((cx, cy, cx + TARGET_SIZE, cy + TARGET_SIZE)) buf = StringIO() im.save(buf, format='JPEG') return buf.getvalue()
def proc_img(target_size, squarecrop, is_string=False, imgfile=None): imgfile = StringIO(imgfile) if is_string else imgfile im = PILImage.open(imgfile) scale_factor = target_size / np.float32(min(im.size)) if scale_factor == 1 and im.size[0] == im.size[1] and is_string is False: return np.fromfile(imgfile, dtype=np.uint8) (wnew, hnew) = map(lambda x: int(round(scale_factor * x)), im.size) if scale_factor != 1: filt = PILImage.BICUBIC if scale_factor > 1 else PILImage.ANTIALIAS im = im.resize((wnew, hnew), filt) if squarecrop is True: (cx, cy) = map(lambda x: (x - target_size) // 2, (wnew, hnew)) im = im.crop((cx, cy, cx + target_size, cy + target_size)) buf = StringIO() im.save(buf, format="JPEG", subsampling=0, quality=95) return buf.getvalue()