Question

How to resize images on Spaces

I have 15000 photos and their sizes are 2000x2000.

I want to use them as;

blabla.com/450x600/images/blabla.jpg blabla.com/1200x0/images/blabla.jpg

how can this be possible for spaces? Is there any tool to make it?


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

so you could try something like this like mariogonzalez said. this will pull from one bucket and resize them locally, and upload them to another bucket, renaming it in the process. If you want to convert the images to a few dimensions, you can just expand the script to do so.

hth

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import boto3
import os
import subprocess
import time

session = boto3.session.Session()
####################################################################
client = session.client('s3',
                        region_name='sfo2',
                        endpoint_url='https://sfo2.digitaloceanspaces.com',
                        aws_access_key_id='32gfeGRITgtfrYOURKEYIDGTRHtrh45gdHG',
                        aws_secret_access_key='FwrTUCDbQG8/QMbYOUACCESSKEYRwZg3etrHtrjrJTRuREnl84')
bucket1 = ("sourcebucket")  #the bucket holding original images
bucket2 = ("emptybucket")  #an empty bucket you can send the converted images to
workdirectory = ("/home/username/pics/")
resizeDimension = ("512x512")
####################################################################

uploaded = []
filelist = []
failed = []
#os.mkdir(workdirectory)

def GetNames(s3, **base_kwargs):  #gets the list of images in the bucket
    continuation_token = None
    while True:
        list_kwargs = dict(MaxKeys=1000, **base_kwargs)
        if continuation_token:
            list_kwargs['Marker'] = continuation_token
        response = client.list_objects(**list_kwargs)
        yield from response.get('Contents', [])
        time.sleep(1)
        if not response.get('IsTruncated'):  # At the end of the list?
            break
        else:
            continuation_token = response.get('NextMarker')

def convert(filename):
    try:
        print("CV",filename)
        subprocess.run('convert ' + workdirectory + filename+'[0]' +' -resize ' + resizeDimension +" "+ workdirectory + resizeDimension + filename, shell=True)
    except Exception as e:
        failed.append(filename)

def uploadImage(filename):
    if os.path.exists(workdirectory+resizeDimension+filename):
        try:
            print("UPLOADING :",resizeDimension+filename)

            client.upload_file(workdirectory+resizeDimension+filename,  # Path to local file
                                bucket2,  # Name of Space
                                resizeDimension+filename)
        except Exception as e:
            print(e)
            s = str(e)
            return False
        return True

try:
    for file in GetNames(client, Bucket=bucket1):
        filelist.append(file['Key'])
        print("DL",file['Key'])
        try:
            with open(workdirectory+file['Key'], 'wb') as f:
                client.download_fileobj(bucket1, file['Key'], f)
                convert(file['Key'])
        except Exception as e:
            s = str(e)
            print(e)

except Exception as e:
    s = str(e)
    print(e)


for x in filelist:
    try:
        uploadImage(x)
    except Exception as e:
        s = str(e)
        print(e)

You can use ImageMagick for this, there are hooks for most programming languages (python / ruby / php)

I would recommend Python, with the Boto3 library (official S3 library by amazon). With Python + ImageMagick + Boto3 + Spaces, you should be in business in no time.

PS: A more conventional / better (in my opinion), route for your images might be something like

example.com/images/blahblah/450x600.jpg

or

example.com/images/blahblah/blah-450x600.jpg

hi you can use Photoshop and paint tool for resizing your photo Thanks AINJ IT SERVICES PVT LTD www.ainj.in