def save_thumb_to_s3(page, path): i = Image.open(path) size = i.size is_wide_thumb = size[0] > size[1] thumb = i.copy() thumb_filename = "%s_full.png" % (page.id) thumbIO = cStringIO.StringIO() thumb.save(thumbIO, "png") thumbIO.seek(0) s3.save_string_to_bucket(localsettings.S3_BUCKET_THUMB, thumb_filename, thumbIO.read(), "public-read", "image/png") thumbIO.close() for dim in settings.THUMB_DIMENSIONS: w = dim[0] h = dim[1] if is_wide_thumb and ZOOM_AND_CROP_WIDE_THUMBS: ratio = max(float(h) / size[1], float(w) / size[0]) else: ratio = min(float(h) / size[1], float(w) / size[0]) new_width = int(size[0] * ratio) new_height = int(size[1] * ratio) if i.mode == 'RGBA': thumb = Image.new('RGBA', (w, h), (255, 255, 255, 255)) else: thumb = Image.new('RGB', (w, h), (255, 255, 255)) thumbImage = i.resize((new_width, new_height), Image.ANTIALIAS) if i.mode == 'RGBA': if is_wide_thumb and ZOOM_AND_CROP_WIDE_THUMBS: # Cropping from the top, left. thumbImage = thumbImage.crop((0, 0, new_width, new_height)) thumb.paste(thumbImage, (0, 0), thumbImage) else: thumb.paste(thumbImage, ((w - new_width) / 2, (h - new_height) / 2), thumbImage) thumb = thumb.convert('RGB') else: thumb.paste(thumbImage, ((w - new_width) / 2, (h - new_height) / 2)) thumb_filename = "%s_%d_%d.jpg" % (page.id, w, h) thumbIO = cStringIO.StringIO() thumb.save(thumbIO, "jpeg", quality=95) thumbIO.seek(0) s3.save_string_to_bucket(localsettings.S3_BUCKET_THUMB, thumb_filename, thumbIO.read(), "public-read", "image/jpeg") thumbIO.close() page.thumb_path = "s3" page.thumb_dir = "s3" page.save()
def save_uploaded_avatar(profile, avatar_form): avatar_upload = avatar_form.cleaned_data["avatar"] filename = "%d_%d" % (profile.user.id , now()) s3.save_string_to_bucket("avatar", filename, avatar_upload.content) make_upload_thumbnails(filename, avatar_upload.content) profile.avatar = filename profile.avatar_filetype = EXTENSION profile.avatar_builtin = False profile.save()
def make_upload_thumbnails(filename, data): dataIO = cStringIO.StringIO(data) orig = Image.open(dataIO) orig = orig.crop(middle_square(orig.size)) for size in SIZES: thumb = orig.resize((size, size), Image.ANTIALIAS) if thumb.mode != "RGB": thumb = thumb.convert("RGB") new_filename = "%s_%d.%s" % (filename, size, EXTENSION) new_file = cStringIO.StringIO() thumb.save(new_file, "jpeg", quality=95) new_file.seek(0) s3.save_string_to_bucket("avatar", new_filename, new_file.read()) new_file.close() dataIO.close()
def set_file(self, content): if self.media_type == MEDIA_TYPE_IMAGE: s3.save_string_to_bucket( localsettings.S3_BUCKET_UPLOAD, self.id + ".fimage", content, content_type=self.mimetype )
def save_ppt_to_s3(self, content): key = self.get_s3_key() s3.save_string_to_bucket(localsettings.S3_BUCKET_UPLOAD, key, content) self.uploaded_to_s3 = True self.save()
def save_css_file_to_s3(task, file): s3.save_string_to_bucket(localsettings.S3_BUCKET_CSS, task.id + ".css", file, "public-read", "text/css") return "S3"