In [157]:
import numpy as np
import urllib.request
import boto3
In [22]:
!pip3 install boto3
In [156]:
path = "wrong_img_id.txt"
with open(path, "r") as f:
id_list = f.readlines()
img_id = []
for i in id_list:
img_id.append(i.strip("\n"))
print("")
In [ ]:
with urllib.request.urlopen("http://www.python.org") as url:
s = url.read()
In [64]:
# def save_img(path, img_url):
# user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
# user_agent = "Magic Browser"
# headers={'User-Agent':user_agent,}
# f = open(path,'wb')
# r=urllib.request.Request(img_url,None,headers)
# f.write(urllib.request.urlopen(r).read())
# f.close()
# for i in img_id[:2]:
# path = "img_file2/" + i + ".jpg"
# img_url = "https://s3.amazonaws.com/travel-with-friends/img_file/" + i +".jpg"
# save_img(path, img_url)
In [133]:
# import boto3
# aws_access_key_id = "AKIAIW722D6CEHVQ4CEA"
# aws_secret_access_key = "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"
# region_name = "us-east-1"
# # conn = boto3.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
# session = boto3.Session(aws_access_key_id, aws_secret_access_key, region_name)
# bucket = session.resource("s3").Bucket("travel-with-friends")
# for key in bucket.list():
# print (key.name.encode('utf-8'))
In [160]:
import os
from boto.s3.connection import S3Connection
from boto.s3.key import Key
aws_access_key_id = "AKIAIZ3QOJ42ZZ7PVKCQ"
aws_secret_access_key = "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"
conn = S3Connection(aws_access_key_id, aws_secret_access_key)
bucket = conn.get_bucket('travel-with-friends')
#download from s3
# for i in bucket.list():
# s3_img_name= str(i.key).replace("img_file/","").replace(".jpg","") #remove s3_path to just the name of the file
# if s3_path in img_id: #check if the file is in the target_list
# i.get_contents_to_filename("img_file2/"+s3_img_name+".jpg") #download+ save to path
#upload to s3
img_list= os.listdir("img_file/")
for count, img in enumerate(img_list):
img_name = img
path = 'img_file/' +img_name
s3_path = "img_file/" + img_name
k = Key(bucket)
k.key = s3_path
k.set_contents_from_filename(path)
if count%1000 ==0:
print("already finish " + str(count) +" images")
In [144]:
k.set_contents_from_filename?
In [138]:
key_name = '0.jpg'
path = 'img_file2/' #Directory Under which file should get upload
full_key_name = os.path.join(path, key_name)
k = bucket.new_key(full_key_name)
k.set_contents_from_filename(key_name)
In [37]:
# conn = conn.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
# bucket = conn.get_bucket(AWS_BUCKET_NAME)
# for file in bucket.list("FOLDER_NAME/", "/"):
# <do something with required file>
Out[37]:
In [ ]:
# resource = session.resource('s3')
# bucket = resource.Bucket(bucket_name)
# for s3_key in self.client.list_objects(Bucket=self.bucket_name, Prefix=directory_path)['Contents']:
# s3_object = s3_key['Key']
# if s3_object not in exclude_file_names:
# bucket.download_file(file_path, download_path + str(s3_object.split('/')[-1])
In [ ]:
# for bucket in s3.buckets.all():
# print(bucket.name)
# # Upload a new file
# data = open('test.jpg', 'rb')
# s3.Bucket('my-bucket').put_object(Key='test.jpg', Body=data)
In [70]:
import cv2
In [112]:
import math, cv2, numpy as np
# load up an image
img_path = "img_file/0.jpg"
img = cv2.imread(img_path)
aspect_len= int(100)
weight, height, depth = img.shape
ratio = float(height) / float(weight)
if ratio <= 1:
new_w = aspect_len
new_h = int(aspect_len*ratio)
else:
new_w = int(aspect_len*ratio)
new_h = aspect_len
print (new_w,new_h)
# new_h = int(math.sqrt(TARGET_PIXEL_AREA / ratio) + 0.5)
# new_w = int((new_h * ratio) + 0.5)
img2 = cv2.resize(img, (new_w,new_h))
save_path = "img_file2/0.jpg"
cv2.imwrite(save_path, img2)
Out[112]:
In [132]:
import os
img_list= os.listdir("img_file/")
for count, img in enumerate(img_list[1:]):
img_process(img)
if count%1000 ==0:
print("already finish " + str(count) +" images")
In [131]:
def img_process(img_name):
img_path = "img_file/"+img_name
img = cv2.imread(img_path)
# print(img.shape)
aspect_len= int(100)
height, weight, depth = img.shape
ratio = float(height) / float(weight)
# print(ratio)
if ratio >= 1:
new_w = aspect_len
new_h = int(aspect_len*ratio)
else:
new_w = int(aspect_len/ratio)
new_h = aspect_len
# new_h = int(math.sqrt(TARGET_PIXEL_AREA / ratio) + 0.5)
# new_w = int((new_h * ratio) + 0.5)
img2 = cv2.resize(img, (new_w,new_h))
# print(img2.shape)
save_path = "img_file2/icon_"+str(img_name)
cv2.imwrite(save_path, img2)
In [113]:
s3_connection = boto.connect_s3()
bucket = s3_connection.get_bucket('your bucket name')
key = boto.s3.key.Key(bucket, 'some_file.zip')
with open('some_file.zip') as f:
key.send_file(f)
Out[113]:
In [ ]: