code to upload images to s3
This commit is contained in:
parent
58bf8ca74e
commit
e6178c8a72
api
@ -32,7 +32,7 @@ def get(user, id):
|
||||
if obj['type'] == 'pattern' and 'preview' in obj and '.png' in obj['preview']:
|
||||
obj['previewUrl'] = uploads.get_presigned_url('projects/{0}/{1}'.format(proj['_id'], obj['preview']))
|
||||
del obj['preview']
|
||||
wif.draw_image(obj)
|
||||
wif.draw_image(obj, with_plan=True)
|
||||
return obj
|
||||
|
||||
def copy_to_project(user, id, project_id):
|
||||
|
@ -32,6 +32,14 @@ def get_presigned_url(path):
|
||||
}
|
||||
)
|
||||
|
||||
def upload_file(path, data):
|
||||
s3 = get_s3()
|
||||
s3.upload_fileobj(
|
||||
data,
|
||||
os.environ['AWS_S3_BUCKET'],
|
||||
path,
|
||||
)
|
||||
|
||||
def get_file(key):
|
||||
s3 = get_s3()
|
||||
return s3.get_object(
|
||||
|
@ -1,5 +1,7 @@
|
||||
import io, time
|
||||
import configparser
|
||||
from PIL import Image, ImageDraw
|
||||
from api import uploads
|
||||
|
||||
def normalise_colour(max_color, triplet):
|
||||
color_factor = 256/max_color
|
||||
@ -204,6 +206,13 @@ def loads(wif_file):
|
||||
|
||||
return draft
|
||||
|
||||
def generate_images(obj):
|
||||
try:
|
||||
preview = draw_image(obj)
|
||||
full_preview = draw_image(obj, with_plan=True)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
def draw_image(obj, with_plan=False):
|
||||
if not obj or not obj['pattern']: raise Exception('Invalid pattern')
|
||||
BASE_SIZE = 10
|
||||
@ -356,5 +365,12 @@ def draw_image(obj, with_plan=False):
|
||||
(drawdown_right - x * BASE_SIZE, drawdown_top + (y + 1) * BASE_SIZE),
|
||||
], fill=colour_tuple(warp_colour if thread_type == 'warp' else weft_colour))
|
||||
|
||||
|
||||
img.save("image.png", "PNG")
|
||||
in_mem_file = io.BytesIO()
|
||||
img.save(in_mem_file, 'PNG')
|
||||
in_mem_file.seek(0)
|
||||
file_name = 'preview-{0}_{1}_{2}.png'.format(
|
||||
'full' if with_plan else 'base', obj['_id'], int(time.time())
|
||||
)
|
||||
path = 'projects/{}/{}'.format(obj['project'], file_name)
|
||||
uploads.upload_file(path, in_mem_file)
|
||||
return file_name
|
||||
|
Loading…
Reference in New Issue
Block a user