Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def run_pngnq(pngs, speed=3):
print("Running PNGnq ...\n")
out_folder = mk_folder("pngnq_results/speed_" + str(speed))
base = ["./pngnq", "-v", "-s" + str(speed)]
compression_percent = list()
mean_time = list()
saved_output = [] # list of output that needs to be parsed to gather more data
for img in pngs.iterdir():
if img.name.lower().find('png') != -1:
print(img.name)
start = time.time()
command = copy.deepcopy(base)
Path.touch(out_folder / img.name)
command.append("-d")
command.append(str(Path(out_folder)))
command.append(str(Path(img)))
try:
output = subprocess.check_output(command, stderr=subprocess.STDOUT)
mean_time.append(time.time() - start)
compression_percent.append(calc_percent_difference(img, Path(out_folder / img.name)))
except Exception as e:
print("An error occurred with PNGnq:" + str(e))
mean_time = mean(mean_time)
res = {'mean_run_time':mean_time, 'mean_compression': mean(compression_percent),'best_compression_id':compression_percent.index(max(compression_percent)),\
'worst_compression_id':compression_percent.index(min(compression_percent)), 'other':None}
best_methods = list()
compression_percent = list()
increase_percent = list() # list of filesize increase percentages
mean_time = list()
num_pngs = 0
for img in pngs.iterdir():
if img.name.lower().find("png") != -1:
print(img.name)
start = time.time()
command = copy.deepcopy(base)
command.append(img)
command.append(out_folder / img.name)
try:
Path.touch(out_folder / img.name)
output = subprocess.check_output(command, stderr=subprocess.STDOUT)
# output is a bytes-like object, so decode into a string
decoded_output = output.decode('ascii')
# find index of relevant data i.e. best method, compression percentage, etc.
relevant_index = decoded_output.find("Best")
# store relevant data for later processing
if relevant_index != -1:
saved_output.append(decoded_output[relevant_index:])
mean_time.append(time.time() - start)
except Exception as e:
print("An error occurred with PNGCrush:" + str(e))
for line in saved_output:
# best method number is after the first '=' and before the first '('
best_method = int(line[line.find('=') + 1: line.find('(')])
def run_pngnq(pngs, speed=3):
print("Running PNGnq ...\n")
out_folder = mk_folder("pngnq_results/speed_" + str(speed))
base = ["./pngnq", "-v", "-s" + str(speed)]
compression_percent = list()
mean_time = list()
saved_output = [] # list of output that needs to be parsed to gather more data
for img in pngs.iterdir():
if img.name.lower().find('png') != -1:
print(img.name)
start = time.time()
command = copy.deepcopy(base)
Path.touch(out_folder / img.name)
command.append("-d")
command.append(str(Path(out_folder)))
command.append(str(Path(img)))
try:
output = subprocess.check_output(command, stderr=subprocess.STDOUT)
mean_time.append(time.time() - start)
compression_percent.append(calc_percent_difference(img, Path(out_folder / img.name)))
except Exception as e:
print("An error occurred with PNGnq:" + str(e))
mean_time = mean(mean_time)
res = {'mean_run_time':mean_time, 'mean_compression': mean(compression_percent),'best_compression_id':compression_percent.index(max(compression_percent)),\
'worst_compression_id':compression_percent.index(min(compression_percent)), 'other':None}
best_methods = list()
compression_percent = list()
increase_percent = list() # list of filesize increase percentages
mean_time = list()
num_pngs = 0
for img in pngs.iterdir():
if img.name.lower().find("png") != -1:
print(img.name)
start = time.time()
command = copy.deepcopy(base)
command.append(img)
command.append(out_folder / img.name)
try:
Path.touch(out_folder / img.name)
output = subprocess.check_output(command, stderr=subprocess.STDOUT)
# output is a bytes-like object, so decode into a string
decoded_output = output.decode('ascii')
# find index of relevant data i.e. best method, compression percentage, etc.
relevant_index = decoded_output.find("Best")
# store relevant data for later processing
if relevant_index != -1:
saved_output.append(decoded_output[relevant_index:])
mean_time.append(time.time() - start)
except Exception as e:
print("An error occurred with PNGCrush:" + str(e))
for line in saved_output:
# best method number is after the first '=' and before the first '('
best_method = int(line[line.find('=') + 1: line.find('(')])
mean_time = list()
out_folder = mk_folder("pngquant_results/speed_" + str(speed))
speed = "-s" + str(speed)
for img in pngs.iterdir():
if img.name.lower().find('png') != -1:
print(img.name)
start = time.time()
command = copy.deepcopy(base)
command.append("--output")
command.append(str(Path(out_folder / img.name)))
command.append(speed)
command.append(str(Path(img)))
try:
Path.touch(out_folder / img.name)
output = subprocess.check_output(command, stderr=subprocess.STDOUT)
mean_time.append(time.time() - start)
original_size = os.stat(img).st_size
compressed_size = os.stat(Path(out_folder / img.name)).st_size
percent_comp = ((original_size - compressed_size) / original_size) * 100
compression_percent.append(percent_comp)
except Exception as e:
print("An error occurred with PNGQuant:" + str(e))
mean_time = mean(mean_time)
res = {'mean_run_time':mean_time, 'mean_compression': mean(compression_percent),'best_compression_id':compression_percent.index(max(compression_percent)),\
'worst_compression_id':compression_percent.index(min(compression_percent)), 'other':None}
return res
it = list(filter(lambda p: p is not None, it))
if it:
explicit_tmp_dir = it[0]
else:
explicit_tmp_dir = tempfile.TemporaryDirectory().name
try:
# Try to create directory
if not os.path.exists(explicit_tmp_dir):
os.makedirs(explicit_tmp_dir, exist_ok=True)
# Check that it is actually a directory
if not os.path.isdir(explicit_tmp_dir):
raise Exception(
'{} is not a directory.'.format(explicit_tmp_dir))
# Can we interact with directory?
Path.touch(Path(os.path.join(explicit_tmp_dir, '.can_touch')))
# All checks have passed by this point
RVConfig.tmp_dir = explicit_tmp_dir
# If directory cannot be made and/or cannot be interacted
# with, fall back to default.
except Exception as e:
log.warning(
'Root temporary directory cannot be used: {}. Using root: {}'.
format(explicit_tmp_dir, DEFAULT_DIR))
RVConfig.tmp_dir = DEFAULT_DIR
finally:
make_dir(RVConfig.tmp_dir)
log.debug('Temporary directory is: {}'.format(RVConfig.tmp_dir))