Skip to content

Add function to convert size from bytes to a more readable format #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/imcflibs/imagej/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,3 +711,23 @@
IJ.log("Conversion to .ims is finished.")
else:
IJ.log("Conversion failed with error code: %d" % result)

def convert_bytes(size):
"""Convert size from bytes to a readable value.

Parameters
----------
size : int
Byte size

Returns
-------
str
Easy to read value with the correct unit
"""
for x in ["bytes", "KB", "MB", "GB", "TB"]:
if size < 1024.0:
return "%3.1f %s" % (size, x)
size /= 1024.0

Check warning on line 731 in src/imcflibs/imagej/misc.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/misc.py#L728-L731

Added lines #L728 - L731 were not covered by tests

return size

Check warning on line 733 in src/imcflibs/imagej/misc.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/misc.py#L733

Added line #L733 was not covered by tests
Loading