def zip_from_image(file, sizes, quality=75, default_size=1600, alt_text="YOU MUST ENTER ALT TEXT", image_tag_path="images/"): rand = ''.join(random.sample(string.letters, 15)) os.mkdir(os.path.join(dir, 'temp',rand)) file_path, file_name = os.path.split(file.filename) base, ext = os.path.splitext(file_name) base = base.lower() ext = ext.lower() image_name = "{}_{}{}".format(base, 'orig', ext) image_path = os.path.join(dir, 'temp', rand, image_name) f = file.save(image_path) zip_name = "{}.zip".format(base).lower() zip_path = os.path.join(dir, 'temp', rand, zip_name) image = Image.open(image_path) img_srcset_tuples = [] with ZipFile(zip_path, 'w') as zipfile: zipfile.write(image_path, image_name) os.remove(image_path) for size in sizes: image_name = "{}_{}{}".format(base, size, ext) img_srcset_tuples.append(("{}{}".format(image_tag_path, image_name), size)) image_path = os.path.join(dir, 'temp', rand, image_name) img = ResizeToFit(width=size, upscale=True).process(image) img.save(image_path, progressive=True, exif="", optimize=True, quality = quality, icc_profile=img.info.get('icc_profile')) img.close() zipfile.write(image_path, image_name) os.remove(image_path) # src_file = open(os.path.join(dir, 'temp', rand, 'img_tag.txt') # src_file.write('<img srcset="') img_srcset_strings = map(img_src_formatter, img_srcset_tuples) best_size = take_closest(default_size, sizes) img_src_string = img_srcset_tuples[sizes.index(best_size)][0] img_tag_html = '<img srcset="{}" sizes="(min-width: 1px) 100vw, 100vw" src="{}" alt="{}">'.format(', '.join(img_srcset_strings), img_src_string, alt_text) img_tag_html_file_name = os.path.join(dir, 'temp', rand, 'html.txt') img_tag_html_file = open(img_tag_html_file_name, 'w') img_tag_html_file.write(img_tag_html) img_tag_html_file.close() zipfile.write(img_tag_html_file_name, 'html.txt') return zip_path
def export_dataset_photo_task(photo_id, out_dir): photo = Photo.objects.filter(id=photo_id).select_related( 'license', 'intrinsic_points', 'intrinsic_points__opacities', 'intrinsic_comparisons' 'intrinsic_comparisons__responses', )[0] image_filename = '%s.png' % photo_id json_filename = '%s.json' % photo_id intrinsic_points = photo.intrinsic_points.all() intrinsic_comparisons = photo.intrinsic_comparisons.all() intrinsic_points = intrinsic_points.filter(opaque__isnull=False, ) intrinsic_comparisons = intrinsic_comparisons.filter( point1__opaque=True, point2__opaque=True, darker__isnull=False, darker__in=("1", "2", "E"), darker_score__isnull=False, darker_score__gt=0) intrinsic_points = [{ 'id': p.id, 'x': p.x, 'y': p.y, 'sRGB': p.sRGB, 'min_separation': float(p.min_separation), 'opaque': p.opaque, 'opaque_score': p.opaque_score, 'opaque_method': p.opaque_method, 'opaque_responses': [{ 'id': r.id, 'mturk_worker_id': r.user.mturk_worker_id, 'opaque': r.opaque, 'time_ms': r.time_ms, 'time_active_ms': r.time_active_ms, } for r in p.opacities.filter(invalid=False).order_by('id')] } for p in intrinsic_points] intrinsic_comparisons = [{ 'id': c.id, 'point1': c.point1_id, 'point2': c.point2_id, 'min_separation': float(min(c.point1.min_separation, c.point2.min_separation)), 'darker': c.darker, 'darker_score': c.darker_score, 'darker_method': c.darker_method, 'darker_responses': [{ 'id': r.id, 'mturk_worker_id': r.user.mturk_worker_id, 'darker': r.darker, 'confidence': r.confidence, 'time_ms': r.time_ms, 'time_active_ms': r.time_active_ms, } for r in c.responses.filter(invalid=False).order_by('id')] } for c in intrinsic_comparisons] if photo.flickr_user_id: attribution_name = photo.flickr_user.display_name attribution_url = photo.get_flickr_url() else: attribution_name = photo.attribution_name attribution_url = photo.attribution_url data = { 'photo': photo_id, 'image_filename': image_filename, 'aspect_ratio': photo.aspect_ratio, 'flickr_user': photo.flickr_user.username if photo.flickr_user_id else None, 'flickr_id': photo.flickr_id, 'license': { 'name': photo.license.name, 'url': photo.license.url, 'cc': photo.license.creative_commons, }, 'light_stack': photo.light_stack_id, 'attribution_name': attribution_name, 'attribution_url': attribution_url, 'intrinsic_points': intrinsic_points, 'intrinsic_comparisons': intrinsic_comparisons, } if not os.path.exists(out_dir): try: os.makedirs(out_dir) except: # this could be multiple attempts to create the same directory. # the file-write step will fail if this is a true fail. pass with open(os.path.join(out_dir, json_filename), 'w') as f: f.write(json.dumps(data, indent=2, sort_keys=True)) image = ResizeToFit(512, 512).process(open_image(photo.image_orig)) image.save(os.path.join(out_dir, image_filename))