hostshowcase.blogg.se

Convert picture size from 106 kb to mb
Convert picture size from 106 kb to mb













convert picture size from 106 kb to mb

convert picture size from 106 kb to mb

How do I reduce the size of a JPEG to 100kb?.# sizeUnitListWithI = ĭecimalFormat = "." + str(decimalNum) + "f" # ".1f"įinalFormat = "%" + decimalFormat + sizeUnitSeperator + "%s%s" # "%.

convert picture size from 106 kb to mb

SizeUnitList = įor curIdx, eachUnit in enumerate(sizeUnitList): # K=kilo, M=mega, G=giga, T=tera, P=peta, E=exa, Z=zetta, Y=yotta """format size to human readable string""" Referencing Sridhar Ratnakumar's answer, updated to: def formatSize(sizeInBytes, decimalNum=1, isUnitWithI=False, sizeUnitSeperator=""): Order = min(int(order) + order_corr, order_max)įactored = round(value/base**order, precision) Order_corr = order - int(order) >= log(base - 0.5/10**precision, base) Resulting in def abbreviate(value, base=1000, precision=2, suffixes=None): Of course, we can check for it explicitly: if round(value/base**order, 2) = base:īut can we do better? Can we get to know which way the order should be cut before we do the final step?Īssuming 0.5 decimal rounding rule, the above if condition translates into: With the precision set to 2 digits we get: > round(value/base**order, 2) And this is when things start to get a bit difficult. This seems to be exactly what we'd expect until we're required to control output precision. Which, being truncated to the nearest integer and applied back to the input gives > order = int(math.log(value, base)) Namely the case when input like 999_995 is given: Python 3.6.1. Instead, it focuses on one particular issue that many of the other answers miss. What you're about to find below is by no means the most performant or shortest solution among the ones already posted. KB,MB,GB are all multiples of 1000, whereas KiB, MiB, GiB etc are all multiples of 1024. KB means 1000 bytes, whereas KiB means 1024 bytes. NOTE - There is a difference between Kb and KiB.

convert picture size from 106 kb to mb

You can add extra checks for them: def humanizeFileSize(filesize): It however doesn't work if filesize is 0 or negative (because log is undefined for 0 and -ve numbers).

  • Returns file_size/value_of_closest_unit along with unit.
  • (eg if size is 5000 bytes, the closest unit is Kb, so the answer should be X KiB)
  • Divides it by 10 to get the closest unit.
  • P = int(math.floor(math.log(filesize, 2)/10)) How about a simple 2 liner: def humanizeFileSize(filesize): Return format_string.format(quotient, unit) If you need thousands commas, just apply the obvious extension: def prettier_size(n,pow=0,b=1024,u='B',pre=+): PEP8 compliance is an exercise for the reader.

    Convert picture size from 106 kb to mb code#

    This code is both Python 2 and Python 3 compatible. This will do what you need in almost any situation, is customizable with optional arguments, and as you can see, is pretty much self-documenting: from math import logĭef pretty_size(n,pow=0,b=1024,u='B',pre=+):















    Convert picture size from 106 kb to mb