def bpg_measure(input_image_p, bpp, precise=False, save_output_as_png=None, tmp_dir=None): """ :return (PSNR, SSIM, MS-SSIM, actual_bpp) """ input_image_root_p, ext = os.path.splitext(input_image_p) assert ext == '.png', 'Expected PNG to convert to BMP, got {}'.format( input_image_p) output_image_bpg_p, actual_bpp = _bpg_compress_to_bpp(input_image_p, target_bpp=bpp, precise=precise) output_image_bpg_png_p = decode_bpg_to_png(output_image_bpg_p) os.remove(output_image_bpg_p) # don't need that anymore _, msssim, _ = compare_imgs.compare(input_image_p, output_image_bpg_png_p, calc_ssim=False, calc_msssim=True, calc_psnr=False) if save_output_as_png: os.rename(output_image_bpg_png_p, save_output_as_png) else: os.remove(output_image_bpg_png_p) return msssim, actual_bpp
def jp_compress_accurate(input_image_p, target_bpp, verbose=False): global row global accumlate out_path = os.path.splitext(input_image_p)[0] + '_out_jp.jpg' img = Image.open(input_image_p) dim = float(img.size[0] * img.size[1]) for q in range(1, 99): img.save(out_path, quality=q) bpp = (8 * _jpeg_content_length(out_path)) / dim if bpp > target_bpp: if verbose: print('q={} -> {}bpp'.format(q, bpp)) actual_bpp, output_image_jp_p = jp_compress(input_image_p, q) im_in = scipy.ndimage.imread(input_image_p) im_out = scipy.ndimage.imread(output_image_jp_p) ssim, msssim, psnr = compare_imgs.compare(im_in, im_out, calc_ssim=True, calc_msssim=True, calc_psnr=True) print("MS-SSIM=", msssim) myListms[row][accumlate] = msssim myListbpp[row][accumlate] = bpp print(myListms) print(myListbpp) accumlate = accumlate + 1 #if accumlate== #os.remove(output_image_jp_p) return out_path, bpp raise ValueError( 'Cannot achieve target bpp {} with JPEG for image {} (max {})'.format( target_bpp, input_image_p, bpp))
def jp_measure_over_interval(input_image_p, fout, q_grid): input_image_root_p, ext = os.path.splitext(input_image_p) assert ext == '.png' for q in q_grid: actual_bpp, output_image_jp_p = jp_compress(input_image_p, q) im_in = scipy.ndimage.imread(input_image_p) im_out = scipy.ndimage.imread(output_image_jp_p) ssim, msssim, psnr = compare_imgs.compare(im_in, im_out, calc_ssim=True, calc_msssim=True, calc_psnr=True) fout_str = _append_to_measures_f(fout, q, actual_bpp, ssim, msssim, psnr) print(fout_str, end='\r') os.remove(output_image_jp_p) print()
def webp_measure_over_interval(input_image_p, fout, q_grid): input_image_root_p, ext = os.path.splitext(input_image_p) assert ext == '.png' for q in map(int, q_grid): out_p, bpp = _webp_compress(input_image_p, q) with remove_file_after(out_p) as out_p: with remove_file_after(_decode_webp_to_png(out_p)) as out_png_p: im_in = scipy.ndimage.imread(input_image_p) im_out = scipy.ndimage.imread(out_png_p) ssim, msssim, psnr = compare_imgs.compare(im_in, im_out, calc_ssim=True, calc_msssim=True, calc_psnr=True) fout_str = _append_to_measures_f(fout, q, bpp, ssim, msssim, psnr) print(fout_str, end='\r') print()
def jp2k_measure_over_interval(input_image_p, fout, bpp_grid): input_image_root_p, ext = os.path.splitext(input_image_p) assert ext == '.png', 'Expected PNG to convert to BMP, got {}'.format( input_image_p) for bpp in bpp_grid: output_image_j2_p, actual_bpp = jp2k_compress(input_image_p, bpp) im_in = scipy.ndimage.imread(input_image_p) im_out = scipy.ndimage.imread(output_image_j2_p) ssim, msssim, psnr = compare_imgs.compare(im_in, im_out, calc_ssim=True, calc_msssim=True, calc_psnr=True) fout_str = _append_to_measures_f(fout, bpp, actual_bpp, ssim, msssim, psnr) print(fout_str, end='\r') os.remove(output_image_j2_p) print()
def bpg_measure_over_interval(input_image_p, fout, grid): input_image_root_p, ext = os.path.splitext(input_image_p) assert ext == '.png', 'Expected PNG to convert to BMP, got {}'.format( input_image_p) for q in map(int, grid): with remove_file_after(bpg_compress(input_image_p, q)) as out_p: bpp = bpp_of_bpg_image(out_p) with remove_file_after(decode_bpg_to_png(out_p)) as out_png_p: im_in = scipy.ndimage.imread(input_image_p) im_out = scipy.ndimage.imread(out_png_p) ssim, msssim, psnr = compare_imgs.compare(im_in, im_out, calc_ssim=True, calc_msssim=True, calc_psnr=True) fout_str = _append_to_measures_f(fout, q, bpp, ssim, msssim, psnr) print(fout_str, end='\r') print()