Skip to content

Commit 88fe610

Browse files
lguerardehrenfeu
authored andcommitted
Add function to convert size from bytes to a more readable format
1 parent c276511 commit 88fe610

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/imcflibs/imagej/misc.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,3 +790,23 @@ def save_script_parameters(destination, save_file_name="script_parameters.txt"):
790790
f.write("%s: %s\n" % (key, str(val)))
791791

792792
timed_log("Saved script parameters to: %s" % out_path)
793+
794+
def convert_bytes(size):
795+
"""Convert size from bytes to a readable value
796+
797+
Parameters
798+
----------
799+
size : int
800+
Byte size
801+
802+
Returns
803+
-------
804+
str
805+
Easy to read value with the correct unit
806+
"""
807+
for x in ["bytes", "KB", "MB", "GB", "TB"]:
808+
if size < 1024.0:
809+
return "%3.1f %s" % (size, x)
810+
size /= 1024.0
811+
812+
return size

0 commit comments

Comments
 (0)