Tuesday, 30 March 2021

How to reduce an Image file size before uploading to a server

Lot of application allow sharing an image, which is picked from the gallery.

Do they upload the original image file? Which is like 1-3 mb? Or do they process?

In any case, how can I take the image from a filepath, reduce its size by lowering the resolution and save it some where else and the try to upload?

I tried:

Bitmap photo = decodeSampledBitmapFromFile(filePath, DESIRED_WIDTH,
                    DESIRED_HEIGHT);

FileOutputStream out = new FileOutputStream(filePath);
photo.compress(Bitmap.CompressFormat.JPEG, 100, out);

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth,
        int reqHeight) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    final int height = options.outHeight;
    final int width = options.outWidth;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    int inSampleSize = 1;

    if (height > reqHeight) {
        inSampleSize = Math.round((float) height / (float) reqHeight);
    }
    int expectedWidth = width / inSampleSize;

    if (expectedWidth > reqWidth) {
        inSampleSize = Math.round((float) width / (float) reqWidth);
    }
    options.inSampleSize = inSampleSize;
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(path, options);
}

But is this they right way to do? Because I have seen answers suggesting compression operation takes rather big amount of time here



from How to reduce an Image file size before uploading to a server

ModuleNotFoundError when I am trying to import package

my_controller.py is as follow:

from models import Person

david = Person('David')

And my project structure is

app
├── controller
│   └── my_controller.py
│   
└── models
    └── __init__.py
    └── person.py

I must do something wrong because I keep get

ModuleNotFoundError: No module named 'models'    

What is the correct way to import class Person to my_controller.py?

PS I am working on ubuntu



from ModuleNotFoundError when I am trying to import package

Handling rate limit exceptions with Cursor in tweepy

I'm trying to find the correct way to handle rate limits when iterating through a list of followers using the Cursor object. Here is what I'm trying:

while True:
    try:
        for follower in tweepy.Cursor(api.followers, id=root_usr).items():
            print(follower.id)
    except tweepy.TweepError:
        # hit rate limit, sleep for 15 minutes
        print('Rate limited. Sleeping for 15 minutes.')
        time.sleep(15 * 60 + 15)
        continue
    except StopIteration:
        break

This is probably incorrect, since an exception will make the for loop start from the beginning again. What's the right way to iterate through all of root_usr's followers while also handling the rate limit problems?



from Handling rate limit exceptions with Cursor in tweepy