def get_barcode(request, slide_id): try: s = Alopecia.objects.get(pk=slide_id) except Alopecia.DoesNotExist: raise Http404 if s.SlideType == 1: ds = pydicom.dcmread(str(s.UrlPath)) label = get_PIL_image(ds) response = HttpResponse(content_type='image/png') label.thumbnail(MAX_THUMBNAIL_SIZE) label.save(response, "PNG") response['Content-Disposition'] = 'attachment; filename="label.png"' return response else: file = path.join(settings.HISTOSLIDE_SLIDEROOT, str(s.UrlPath)) slide = OpenSlide(file) if ('label' in slide.associated_images.keys() and slide.associated_images['label'] != None): response = HttpResponse(content_type='image/png') label = slide.associated_images['label'] if (label.size[0] < label.size[1]): label = label.transpose(Image.ROTATE_270) basewidth = 200 wpercent = (basewidth / float(label.size[0])) hsize = int((float(label.size[1]) * float(wpercent))) label = label.resize((basewidth, hsize), Image.ANTIALIAS) label.save(response, "PNG") response[ 'Content-Disposition'] = 'attachment; filename="barcode.png"' return response elif (('macro' in slide.associated_images.keys() and slide.associated_images['macro'] != None)): response = HttpResponse(content_type='image/png') label = slide.associated_images['macro'] if (label.size[0] > label.size[1]): label = label.transpose(Image.ROTATE_270) basewidth = 200 wpercent = (basewidth / float(label.size[0])) hsize = int((float(label.size[1]) * float(wpercent))) label = label.resize((basewidth, hsize), Image.ANTIALIAS) label = label.crop((0, 0, 200, 200)) label.save(response, "PNG") response[ 'Content-Disposition'] = 'attachment; filename="barcode.png"' return response else: file_path = settings.STATICFILES_DIRS[ 0] + '/images/placeholder-image.png' if path.exists(file_path): with open(file_path, 'rb') as fh: response = HttpResponse(fh.read(), content_type="image/*") response[ 'Content-Disposition'] = 'inline; filename=' + path.basename( file_path) return response raise Http404
def gen_thumbnail_url(request, slide_id): try: s = Alopecia.objects.get(pk=slide_id) except Alopecia.DoesNotExist: raise Http404 label_name = str(s.LabelUrlPath).split('/')[-1] if label_name != 'placeholder.png': return JsonResponse({ 'thumbnail': request.build_absolute_uri(str(s.LabelUrlPath)), }) else: if s.SlideType == 1: ds = pydicom.dcmread(str(s.UrlPath)) thumbnail = get_PIL_image(ds) response = HttpResponse(content_type='image/png') thumbnail.thumbnail(MAX_THUMBNAIL_SIZE) filename = str(s.UrlPath).split('/')[-1] fWithoutExt = filename.split('.') fWithoutExt.pop() fWithoutExt = ''.join(fWithoutExt) thumbnailName = fWithoutExt + '.thumbnail' dirPath = settings.STATICFILES_DIRS[0] + '/images/thumbnail' thumbnail.save(dirPath + '/' + thumbnailName, 'JPEG') return JsonResponse({ 'thumbnail': request.build_absolute_uri('/static/images/thumbnail/') + thumbnailName, }) else: file = path.join(settings.HISTOSLIDE_SLIDEROOT, str(s.UrlPath)) slide = OpenSlide(file) thumbnail = slide.get_thumbnail((800, 600)) filename = str(s.UrlPath).split('/')[-1] fWithoutExt = filename.split('.') fWithoutExt.pop() fWithoutExt = ''.join(fWithoutExt) thumbnailName = fWithoutExt + '.thumbnail' dirPath = settings.STATICFILES_DIRS[0] + '/images/thumbnail' thumbnail.save(dirPath + '/' + thumbnailName, 'JPEG') return JsonResponse({ 'thumbnail': request.build_absolute_uri('/static/images/thumbnail/') + thumbnailName, })
def get_thumbnail(request, slide_id): try: s = Alopecia.objects.get(pk=slide_id) except Alopecia.DoesNotExist: raise Http404 label_name = str(s.LabelUrlPath).split('/')[-1] if label_name != 'placeholder.png': file_path = str(s.LabelUrlPath) if path.exists(file_path): with open(file_path, 'rb') as fh: response = HttpResponse(fh.read(), content_type="image/*") response[ 'Content-Disposition'] = 'inline; filename=' + path.basename( file_path) return response if s.SlideType == 1: ds = pydicom.dcmread(str(s.UrlPath)) label = get_PIL_image(ds) response = HttpResponse(content_type='image/png') label.thumbnail(MAX_THUMBNAIL_SIZE) label.save(response, "PNG", optimize=True, quality=95) response['Content-Disposition'] = 'attachment; filename="label.png"' return response else: file = path.join(settings.HISTOSLIDE_SLIDEROOT, str(s.UrlPath)) slide = OpenSlide(file) if (('macro' in slide.associated_images.keys() and slide.associated_images['macro'] != None)): response = HttpResponse(content_type='image/png') label = slide.associated_images['macro'] if (label.size[0] > label.size[1]): label = label.transpose(Image.ROTATE_270) basewidth = 200 wpercent = (basewidth / float(label.size[0])) hsize = int((float(label.size[1]) * float(wpercent))) label = label.resize((basewidth, hsize), Image.ANTIALIAS) if (label.size[1] > 620): label = label.crop((0, 120, label.size[0], label.size[1])) if (('label' in slide.associated_images.keys() and slide.associated_images['label'] != None)): barcode = slide.associated_images['label'] if (barcode.size[0] < barcode.size[1]): barcode = barcode.transpose(Image.ROTATE_270) bw = 200 wp = (bw / float(barcode.size[0])) hs = int((float(barcode.size[1]) * float(wp))) barcode = barcode.resize((bw, hs), Image.ANTIALIAS) new_image = Image.new('RGB', (200, barcode.size[1] + label.size[1]), (250, 250, 250)) new_image.paste(barcode, (0, 0)) new_image.paste(label, (0, barcode.size[1])) label = new_image label.save(response, "PNG") response[ 'Content-Disposition'] = 'attachment; filename="label.png"' return response else: file_path = settings.STATICFILES_DIRS[ 0] + '/images/placeholder-image.png' if path.exists(file_path): with open(file_path, 'rb') as fh: response = HttpResponse(fh.read(), content_type="image/*") response[ 'Content-Disposition'] = 'inline; filename=' + path.basename( file_path) return response raise Http404