def import_andalusian_catalog(request): message="" if request.method == 'POST': form = forms.CsvAndalusianCatalogForm(request.POST, request.FILES) if form.is_valid(): transliterator = ALA_LC_Transliterator() vocalizer=TashkeelClass() omited = 0 csv_file = form.cleaned_data['csv_file'] reader = csv.reader(csv_file.read().splitlines()) next(reader) for row in reader: genre = row[0] rec_mbid = row[11] nawba = row[14] tab = row[15] form = row[16] mizan = row[17] if rec_mbid: section_start = row[18] section_end = row[19] genre, created = andalusian.models.Genre.objects.get_or_create(name = genre.decode('utf8')) rec = andalusian.models.Recording.objects.get(mbid = rec_mbid) sec, created = andalusian.models.Section.objects.get_or_create(recording=rec, start_time=section_start, end_time=section_end) tab = andalusian.models.Tab.objects.get(name=tab.decode('utf8')) form = andalusian.models.Form.objects.get(name=form.decode('utf8')) nawba = andalusian.models.Nawba.objects.get(name=nawba.decode('utf8')) mizan = andalusian.models.Mizan.objects.get(name=mizan.decode('utf8')) voc = vocalizer.tashkeel(row[0].decode('utf8')) tr = transliterator.do(voc.strip()) genre.transliterated_name = arabic_reshaper.reshape(tr) genre.save() voc = vocalizer.tashkeel(rec.title) tr = transliterator.do(voc.strip()) rec.transliterated_title = arabic_reshaper.reshape(tr) rec.genre = genre rec.save() sec.mizan = mizan sec.tab = tab sec.form = form sec.nawba = nawba sec.save() else: omited += 1 message = "The elements has been successfully loaded" if omited: message += "(%d rows omited)" % omited else: form = forms.CsvAndalusianCatalogForm() params = {"form": form, "message": message} return render(request, "dashboard/load_catalog_csv.html", params)
def import_andalusian_elements(request): message="" if request.method == 'POST': form = forms.CsvAndalusianForm(request.POST, request.FILES) if form.is_valid(): transliterator = ALA_LC_Transliterator() vocalizer=TashkeelClass() csv_file = form.cleaned_data['csv_file'] reader = csv.reader(csv_file.read().splitlines()) klass = None if form.cleaned_data['elem_type'] == 'tabs': klass = andalusian.models.Tab elif form.cleaned_data['elem_type'] == 'nawbas': klass = andalusian.models.Nawba elif form.cleaned_data['elem_type'] == 'forms': klass = andalusian.models.Form elif form.cleaned_data['elem_type'] == 'mizans': klass = andalusian.models.Mizan if klass: for row in reader: elem, created = klass.objects.get_or_create(name = row[0].decode('utf8')) voc = vocalizer.tashkeel(row[0].decode('utf8')) tr = transliterator.do(voc.strip()) elem.transliterated_name = arabic_reshaper.reshape(tr) elem.save() message = "The elements has been successfully loaded" else: form = forms.CsvAndalusianForm() params = {"form": form, "message": message} return render(request, "dashboard/load_csv.html", params)
def text(self, text): """The text to be rendered. Use \\\\n to make new lines. Issues: May be slow, and pyglet has a memory leak when setting text. For these reasons, this function checks so that it only updates the text if it has changed. So scripts can safely set the text on every frame, with no need to check if it has actually altered. """ if text == self.text: # only update for a change return if text is not None: text = str(text) # make sure we have unicode object to render # deal with some international text issues. Only relevant for Python: # online experiments use web technologies and handle this seamlessly. style = self.languageStyle.lower() # be flexible with case if style == 'arabic' and haveArabic: # reshape Arabic characters from their isolated form so that # they flow and join correctly to their neighbours: text = arabic_reshaper.reshape(text) if style == 'rtl' or style == 'arabic' and haveArabic: # deal with right-to-left text presentation by applying the # bidirectional algorithm: text = bidi_algorithm.get_display(text) # no action needed for default 'ltr' (left-to-right) option self.__dict__['text'] = text if self.useShaders: self._setTextShaders(text) else: self._setTextNoShaders(text) self._needSetText = False
def reshape(text): """Reshapes arabic in order to display characters from right to left """ if platform.system() == "Darwin": return text else: reshaped_text = arabic_reshaper.reshape(text) bidi_text = get_display(reshaped_text) return bidi_text
def get_dictionary(): arabicWords = [ arabic_reshaper.reshape(u"وزارة"), arabic_reshaper.reshape(u"الشؤون"), arabic_reshaper.reshape(u"الخارجيّة"), arabic_reshaper.reshape(u"شمس"), arabic_reshaper.reshape(u"الله"), arabic_reshaper.reshape(u"نبي"), arabic_reshaper.reshape(u"عام"), arabic_reshaper.reshape(u"مليون"), ] return arabicWords
def create_images(label,count=100,font_size = 17): # here we want to create count images of label with # different filters/rotations etc.. img = Image.new("L", (100, 100),200) draw=ImageDraw.Draw(img) font=ImageFont.truetype("/Library/Fonts/Arial Unicode.ttf", font_size) label_correct = arabic_reshaper.reshape(label) label_correct = get_display(label_correct) draw.text((22,22),label_correct,font=font) img.save('pics/' +label + '.png') img.close()
def renderImage(word, label): reshaped = arabic_reshaper.reshape(u"%s" % word) bidi = get_display(reshaped) for f in fList: #print(f) font = ImageFont.truetype(fFolder+f, fntSize) width, height = font.getsize(reshaped) img = Image.new('RGB', (width, height), "white") d = ImageDraw.Draw(img) d.text((0, 0), bidi, fill="black", font=font) img.save(iFolder+"%s_%s_%s.png" % (label, str(fntSize), f[:-4]))
def process_single_image(word, label, size, fontsize = 50, loc = (25,25)): if not os.path.exists('pics/' + label): os.makedirs('pics/' + label) img = Image.new("L", size ,256) # greyscale and white background draw = ImageDraw.Draw(img) font = ImageFont.truetype("/Library/Fonts/Arial Unicode.ttf", fontsize) label_correct = arabic_reshaper.reshape(word) label_correct = get_display(label_correct) draw.text(loc,label_correct,font=font) direc = 'pics/'+label img.save(direc + '/original.png') img.close() # now we've created the base picture - now we can process it a bit more create_copies(direc, '/original.png')
def _formatText(self, text): "Generates PDF text output operator(s)" canv = self._canvas font = pdfmetrics.getFont(self._fontname) R = [] # Adding Unicode BiDirection Support # Refer to http://github.com/barsi/openerp-rtl # for more info try: text = type(text) == type(u"") and text or text.decode("utf8") reshaped_text = arabic_reshaper.reshape(text) text = get_display(reshaped_text) except UnicodeDecodeError, e: i, j = e.args[2:4] raise UnicodeDecodeError( *( e.args[:4] + ("%s\n%s==[%s]==%s" % (e.args[4], text[max(i - 10, 0) : i], text[i:j], text[j : j + 10]),) ) )
def draw_text_using_font(text, font_path): reshaped_text = arabic_reshaper.reshape(text) bidi_text = get_display(reshaped_text) image = Image.new("RGBA", (700, 500), (255, 255, 255)) draw = ImageDraw.Draw(image) font = ImageFont.truetype(font_path, 100) draw.text((0, 0), bidi_text, (0, 0, 0), font=font) width, height = draw.textsize(bidi_text, font=font) image_frames = [] for i in xrange(len(bidi_text)): line = bidi_text[:i + 1] symbol = bidi_text[i:i + 1] w, _ = draw.textsize(line, font=font) line_frame = [(0, 0), (w, height)] symbol_width, _ = draw.textsize(symbol, font=font) symbol_frame = [(w - symbol_width, 0), (symbol_width, height)] image_frames.append({'symbol': symbol, 'frame' : symbol_frame}) draw.rectangle(line_frame, outline=(0, 0, 0)) return image, image_frames
def Camera(): camera = cv2.VideoCapture(0) while True: value, frame = camera.read() frame = cv2.resize(frame, (600, 500)) blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False) net.setInput(blob) layer_names = net.getLayerNames() output_layers = [ layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers() ] output_layers = net.forward(output_layers) class_ids = [] confidences = [] boxes = [] key = cv2.waitKey(1) for out in output_layers: for detection in out: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > 0.5: center_x = int(detection[0] * frame.shape[1]) center_y = int(detection[1] * frame.shape[0]) w = int(detection[2] * frame.shape[1]) h = int(detection[3] * frame.shape[0]) x = center_x - w / 2 y = center_y - h / 2 class_ids.append(class_id) confidences.append(float(confidence)) boxes.append([x, y, w, h]) indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) for i in indices: i = i[0] box = boxes[i] x = box[0] y = box[1] w = box[2] h = box[3] text = str(classes[class_ids[i]][1]) font = ImageFont.truetype("font.ttf", 35) reshaped_text = arabic_reshaper.reshape(text) arabic_text = get_display(reshaped_text) #print(arabic_text) image_pil = Image.fromarray(frame) draw = ImageDraw.Draw(image_pil) draw.text((round(x), round(y) - 50), arabic_text, (255, 255, 255), font=font) frame = np.array(image_pil) cv2.rectangle(frame, (round(x), round(y)), (round(x + w), round(y + h)), (255, 255, 255), 2) #cv2.putText(frame, "{:.0f}%".format(confidences[i]*100), (round(x),round(y)-40), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,0,0), 2) cv2.imshow("Object Detection Using YOLOv3", frame) if key == 27: break cv2.destroyAllWindows() camera.release()
def reshape_arabic_text(text): return get_display(arabic_reshaper.reshape(text))
def __init__(self, **kwargs): super(Ar_text, self).__init__(**kwargs) self.text = get_display(arabic_reshaper.reshape(""))
def drawTextAtMiddle(imageWidth, draw, text, y, fontSize, color): font = ImageFont.truetype('IRANSansWeb (1).ttf', size=fontSize) reshaped_text = arabic_reshaper.reshape(text) bidi_text = get_display(reshaped_text) w, h = font.getsize(bidi_text) draw.text((((imageWidth - w) / 2), y), bidi_text, fill=color, font=font)
# -*- coding: UTF-8 -*- import arabic_reshaper from bidi.algorithm import get_display from reportlab.platypus import SimpleDocTemplate, Paragraph from reportlab.pdfbase import pdfmetrics from reportlab.lib.styles import ParagraphStyle from reportlab.lib.enums import TA_RIGHT from reportlab.lib.pagesizes import A4 from reportlab.pdfbase.ttfonts import TTFont arabic_text = u'تلفون' new_word = '' for i in arabic_text[::-1]: new_word = new_word+i arabic_text = arabic_reshaper.reshape(arabic_text) new_word = arabic_reshaper.reshape(new_word) # join characters # arabic_text = get_display(arabic_text) # change orientation by using bidi pdf_file=open('disclaimer.pdf','w') pdf_doc = SimpleDocTemplate(pdf_file, pagesize=A4) pdfmetrics.registerFont(TTFont('Arabic-normal', 'KacstOne.ttf')) style = ParagraphStyle(name='Normal', fontName='Arabic-normal', fontSize=12, leading=12. * 1.2) style.alignment=TA_RIGHT pdf_doc.build([Paragraph(arabic_text, style)]) pdf_doc.build([Paragraph(arabic_text[::-1], style)]) pdf_file.close()
def get_arabic_string(string): reshaped_text = arabic_reshaper.reshape(string) bidi_text = get_display(reshaped_text) return bidi_text
def Processing_Image(Image, destination): test = Image large = cv2.imread(test) large = cv2.resize(large, (800, 850)) large = cv2.fastNlMeansDenoisingColored(large, None, 10, 10, 7, 21) rgb = cv2.pyrDown(large) small = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY) k = small kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (4, 2)) grad = cv2.morphologyEx(small, cv2.MORPH_GRADIENT, kernel) _, bw = cv2.threshold(grad, 0.0, 255.0, cv2.THRESH_BINARY | cv2.THRESH_OTSU) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (4, 1)) connected = cv2.morphologyEx(bw, cv2.MORPH_CLOSE, kernel) _, contours, _ = cv2.findContours(connected.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) mask = np.zeros(bw.shape, dtype=np.uint8) translator = Translator() pytesseract.pytesseract.tesseract_cmd = "C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe" for idx in range(len(contours)): x, y, w, h = cv2.boundingRect(contours[idx]) mask[y:y + h, x:x + w] = 0 cv2.drawContours(mask, contours, idx, (255, 255, 255), -1) r = float(cv2.countNonZero(mask[y:y + h, x:x + w])) / (w * h) if r > 0.45 and w > 20 and h > 10: temp = k[y:y + 3 + h, x:x + 3 + w] #_, bw = cv2.threshold(temp, 0.0, 255.0, cv2.ADAPTIVE_THRESH_GAUSSIAN_C | cv2.THRESH_OTSU) #cv2.imshow('rects', bw) #cv2.waitKey(0) text = pytesseract.image_to_string(temp, lang='eng+deu') if (text != ''): #rgb[y:y + 3 + h, x:x + 3 + w]=temp[5,5] print("The text is: ", text) file = open("test.txt", "w", encoding="utf-8") pass file.write(str(text)) file.close() c = detect_forth_tested("test.txt") if (c == "eng"): c = 'English' elif (c == "fre"): c = "French" else: c = "German" cv2.rectangle(rgb, (x - 3, y), (x + w, y + h), (0, 255, 0), 1) print('the language is: ', c) try: t = translator.translate(text, dest=destination, src=c) except: print( "Translation Error ... Check the internet connection") exit() print('the translation is: ', t.text) h1 = t.text h1 = h1.replace('\n', ' ') d = ImageDraw.Draw(rgb) reshaped_text = arabic_reshaper.reshape(t.text) bidi_text = get_display(reshaped_text) font = cv2.FONT_HERSHEY_SIMPLEX if (temp[0, 0] >= 240): rgb[y:y + 3 + h, x:x + 3 + w] = temp[0, 0] #cv2.putText(rgb, bidi_text, (x, y +13), font, 0.5, (0, 0, 0), 2, cv2.LINE_AA) d.text((x, y + 13), bidi_text, font=font, fill=(0, 255, 255, 255)) else: #cv2.putText(rgb, bidi_text, (x, y -3), font, 0.5, (255, 255, 255), 2, cv2.LINE_AA) d.text((x, y + 13), bidi_text, font=font, fill=(0, 255, 255, 255)) else: cv2.rectangle(rgb, (x, y), (x + w, y + h), (0, 0, 255), 1) if (float(time.time() - start_time) >= 60): print("%.2f minutes" % (float(time.time() - start_time) / 60)) else: print("%.2f seconds" % (time.time() - start_time)) cv2.imshow('rects', rgb) cv2.waitKey(0)
import arabic_reshaper text_to_be_reshaped = 'اللغة العربية رائعة' reshaped_text = arabic_reshaper.reshape(text_to_be_reshaped) print(reshaped_text) from arabic_reshaper import ArabicReshaper configuration = { 'delete_harakat': False, 'support_ligatures': True, 'RIAL SIGN': True, # Replace ر ي ا ل with ﷼ } reshaper = ArabicReshaper(configuration=configuration) l1='ل' l2='ا' l3='ر' l4='ي' text_to_be_reshaped = 'ب ﺭ ﻱ ﺕ' # had to split the string for display reshaped_text = reshaper.reshape(text_to_be_reshaped.replace(' ','')) print(reshaped_text)
def CleanMOJUsingSVM(data, lands, villas, save_svm_plots, save_aqar_plots, cityName): if len(villas) == 0: TransactionsTemp = [] for item in data: tempr = [] for i, ele in enumerate(item): tempr.append(ele) tempr.append(0) tempr.append(1.0) TransactionsTemp.append(tempr) return np.array(TransactionsTemp) directory = "MOHplots/" if not os.path.exists(directory + ""): os.mkdir(directory + "/") if save_svm_plots: os.mkdir(directory + "/SVM Plots/") if save_aqar_plots: os.mkdir(directory + "/Aqar Plots/") city = cityName if save_svm_plots and not os.path.exists(directory + "/SVM Plots/" + city + "/"): os.mkdir(directory + "/SVM Plots/" + city + "/") if save_aqar_plots and not os.path.exists(directory + "/Aqar Plots/" + city + "/"): os.mkdir(directory + "/Aqar Plots/" + city + "/") aqar = read_aqar_data(city, lands, villas) ranked2 = sorted([(len(values), key) for key, values in aqar.items()], reverse=True) names = {} if city in merge: for nameList in merge[city]: name = min(nameList, key=len) for n in nameList: names[n] = name for neigh in aqar: if neigh not in names: names[neigh] = neigh X = [] index = {neigh: i for i, neigh in enumerate(aqar)} for count, neigh in ranked2: for x, y, c in aqar[neigh]: if not (np.isnan(x) or np.isnan(y)): c2 = 0 if c == 'Land' else 1 X.append( [c2, x, y] + [1 if i == index[neigh] else 0 for i in range(len(aqar))]) random.shuffle(X) X_train = [x[1:] for x in X] X_label = [x[0] for x in X] clf = svm.SVC(kernel='rbf', probability=True) clf.fit(X_train, X_label) nbh = {} #with open(directory + "Data/" + city + "-Final-MOJ.csv", 'r') as f: #data = [ row.split(',') for row in f.read().splitlines() ] #MOJ data columns = {n: i for i, n in enumerate(data[0])} if "NewNeighborhood" in columns: name = columns["NewNeighborhood"] elif "OriginalName" in columns: name = columns["OriginalName"] else: name = columns["Neighborhood"] #name = 13 data2 = [[names[unicode(d[name])], float(d[9]), float(d[10]), i] for i, d in enumerate(data) if i > 0 and unicode(d[name]) in names and len(d) > 10 ] #and d[0] == '1437' ] for neigh, area, ppm, i in data2: if neigh not in nbh: nbh[neigh] = [] nbh[neigh].append([np.log(area), np.log(ppm), i]) for n in nbh: nbh[n] = [(a, p, i) for a, p, i in nbh[n] if np.isfinite(a) and np.isfinite(p)] colors = [ 'aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure', 'beige', 'bisque', 'blanchedalmond', 'blue', 'blueviolet', 'brown', 'burlywood', 'cadetblue', 'chartreuse', 'chocolate', 'coral', 'cornflowerblue', 'cornsilk', 'crimson', 'cyan', 'darkcyan', 'darkgoldenrod', 'darkkhaki', 'darkmagenta', 'darkorange', 'darkorchid', 'darkred', 'darksalmon', 'darkturquoise', 'deeppink', 'deepskyblue', 'dimgray', 'dodgerblue', 'firebrick', 'floralwhite', 'forestgreen', 'fuchsia', 'gainsboro', 'ghostwhite', 'gold', 'goldenrod', 'gray', 'green', 'greenyellow', 'honeydew', 'hotpink', 'indianred', 'khaki', 'lavender', 'lavenderblush', 'lawngreen', 'lemonchiffon', 'lightblue', 'lightcoral', 'lightcyan', 'lightgoldenrodyellow', 'lightgreen', 'lightgray', 'lightpink', 'lightsalmon', 'lightseagreen', 'lightskyblue', 'lightslategray', 'lightsteelblue', 'lightyellow', 'lime', 'limegreen', 'linen', 'magenta', 'maroon', 'mediumaquamarine', 'mediumblue', 'mediumorchid', 'mediumpurple', 'mediumseagreen', 'mediumslateblue', 'mediumspringgreen', 'mediumturquoise', 'mediumvioletred', 'mintcream', 'mistyrose', 'moccasin', 'navajowhite', 'oldlace', 'olive', 'olivedrab', 'orange', 'orangered', 'orchid', 'palegoldenrod', 'palegreen', 'paleturquoise', 'palevioletred', 'papayawhip', 'peachpuff', 'peru', 'pink', 'plum', 'powderblue', 'purple', 'red', 'rosybrown', 'royalblue', 'saddlebrown', 'salmon', 'sandybrown', 'seagreen', 'seashell', 'sienna', 'silver', 'skyblue', 'slateblue', 'slategray', 'snow', 'springgreen', 'steelblue', 'tan', 'teal', 'thistle', 'tomato', 'turquoise', 'violet', 'wheat', 'white', 'whitesmoke', 'yellow', 'yellowgreen' ] random.shuffle(colors) ranked = sorted([(len(values), key) for key, values in nbh.items()], reverse=True) for count, neigh in ranked[:]: if neigh not in aqar: continue X_train = [] for x, y, i in nbh[neigh]: X_train.append( [x, y] + [1 if i == index[neigh] else 0 for i in range(len(aqar))]) X_labels = clf.predict(X_train) X_prob = clf.predict_proba(X_train) for i, label in enumerate(X_labels): #Final_Labels.append([data[int(nbh[neigh][i][2])][7],label, max(X_prob[i]) ]) data[int(nbh[neigh][i][2])] += [label, max(X_prob[i])] if save_svm_plots: colors2 = [ 'red', 'blue', 'green', 'orange', 'purple' 'brown', 'yellow', 'lime', 'lightsteelblue', 'darksalmon', 'cyan', 'teal', 'tan', 'slategray' ] + colors labels_found = list(set([c for c in X_labels])) x, y, c = [r[0] for r in X_train], [r[1] for r in X_train], [ 'blue' if c == labels_found[0] else 'red' for c in X_labels ] plt.scatter(x, y, c=c, lw=0) prop = FontProperties("Arabic Typesetting") test = neigh.decode("utf-8") reshaped_text = arabic_reshaper.reshape(test) bidi_text = get_display(reshaped_text) plt.title('SVM Classification for ' + bidi_text + ' (' + city + ')', fontproperties=prop, fontsize=32) plt.axis('tight') #plt.show() plt.xlim(4, 10) plt.ylim(4, 10) plt.savefig(directory + "/SVM Plots/" + city + "/" + neigh.decode("utf-8") + ".pdf") plt.clf() if save_aqar_plots: for count, neigh in ranked[:]: if neigh in aqar: colors2 = [ 'red', 'blue', 'green', 'orange', 'purple' 'brown', 'yellow', 'lime', 'lightsteelblue', 'darksalmon', 'cyan', 'teal', 'tan', 'slategray' ] + colors labels_found = list(set([c for c in X_labels])) x, y, c = [r[0] for r in aqar[neigh] ], [r[1] for r in aqar[neigh]], [ 'blue' if r[2] == 'Land' else 'red' for r in aqar[neigh] ] plt.scatter(x, y, c=c, lw=0) prop = FontProperties("Arabic Typesetting") test = neigh.decode("utf-8") reshaped_text = arabic_reshaper.reshape(test) bidi_text = get_display(reshaped_text) plt.title('Aqar Distribution for ' + bidi_text + ' (' + city + ')', fontproperties=prop, fontsize=32) plt.axis('tight') #plt.show() plt.xlim(4, 10) plt.ylim(4, 10) plt.savefig(directory + "/Aqar Plots/" + city + "/" + neigh.decode("utf-8") + ".pdf") plt.clf() return data # with open(directory + "Results/"+city+" Labels.csv", "w+") as f: # for i, row in enumerate(data): # if i > 0: f.write("\n") # for j, col in enumerate(row): # f.write(col if j==0 else ","+str(col)) # if i == 0: f.write(",label") """
textNewsubOld += (' ' + item) * freqNewsubOld[word] # old (label2) - new (lable 1) freqOldsubNew = {} for word in freqold: if word in freqnew: if (freqold[word] > freqnew[word]): freqOldsubNew[word] = int((freqold[word] - freqnew[word]) * 100000) else: freqOldsubNew[word] = int(freqold[word] * 100000) for item in freqOldsubNew: textOldsubNew += (' ' + item) * freqOldsubNew[word] #creating clouds textnew = arabic_reshaper.reshape(textnew) textnew = get_display(textnew) textold = arabic_reshaper.reshape(textold) textold = get_display(textold) textNewsubOld = arabic_reshaper.reshape(textNewsubOld) textNewsubOld = get_display(textNewsubOld) textOldsubNew = arabic_reshaper.reshape(textOldsubNew) textOldsubNew = get_display(textOldsubNew) WordCloudnew = WordCloud(font_path='WordCloud/src/BNazanin.ttf', background_color='white', height=2000, width=2000) WordCloudnew.generate(textnew) plt.imshow(WordCloudnew) plt.axis("off")
- bidi.algorithm - arabic_reshaper Dependencies installation: pip install python-bidi arabic_reshape """ import os import codecs from textcloud import TextCloud import arabic_reshaper from bidi.algorithm import get_display # get data directory (using getcwd() is needed to support running example in generated IPython notebook) d = os.path.dirname(__file__) if "__file__" in locals() else os.getcwd() # Read the whole text. f = codecs.open(os.path.join(d, 'arabicwords.txt'), 'r', 'utf-8') # Make text readable for a non-Arabic library like wordcloud text = arabic_reshaper.reshape(f.read()) text = get_display(text) # Generate a word cloud image wordcloud = TextCloud( font_path='fonts/NotoNaskhArabic/NotoNaskhArabic-Regular.ttf').generate( text) # Export to an image wordcloud.to_file("arabic_example.png")
def word_process(self, x): reshaped_text = arabic_reshaper.reshape(x) return get_display(reshaped_text)
def arabic(str): reshaped_text = arabic_reshaper.reshape(str) return get_display(reshaped_text)
def myFirstPage(canvas, doc): canvas.saveState() canvas.setFont('Arabic-3', 10) # canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-108, Title) # canvas.drawString(PAGE_WIDTH, 1*mm, "First Page / %s" % pageinfo) arabic_text = get_display( arabic_reshaper.reshape(u'طبقا لمقتضيات الظهير الشريف')) canvas.drawCentredString(PAGE_WIDTH - 320, PAGE_HEIGHT - 15, arabic_text) arabic_text = get_display( arabic_reshaper.reshape(u'الصادر في 03 اكتوبر 2002')) canvas.drawCentredString(PAGE_WIDTH - 320, PAGE_HEIGHT - 30, arabic_text) arabic_text = get_display(arabic_reshaper.reshape(u'المملكة المغربية')) canvas.drawRightString(PAGE_WIDTH - 70, PAGE_HEIGHT - 15, arabic_text) arabic_text = get_display( arabic_reshaper.reshape( u'وزارة الداخلية ' )) canvas.drawRightString(PAGE_WIDTH - 70, PAGE_HEIGHT - 30, arabic_text) arabic_text = get_display(arabic_reshaper.reshape(u'عمالة او اقليم:')) canvas.drawRightString(PAGE_WIDTH - 30, PAGE_HEIGHT - 60, "field 1 " + arabic_text) arabic_text = get_display(arabic_reshaper.reshape(u'جماعة:')) canvas.drawRightString(PAGE_WIDTH - 30, PAGE_HEIGHT - 75, "field 2 " + arabic_text) arabic_text = get_display(arabic_reshaper.reshape(u'مكتب الحالة المدنية:')) canvas.drawRightString(PAGE_WIDTH - 30, PAGE_HEIGHT - 90, "field 3 " + arabic_text) arabic_text = get_display(arabic_reshaper.reshape(u'رسم رقم:')) canvas.drawRightString(PAGE_WIDTH - 30, PAGE_HEIGHT - 105, "field 4 " + arabic_text) arabic_text = get_display(arabic_reshaper.reshape(u'لسنة:')) canvas.drawRightString(PAGE_WIDTH - 30, PAGE_HEIGHT - 120, "field 5 " + arabic_text) arabic_text = get_display(arabic_reshaper.reshape(u'هجرية')) canvas.drawRightString(PAGE_WIDTH - 130, PAGE_HEIGHT - 120, arabic_text) arabic_text = get_display(arabic_reshaper.reshape(u' ')) canvas.drawRightString(PAGE_WIDTH - 30, PAGE_HEIGHT - 135, "field 6 " + arabic_text) arabic_text = get_display(arabic_reshaper.reshape(u'ميلادية')) canvas.drawRightString(PAGE_WIDTH - 130, PAGE_HEIGHT - 135, arabic_text) canvas.setFont('Arabic-2', 36) arabic_text = get_display( arabic_reshaper.reshape(u'نسخة موجزة من رسم الولادة')) canvas.drawCentredString(PAGE_WIDTH / 2.0, PAGE_HEIGHT - 165, arabic_text) canvas.setFont('Arabic-3', 10) arabic_text = get_display(arabic_reshaper.reshape(u'الاسم الشخصي:')) canvas.drawRightString(PAGE_WIDTH - 30, PAGE_HEIGHT - 190, "field 7 " + arabic_text) canvas.drawString(PAGE_WIDTH - 380, PAGE_HEIGHT - 190, "Prénom: " + "field 7 bis") arabic_text = get_display(arabic_reshaper.reshape(u'الاسم العائلي:')) canvas.drawRightString(PAGE_WIDTH - 30, PAGE_HEIGHT - 210, "field 8 " + arabic_text) canvas.drawString(PAGE_WIDTH - 380, PAGE_HEIGHT - 210, "Nom: " + "field 8 bis") canvas.restoreState()
def visualization(tweets,y_predicted,noOfSearchTerms): # Create and generate a word cloud image: words_in_tweet_strings = " " weridPatterns = re.compile("[" u"\U0001F600-\U0001F64F" # emoticons u"\U0001F300-\U0001F5FF" # symbols & pictographs u"\U0001F680-\U0001F6FF" # transport & map symbols u"\U0001F1E0-\U0001F1FF" # flags (iOS) u"\U00002702-\U000027B0" u"\U000024C2-\U0001F251" u"\U0001f926-\U0001f937" u'\U00010000-\U0010ffff' u"\u200d" u"\u2640-\u2642" u"\u2600-\u2B55" u"\u23cf" u"\u23e9" u"\u231a" u"\u3030" u"\ufe0f" u"\u2069" u"\u2066" u"\u200c" u"\u2068" u"\u2067" "]+", flags=re.UNICODE) for tweet in tweets: words_in_tweet_strings += tweet.full_text clean_text = weridPatterns.sub(r'', words_in_tweet_strings) data = arabic_reshaper.reshape(clean_text) data = get_display(data) # add this line wordcloud = WordCloud(font_path='arial', background_color='white', mode='RGB', width=2000, height=1000).generate(data) # Display the generated image: #fig1 = plt.figure(1) plt.subplot(2,1,1) plt.imshow(wordcloud, interpolation='bilinear') plt.axis("off") #plt.show() #fig1.savefig('words.png') ####### Visualization n_all = len(y_predicted) n_pos = 0 n_neg = 0 for y in y_predicted: if y == 'pos': n_pos += 1 if y == 'neg': n_neg += 1 positive = percentage(n_pos, n_all) negative = percentage(n_neg, n_all) positive = format(positive,'.2f') negative = format(negative, '.2f') labels = ['Positive [' + str(positive) + '%]', 'Negative [' + str(negative) + '%]'] sizes = [positive, negative] colors = ['yellowgreen', 'red'] #fig2 = plt.figure(2) plt.subplot(2, 1, 2) patches, texts = plt.pie(sizes, colors=colors, startangle=90) plt.legend(patches, labels, loc="best") plt.title(" by analyzing " + str(noOfSearchTerms) + " Tweets.") plt.axis('equal') plt.tight_layout() plt.show()
def _generate_mit_pe_certificate(self, student_name, download_dir, verify_dir, filename='Certificate.pdf'): """ Generate the BigDataX certs """ # 8.5x11 page size 279.4mm x 215.9mm WIDTH = 279 # width in mm (8.5x11) HEIGHT = 216 # height in mm (8.5x11) download_uuid = uuid.uuid4().hex verify_uuid = uuid.uuid4().hex download_url = "{base_url}/{cert}/{uuid}/{file}".format( base_url=settings.CERT_DOWNLOAD_URL, cert=S3_CERT_PATH, uuid=download_uuid, file=filename ) filename = os.path.join(download_dir, download_uuid, filename) # This file is overlaid on the template certificate overlay_pdf_buffer = StringIO.StringIO() c = canvas.Canvas(overlay_pdf_buffer) c.setPageSize((WIDTH * mm, HEIGHT * mm)) # register all fonts in the fonts/ dir, # there are more fonts in here than we need # but the performance hit seems minimal for font_file in glob('{0}/fonts/*.ttf'.format(self.template_dir)): font_name = os.path.basename(os.path.splitext(font_file)[0]) pdfmetrics.registerFont(TTFont(font_name, font_file)) #### STYLE: grid/layout LEFT_INDENT = 10 # mm from the left side to write the text MAX_WIDTH = 260 # maximum width on the content in the cert, used for wrapping #### STYLE: template-wide typography settings style_type_name_size = 36 style_type_name_leading = 53 style_type_name_med_size = 22 style_type_name_med_leading = 27 style_type_name_small_size = 18 style_type_name_small_leading = 21 #### STYLE: template-wide color settings style_color_name = colors.Color(0.000000, 0.000000, 0.000000) #### STYLE: positioning pos_name_y = 137 pos_name_med_y = 142 pos_name_small_y = 140 pos_name_no_wrap_offset_y = 2 #### HTML Parser #### # Since the final string is HTML in a PDF we need to un-escape the html # when calculating the string width. html = HTMLParser() ####### ELEM: Student Name # default is to use Garamond for the name, # will fall back to Arial if there are # unusual characters y_offset_name = pos_name_y y_offset_name_med = pos_name_med_y y_offset_name_small = pos_name_small_y styleUnicode = ParagraphStyle(name="arial", leading=10, fontName='Arial Unicode') styleGaramondStudentName = ParagraphStyle(name="garamond", fontName='Garamond-Bold') styleGaramondStudentName.leading = style_type_name_small_size style = styleGaramondStudentName html_student_name = html.unescape(student_name) larger_width = stringWidth(html_student_name.decode('utf-8'), 'Garamond-Bold', style_type_name_size) / mm smaller_width = stringWidth(html_student_name.decode('utf-8'), 'Garamond-Bold', style_type_name_small_size) / mm paragraph_string = arabic_reshaper.reshape(student_name.decode('utf-8')) paragraph_string = get_display(paragraph_string) # Garamond only supports Latin-1 # if we can't use it, use Gentium if self._use_unicode_font(student_name): style = styleUnicode larger_width = stringWidth(html_student_name.decode('utf-8'), 'Arial Unicode', style_type_name_size) / mm # if the name is too long, shrink the font size if larger_width < MAX_WIDTH: style.fontSize = style_type_name_size style.leading = style_type_name_leading y_offset = y_offset_name elif smaller_width < MAX_WIDTH: y_offset = y_offset_name_med + pos_name_no_wrap_offset_y style.fontSize = style_type_name_med_size style.leading = style_type_name_med_leading else: y_offset = y_offset_name_small style.fontSize = style_type_name_small_size style.leading = style_type_name_small_leading style.textColor = style_color_name style.alignment = TA_CENTER paragraph = Paragraph(paragraph_string, style) paragraph.wrapOn(c, MAX_WIDTH * mm, HEIGHT * mm) paragraph.drawOn(c, LEFT_INDENT * mm, y_offset * mm) ## Generate the final PDF c.showPage() c.save() # Merge the overlay with the template, then write it to file output = PdfFileWriter() overlay = PdfFileReader(overlay_pdf_buffer) # We need a page to overlay on. # So that we don't have to open the template # several times, we open a blank pdf several times instead # (much faster) blank_pdf = PdfFileReader( file("{0}/blank-letter.pdf".format(self.template_dir), "rb") ) final_certificate = blank_pdf.getPage(0) final_certificate.mergePage(self.template_pdf.getPage(0)) final_certificate.mergePage(overlay.getPage(0)) output.addPage(final_certificate) self._ensure_dir(filename) outputStream = file(filename, "wb") output.write(outputStream) outputStream.close() return (download_uuid, verify_uuid, download_url)
import arabic_reshaper import sys file_path = sys.argv[1] with open(file_path, "r") as myfile: text = ''.join(myfile.readlines()) text_to_be_reshaped = 'اللغة العربية رائعة' reshaped_text = arabic_reshaper.reshape(text) output_file = open(file_path, "w") output_file.write(reshaped_text) output_file.close()
def reshaper(self, text): reshaped_text = arabic_reshaper.reshape(text) return get_display(reshaped_text)
image_area = height * width serial_width, serial_height, serial_font_size = generate_random_coordinates( height, width, start_point) if serial_width == False: index = index - 1 continue serial_width = serial_width * 0.7 end_point = (round(start_point[0] + serial_width), round(start_point[1] - serial_height)) serial_area = serial_height * serial_width font = cv2.FONT_HERSHEY_SIMPLEX # print(serial_height , " " , serial_width) print("Image Num #", str(index), "start at :", start_point) original_text = get_shuffle_serial(7) text = original_text reshaped_text = arabic_reshaper.reshape(text) # correct its shape text = get_display(reshaped_text) # cv2.putText(image,text,start_point, font, serial_font_size,(0,0,0),random.randint(0,3),cv2.LINE_AA) # cv2.rectangle(image,start_point,(round(start_point[0]+serial_width),round(start_point[1]-serial_height)),(0,255,0),0) # cv2.circle(image,start_point,2, (0,0,255), -1) # cv2.circle(image,end_point, 2, (0,0,255), -1) # cv2.imwrite("test.png",image) fontpath = "Mirza/Mirza-Regular.ttf" font = ImageFont.truetype(fontpath, round(serial_height * 1.7)) img_pil = Image.fromarray(image) draw = ImageDraw.Draw(img_pil) d = (start_point[0], start_point[1] - serial_height) draw.text(d, text, font=font, fill=(0, 0, 0)) # draw.rectangle((start_point,end_point)) image = np.array(img_pil) cv2.imwrite("dataset/" + str(index) + ".png", image)
def to_arabic(self, text): a = get_display(arabic_reshaper.reshape(str(text))) return(a)
def main(): reshaped_text = arabic_reshaper.reshape(sys.argv[1].decode('utf-8')) print reshaped_text.encode('utf-8')
def do_backspace(self, from_undo=True, mode='bkspc'): self.str = self.str[0:len(self.str)-1] self.text = get_display(arabic_reshaper.reshape(self.str))
def reportP(pid, dates, is_true): import datetime import pandas as pd import numpy as np import firebase_admin from firebase_admin import credentials from firebase_admin import firestore from firebase_admin import storage import pyrebase from datetime import date, timedelta import urllib.request, json import time from matplotlib import pyplot as plt import matplotlib.dates as mdates import os import csv from IPython.display import display from Model import trainData import random from matplotlib.patches import Ellipse import matplotlib.patches as mpatches import seaborn as sns # signal processing from scipy import signal from scipy.ndimage import label from scipy.stats import zscore from scipy.interpolate import interp1d from scipy.integrate import trapz # misc import warnings #generate pdf from reportlab.pdfgen import canvas from reportlab.lib.colors import Color, lightblue, black # In[2]: if not firebase_admin._apps: cred = credentials.Certificate("serene-firebase-adminsdk.json") app = firebase_admin.initialize_app( cred, { 'storageBucket': 'serene-2dfd6.appspot.com', }, name='[DEFAULT]') else: app = firebase_admin.get_app() db = firestore.client() # In[3]: userID = pid GoogleCalendar = is_true # In[4]: today = datetime.datetime.now() timestamp = today.strftime("%Y-%m-%d %H:%M:%S") bucket = storage.bucket(app=app) # ## Get data from storage and get list of dates # In[5]: df = pd.DataFrame() notAvailableDates = [] # loop through the storage and get the data sleep = [] for x in range(0, len(dates)): #Sleep blob = bucket.blob(userID + "/fitbitData/" + dates[x] + "/" + dates[x] + "-sleep.json") #download the file u = blob.generate_signed_url(datetime.timedelta(seconds=300), method='GET') try: with urllib.request.urlopen(u) as url: data = json.loads(url.read().decode()) sleepMinutes = data['summary']["totalMinutesAsleep"] except: notAvailableDates.append(dates[x]) pass #Activity (Steps) blob = bucket.blob(userID + "/fitbitData/" + dates[x] + "/" + dates[x] + "-activity.json") #download the file u = blob.generate_signed_url(datetime.timedelta(seconds=300), method='GET') try: with urllib.request.urlopen(u) as url: data = json.loads(url.read().decode()) steps = data['summary']["steps"] except: notAvailableDates.append(dates[x]) pass #heartrate blob = bucket.blob(userID + "/fitbitData/" + dates[x] + "/" + dates[x] + "-heartrate.json") u = blob.generate_signed_url(datetime.timedelta(seconds=300), method='GET') try: with urllib.request.urlopen(u) as url: data = json.loads(url.read().decode()) df_heartrate = pd.DataFrame( data['activities-heart-intraday']['dataset']) df_heartrate.time.apply(str) df_heartrate['time'] = pd.to_datetime(df_heartrate['time']) df_heartrate['hour'] = df_heartrate['time'].apply( lambda time: time.strftime('%H')) df_heartrate.drop(['time'], axis=1, inplace=True) heart_rate = df_heartrate.groupby(["hour"], as_index=False).mean() heart_rate['sleepMin'] = sleepMinutes heart_rate['TotalSteps'] = steps heart_rate['date'] = dates[x] heart_rate = heart_rate.astype({"hour": int}) except: notAvailableDates.append(dates[x]) pass #append dataframe df = df.append(heart_rate, ignore_index=True) notAvailableDates notSyncedDates = pd.DataFrame() notSyncedDates['date'] = notAvailableDates # In[16]: notSyncedDates = notSyncedDates.drop_duplicates() # ### Get user location # In[17]: # get location from database loc_df = pd.DataFrame() locID = [] locations = db.collection(u'PatientLocations').where( u'patientID', u'==', userID).stream() for location in locations: loc = location.to_dict() locID.append(location.id) loc_df = loc_df.append(pd.DataFrame(loc, index=[0]), ignore_index=True) loc_df['id'] = locID # In[18]: loc_df.drop(['anxietyLevel', 'lat', 'lng', 'patientID'], axis=1, inplace=True) # In[19]: loc_df.time.apply(str) loc_df['time'] = pd.to_datetime(loc_df['time']) loc_df['date'] = pd.to_datetime(loc_df['time'], format='%Y:%M:%D').dt.date loc_df['hour'] = loc_df['time'].apply(lambda time: time.strftime('%H')) loc_df.drop(['time'], axis=1, inplace=True) loc_df.hour = loc_df.hour.astype(int) loc_df.date = loc_df.date.astype(str) df.date = df.date.astype(str) # In[20]: dfinal = pd.merge(left=df, right=loc_df, how='left', left_on=['hour', 'date'], right_on=['hour', 'date']).ffill() # ### Test data into model # In[23]: #test model train_df = dfinal.rename(columns={'value': 'Heartrate'}) # In[24]: Labeled_df = pd.DataFrame() Labeled_df = trainData(train_df) # In[25]: Labeled_df.drop(['lon'], axis=1, inplace=True) # In[26]: # Replace missing values because it doesn't exist Labeled_df['name'].fillna("Not given", inplace=True) Labeled_df['id'].fillna("Not given", inplace=True) #Labeled_df['anxiety_assigned'].fillna('Not given', inplace = True) # In[27]: # In[28]: # Update firebase with the user anxiety level for row in Labeled_df.itertuples(): if row.id != 'Not given': if row.Label == 'Low' or row.Label == 'LowA': anxietyLevel = '1' elif row.Label == 'Meduim': anxietyLevel = '2' else: anxietyLevel = '3' if ((row.anxiety_assigned == False) or (row.anxiety_assigned == 'Not given')): doc_ref = db.collection(u'PatientLocations').document(row.id) doc_ref.update({ u'anxietyLevel': anxietyLevel, u'anxiety_assigned': True }) # ### Show the places with highest anxiety level # In[29]: # Show the highest level df_high = pd.DataFrame() df_high = Labeled_df[Labeled_df.Label == 'High'] # # Recommendation # In[30]: docDf = pd.DataFrame() doc_ref = db.collection(u'Patient').document(userID) doc = doc_ref.get().to_dict() docDf = docDf.append(pd.DataFrame(doc, index=[0]), ignore_index=True) # In[31]: age1 = docDf['age'].values name1 = docDf['name'].values emp1 = docDf['employmentStatus'].values mar1 = docDf['maritalStatus'].values income1 = docDf['monthlyIncome'].values chronicD1 = docDf['chronicDiseases'].values smoke1 = docDf['smokeCigarettes'].values gad1 = docDf['GAD-7ScaleScore'].values gender1 = docDf['gender'].values age = age1[0] name = name1[0] emp = emp1[0] mar = mar1[0] income = income1[0] chronicD = chronicD1[0] smoke = smoke1[0] gad = gad1[0] gender = gender1[0] compareAge = int(age) # In[32]: sleepMin = Labeled_df['sleepMin'].mean() totalSteps = Labeled_df['TotalSteps'].astype(float).mean() sleepRecomendation = False stepsRecomendation = False recomendedSteps = 'No recomendation' if sleepMin < 360: sleepRecomendation = True if compareAge < 20 and compareAge > 11: if totalSteps < 6000: stepsRecomendation = True recomendedSteps = '6000' if compareAge < 66 and compareAge > 19: if totalSteps < 3000: stepsRecomendation = True recomendedSteps = '3000' sleepMin = sleepMin / 60 sleepMin = float("{:.1f}".format(sleepMin)) totalSteps = int(totalSteps) # ## Storage intilization # In[33]: firebaseConfig = { "apiKey": "AIzaSyBoxoXwFm9TuFysjQYag0GB1NEPyBINlTU", "authDomain": "serene-2dfd6.firebaseapp.com", "databaseURL": "https://serene-2dfd6.firebaseio.com", "projectId": "serene-2dfd6", "storageBucket": "serene-2dfd6.appspot.com", "messagingSenderId": "461213981433", "appId": "1:461213981433:web:62428e3664182b3e58e028", "measurementId": "G-J66VP2Y3CR" } firebase = pyrebase.initialize_app(firebaseConfig) storage = firebase.storage() # # AL # In[34]: sns.set(rc={'axes.facecolor': '#fcfeff'}) # In[35]: # Change Label values to num, to represent them in a barchart nums = [] for row in Labeled_df.itertuples(): if row.Label == 'Low' or row.Label == 'LowA': nums.append(1) elif row.Label == 'Meduim': nums.append(2) else: nums.append(3) Labeled_df['numLabel'] = nums # In[36]: # Get anxiety level by day and store it in a new data frame plot_df = pd.DataFrame() avgAnxiety = [] totalAnxiety = 0 rowCount = 1 for x in range(0, len(dates)): totalAnxiety = 0 rowCount = 1 for row in Labeled_df.itertuples(): if (row.date == dates[x]): rowCount += 1 totalAnxiety += row.numLabel avgAnxiety.append(totalAnxiety / rowCount) plot_df['date'] = dates plot_df['Anxiety'] = avgAnxiety # In[37]: #divide dataframe into 15 rows (2 weeks) max is 3 months df1 = pd.DataFrame() df2 = pd.DataFrame() df3 = pd.DataFrame() df4 = pd.DataFrame() df5 = pd.DataFrame() df6 = pd.DataFrame() dfarray = [] count = 0 if (len(plot_df) > 15): df1 = plot_df[:15] df2 = plot_df[15:] dfarray.append(df1) dfarray.append(df2) if (len(df2) > 15): count = (df2.last_valid_index() - (len(df2) - 15)) df3 = df2[count:] dfarray.append(df3) if (len(df3) > 15): count = (df3.last_valid_index() - (len(df3) - 15)) df4 = df3[count:] dfarray.append(df4) if (len(df4) > 15): count = (df4.last_valid_index() - (len(df4) - 15)) df5 = df4[count:] dfarray.append(df5) if (len(df5) > 15): count = (df5.last_valid_index() - (len(df5) - 15)) df6 = df5[count:] dfarray.append(df6) # In[38]: # Plot AL if (len(plot_df) <= 15): fig, ax = plt.subplots() c1 = '#9dd6f5' c2 = '#4ba0d1' c3 = '#23495f' bar_width = 0.25 for t, y in zip(plot_df["date"], plot_df["Anxiety"]): c = "" if (y <= 1): c = c1 elif (1 < y <= 2): c = c2 elif (y > 2): c = c3 plt.bar([t, t], [0, y], bar_width, color=c) colors = [[c1, c1], [c2, c2], [c3, c3]] categories = ['Low', 'Meduim', 'High'] #create dict legend_dict = dict(zip(categories, colors)) #create patches patchList = [] for key in legend_dict: data_key = mpatches.Patch(facecolor=legend_dict[key][0], edgecolor=legend_dict[key][1], label=key) patchList.append(data_key) ax.legend(handles=patchList, ncol=len(categories), fontsize=12) plt.tick_params(axis='x', rotation=70) # Start the graph at 0 ax.set_ylim(0, 3) #fig.set_size_inches(15.5, 10) plt.tight_layout() plt.xlabel('Date') ax.yaxis.set_label_coords(-0.02, 0.48) fig.savefig('AL0.png', dpi=None) imagePath = 'AL0.png' link = storage.child( userID + "/lastGeneratedPatientReport/AL0.png").put('AL0.png') os.remove('AL0.png') else: links = [] for x in range(0, len(dfarray)): fig, ax = plt.subplots() c1 = '#9dd6f5' c2 = '#4ba0d1' c3 = '#23495f' bar_width = 0.25 for t, y in zip(dfarray[x]["date"], dfarray[x]["Anxiety"]): c = "" if (y <= 1): c = c1 elif (1 < y <= 2): c = c2 elif (y > 2): c = c3 plt.bar([t, t], [0, y], bar_width, color=c) colors = [[c1, c1], [c2, c2], [c3, c3]] categories = ['Low', 'Meduim', 'High'] #create dict legend_dict = dict(zip(categories, colors)) #create patches patchList = [] for key in legend_dict: data_key = mpatches.Patch(facecolor=legend_dict[key][0], edgecolor=legend_dict[key][1], label=key) patchList.append(data_key) ax.legend(handles=patchList, ncol=len(categories), fontsize=12) plt.tick_params(axis='x', rotation=70) # Start the graph at 0 ax.set_ylim(0, 3) #fig.set_size_inches(15.5, 10) plt.tight_layout() plt.xlabel('Date') ax.yaxis.set_label_coords(-0.02, 0.48) fig.savefig('AL' + str(x) + '.png', dpi=None) imagePath = 'AL' + str(x) + '.png' link = storage.child(userID + "/lastGeneratedPatientReport/AL" + str(x) + '.png').put('AL' + str(x) + '.png') links.append(link) os.remove('AL' + str(x) + '.png') # ### save file to database first # In[39]: if (len(plot_df) <= 15): linkDF = pd.DataFrame() linkDF = linkDF.append(pd.DataFrame(link, index=[0]), ignore_index=True) token1 = linkDF['downloadTokens'].values token = token1[0] AlLink = storage.child( userID + "/lastGeneratedPatientReport/AL0.png").get_url(token) doc_rec = db.collection(u'LastGeneratePatientReport').document( 'report' + userID) doc_rec.set({ u'average_steps': totalSteps, u'patient_id': userID, u'average_sleep_hours': sleepMin, u'sleepRecomendation': sleepRecomendation, u'stepsRecomendation': stepsRecomendation, u'recommended_steps': recomendedSteps, u'number_of_AL_graphs': 1, u'AL_graph_0': AlLink, u'recommended_sleep_hours': '7-9' }) else: firebase_links = [] for x in range(0, len(links)): linkDF = pd.DataFrame() linkDF = linkDF.append(pd.DataFrame(link, index=[0]), ignore_index=True) token1 = linkDF['downloadTokens'].values token = token1[0] AlLink = storage.child(userID + "/lastGeneratedPatientReport/AL" + str(x) + '.png').get_url(token) firebase_links.append(AlLink) doc_rec = db.collection(u'LastGeneratePatientReport').document( 'report' + userID) doc_rec.set({ u'average_steps': totalSteps, u'patient_id': userID, u'average_sleep_hours': sleepMin, u'sleepRecomendation': sleepRecomendation, u'stepsRecomendation': stepsRecomendation, u'recommended_steps': recomendedSteps, u'number_of_AL_graphs': len(links), u'recommended_sleep_hours': '7-9' }) for x in range(0, len(links)): doc_rec = db.collection(u'LastGeneratePatientReport').document( 'report' + userID) doc_rec.update({u'AL_graph_' + str(x): firebase_links[x]}) # ## To generate graphs for PDF report # In[40]: fig, ax = plt.subplots() c1 = '#9dd6f5' c2 = '#4ba0d1' c3 = '#23495f' bar_width = 0.25 for t, y in zip(plot_df["date"], plot_df["Anxiety"]): c = "" if (y <= 1): c = c1 elif (1 < y <= 2): c = c2 elif (y > 2): c = c3 plt.bar([t, t], [0, y], bar_width, color=c) colors = [[c1, c1], [c2, c2], [c3, c3]] categories = ['Low', 'Meduim', 'High'] #create dict legend_dict = dict(zip(categories, colors)) #create patches patchList = [] for key in legend_dict: data_key = mpatches.Patch(facecolor=legend_dict[key][0], edgecolor=legend_dict[key][1], label=key) patchList.append(data_key) ax.legend(handles=patchList, ncol=len(categories), fontsize=12) plt.tick_params(axis='x', rotation=70) # Start the graph at 0 ax.set_ylim(0, 3) fig.set_size_inches(15.5, 10) plt.tight_layout() plt.xlabel('Date') ax.yaxis.set_label_coords(-0.02, 0.48) fig.savefig('ALpdf.png', dpi=None) # # Location Analysis # In[41]: # get location from database new_loc = pd.DataFrame() locID = [] locations = db.collection(u'PatientLocations').where( u'patientID', u'==', userID).stream() for location in locations: loc = location.to_dict() locID.append(location.id) new_loc = new_loc.append(pd.DataFrame(loc, index=[0]), ignore_index=True) new_loc['id'] = locID # In[42]: new_loc.time.apply(str) new_loc['time'] = pd.to_datetime(new_loc['time']) new_loc['date'] = pd.to_datetime(new_loc['time'], format='%Y:%M:%D').dt.date new_loc.drop(['time'], axis=1, inplace=True) new_loc.date = new_loc.date.astype(str) # In[43]: new_loc = new_loc[(new_loc.date >= dates[0]) & (new_loc.date <= dates[len(dates) - 1])] # In[44]: names = [] Name = "" for row in new_loc.itertuples(): Name = row.nearestLoc names.append(Name) # In[45]: new_name = pd.DataFrame() new_name['name'] = names # In[46]: new_name = new_name.drop_duplicates() new_name.dropna() # In[47]: fnames = [] fName = "" for row in new_name.itertuples(): fName = row.name fnames.append(fName) # In[48]: analysis_EN = pd.DataFrame() analysis_AR = pd.DataFrame() count = 0 i = 0 #label = "" locationName = "" near = '' nearLocs = [] counts = [] labels = [] locationNames = [] for x in range(0, len(fnames)): count = 0 locName = fnames[i] for row in new_loc.itertuples(): if (locName == row.nearestLoc): if (row.anxietyLevel == '3'): count += 1 label = row.anxietyLevel locationName = row.name near = row.nearestLoc i += 1 counts.append(count) #labels.append(label) locationNames.append(locationName) nearLocs.append(near) analysis_EN['Location'] = locationNames analysis_EN['Number of occurrences'] = counts #analysis_EN ['Anxiety Level'] = labels analysis_EN['Nearest Location'] = nearLocs analysis_AR['الموقع'] = locationNames analysis_AR['عدد مرات الزيارة'] = counts #analysis_AR ['مستوى القلق'] = labels analysis_AR['أقرب موقع'] = nearLocs # In[49]: newEn = analysis_EN.drop( analysis_EN[analysis_EN['Number of occurrences'] == 0].index, inplace=True) newAr = analysis_AR.drop( analysis_AR[analysis_AR['عدد مرات الزيارة'] == 0].index, inplace=True) # In[50]: #analysis_EN ['Anxiety Level'] = 'High' #analysis_AR ['مستوى القلق'] = 'مرتفع' # In[51]: import six import arabic_reshaper from googletrans import Translator from bidi.algorithm import get_display from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont # In[52]: def render_mpl_table(data, col_width=5.0, row_height=0.625, font_size=14, tran='', header_color='#23495f', row_colors=['#e1eff7', 'w'], edge_color='#23495f', bbox=[0, 0, 1, 1], header_columns=0, ax=None, **kwargs): if ax is None: size = (np.array(data.shape[::-1]) + np.array([0, 1])) * np.array( [col_width, row_height]) fig, ax = plt.subplots(figsize=size) ax.axis('off') mpl_table = ax.table(cellText=data.values, bbox=bbox, colLabels=data.columns, cellLoc='center', **kwargs) mpl_table.auto_set_font_size(False) mpl_table.set_fontsize(font_size) for k, cell in six.iteritems(mpl_table._cells): cell.set_edgecolor(edge_color) if k[0] == 0 or k[1] < header_columns: cell.set_text_props(weight='bold', color='w') cell.set_facecolor(header_color) else: cell.set_facecolor(row_colors[k[0] % len(row_colors)]) cell.alignment = 'center' fig.savefig(tran + '.png', dpi=100) return ax # In[53]: if (len(analysis_EN) > 0): for ind, row in analysis_EN.iterrows(): analysis_EN.loc[ind, 'Nearest Location'] = get_display( arabic_reshaper.reshape(analysis_EN.loc[ind, 'Nearest Location'])) if (len(analysis_AR) > 0): for ind, row in analysis_AR.iterrows(): analysis_AR.loc[ind, 'أقرب موقع'] = get_display( arabic_reshaper.reshape(analysis_AR.loc[ind, 'أقرب موقع'])) # analysis_AR.loc[ind,'مستوى القلق']=get_display(arabic_reshaper.reshape(analysis_AR.loc[ind,'مستوى القلق'])) analysis_AR = analysis_AR.rename( columns={'الموقع': get_display(arabic_reshaper.reshape('الموقع'))}) analysis_AR = analysis_AR.rename( columns={ 'التكرار': get_display(arabic_reshaper.reshape('التكرار')) }) #analysis_AR = analysis_AR.rename(columns={'مستوى القلق': get_display(arabic_reshaper.reshape('مستوى القلق')) }) analysis_AR = analysis_AR.rename( columns={ 'أقرب موقع': get_display(arabic_reshaper.reshape('أقرب موقع')) }) # In[54]: if (len(analysis_EN) > 0): render_mpl_table(analysis_EN, header_columns=0, col_width=6, tran='Location-EN') if (len(analysis_AR) > 0): render_mpl_table(analysis_AR, header_columns=0, col_width=6, tran='Location-AR') # # Events with highest level of anxiety # In[59]: from dateutil.parser import parse # In[60]: if GoogleCalendar == 'true': events_df = pd.DataFrame() finalEvents_EN = pd.DataFrame() finalEvents_AR = pd.DataFrame() eventID = [] events = db.collection(u'PatientEvents').where(u'patientID', u'==', userID).stream() for event in events: ev = event.to_dict() eventID.append(event.id) events_df = events_df.append(pd.DataFrame(ev, index=[0]), ignore_index=True) events_df['idEvent'] = eventID # In[61]: if len(events_df) > 0: newDates = [] for row in events_df.itertuples(): newDate = parse(row.date) newDates.append(newDate) events_df['newDate'] = newDates events_df = events_df.rename(columns={'name': 'eventName'}) events_df.newDate.apply(str) events_df['date'] = pd.to_datetime(events_df['newDate'], format='%Y:%M:%D').dt.date events_df['hour'] = events_df['newDate'].apply( lambda time: time.strftime('%H')) events_df.hour = events_df.hour.astype(int) events_df.date = events_df.date.astype(str) Test = Labeled_df merge_df = pd.merge(left=Test, right=events_df, how='left', left_on=['hour', 'date'], right_on=['hour', 'date']).ffill() merge_df['eventName'].fillna("Not given", inplace=True) merge_df = merge_df[merge_df.eventName != 'Not given'] #finalEvents_EN = pd.DataFrame() #finalEvents_AR = pd.DataFrame() ev_name = '' evNames = [] #evLabels = [] evDate = [] for row in merge_df.itertuples(): if row.eventName != ev_name: if row.Label == 'High': ev_name = row.eventName #ev_label = row.Label ev_date = row.date if (ev_name != ''): evNames.append(ev_name) #evLabels.append(ev_label) evDate.append(ev_date) finalEvents_EN['Event Name'] = evNames #finalEvents_EN['Anxiety Level'] = evLabels finalEvents_EN['Date'] = evDate finalEvents_AR['اسم الحدث'] = evNames #finalEvents_AR['مستوى القلق'] = evLabels finalEvents_AR['تاريخ الحدث'] = evDate finalEvents_EN = finalEvents_EN.drop_duplicates() finalEvents_AR = finalEvents_AR.drop_duplicates() Final_duplicated = pd.DataFrame() eventId = '' eventIds = [] event_Labels = [] event_names = [] hours = [] for row in merge_df.itertuples(): if eventId != row.idEvent: eventId = row.idEvent if eventId != '': eventIds.append(eventId) event_Labels.append(row.Label) event_names.append(row.eventName) hours.append(row.hour) Final_duplicated['id'] = eventIds Final_duplicated['Label'] = event_Labels Final_duplicated['name'] = event_names Final_duplicated['hour'] = hours Final_duplicated = Final_duplicated.drop_duplicates(subset='id') # Update firebase with the user anxiety level for row in Final_duplicated.itertuples(): if row.Label == 'High': doc_ref = db.collection(u'PatientEvents').document(row.id) doc_ref.update({u'anxietyLevel': '3'}) else: doc_ref = db.collection(u'PatientEvents').document(row.id) doc_ref.delete() checkEvents_df = pd.DataFrame() cEventsID = [] checkEvents = db.collection(u'PatientEvents').where( u'patientID', u'==', userID).stream() for event in checkEvents: cEv = event.to_dict() cEventsID.append(event.id) checkEvents_df = checkEvents_df.append(pd.DataFrame(cEv, index=[0]), ignore_index=True) checkEvents_df['id'] = cEventsID checkEvents_df.fillna("Not given", inplace=True) if len(checkEvents_df) > 0: checkEvents_df = checkEvents_df[checkEvents_df.anxietyLevel == 'Not given'] for row in checkEvents_df.itertuples(): doc_ref = db.collection(u'PatientEvents').document(row.id) doc_ref.delete() # In[62]: if (len(finalEvents_EN) > 0): finalEvents_EN = finalEvents_EN.drop_duplicates() finalEvents_AR = finalEvents_AR.drop_duplicates() # In[63]: if (len(finalEvents_EN) > 0): for ind, row in finalEvents_EN.iterrows(): try: finalEvents_EN.loc[ind, 'Event Name'] = get_display( arabic_reshaper.reshape( finalEvents_EN.loc[ind, 'Event Name'])) except: pass if (len(finalEvents_AR) > 0): #finalEvents_AR['مستوى القلق'] = 'مرتفع' for ind, row in finalEvents_AR.iterrows(): try: finalEvents_AR.loc[ind, 'اسم الحدث'] = get_display( arabic_reshaper.reshape( finalEvents_AR.loc[ind, 'اسم الحدث'])) #finalEvents_AR.loc[ind,'مستوى القلق']=get_display(arabic_reshaper.reshape(finalEvents_AR.loc[ind,'مستوى القلق'])) except: pass finalEvents_AR = finalEvents_AR.rename(columns={ 'اسم الحدث': get_display(arabic_reshaper.reshape('اسم الحدث')) }) #finalEvents_AR = finalEvents_AR.rename(columns={'مستوى القلق': get_display(arabic_reshaper.reshape('مستوى القلق')) }) finalEvents_AR = finalEvents_AR.rename( columns={ 'تاريخ الحدث': get_display(arabic_reshaper.reshape('تاريخ الحدث')) }) # In[64]: if (len(finalEvents_EN) > 0): render_mpl_table(finalEvents_EN, header_columns=0, col_width=4, tran='Events-EN') if (len(finalEvents_AR) > 0): render_mpl_table(finalEvents_AR, header_columns=0, col_width=4, tran='Events-AR') # In[ ]: # # Genertate patient report and save it in storage # In[65]: from reportlab.lib import colors # In[66]: pdfmetrics.registerFont(TTFont('Arabic', 'traditional-arabic/tradbdo.TTF')) # In[67]: pdf = canvas.Canvas('Patient-EN.pdf') pdf.setTitle('Patient report') pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFillColor(colors.HexColor('#e1eff7')) pdf.roundRect(57, 620, 485, 50, 4, fill=1, stroke=0) pdf.setFont("Helvetica-Bold", 16) pdf.setFillColor(colors.HexColor('#23495f')) pdf.drawString( 115, 638, "Report Duration From: " + dates[0] + " To: " + dates[len(dates) - 1]) pdf.setFillColor(colors.HexColor('#e1eff7')) pdf.roundRect(57, 400, 485, 200, 4, fill=1, stroke=0) pdf.setFont("Helvetica-Bold", 20) pdf.setFillColor(colors.HexColor('#23495f')) pdf.drawString(100, 570, "Patient Information") pdf.setFont("Helvetica-Bold", 15) pdf.drawString(150, 550, "Name: ") pdf.drawString(150, 530, "Age: ") pdf.drawString(150, 510, "Employment Status: ") pdf.drawString(150, 490, "Martial Status: ") pdf.drawString(150, 470, "Monthly Income: ") pdf.drawString(150, 450, "Chronic Diseases: ") pdf.drawString(150, 430, "Cigarette Smoker: ") pdf.drawString(150, 410, "Gender: ") pdf.setFont("Helvetica", 15) pdf.setFillColor(black) pdf.drawString(210, 550, name) pdf.drawString(210, 530, age) pdf.drawString(310, 510, emp) pdf.drawString(260, 490, mar) pdf.drawString(290, 470, income) pdf.drawString(290, 450, chronicD) pdf.drawString(290, 430, smoke) pdf.drawString(220, 410, gender) pdf.setFillColor(colors.HexColor('#bfbfbf')) pdf.roundRect(370, 560, 125, 30, 4, fill=1, stroke=0) pdf.setFillColorRGB(1, 1, 1) pdf.drawString(375, 570, "GAD-7 Score = ") pdf.setFont("Helvetica-Bold", 15) pdf.drawString(480, 570, gad) pdf.setFillColor(colors.HexColor('#e1eff7')) pdf.roundRect(57, 160, 485, 200, 4, fill=1, stroke=0) pdf.setFont("Helvetica-Bold", 16) pdf.setFillColor(colors.HexColor('#23495f')) pdf.drawString(80, 330, "Recommendations: ") pdf.drawString(100, 300, "Sleep Recomendation: ") pdf.drawString(100, 220, "Steps Recomendation: ") pdf.setFont("Helvetica", 11) pdf.setFillColor(black) if (sleepRecomendation == True): t = pdf.beginText(100, 280) text = [ "It is recommended to sleep at least 7-9 hours per day to get lower chance of anxiety.", "Getting a good night’s sleep can improve your mental well-being and help you to better", "manage your anxiety." ] for line in text: t.textLine(line) pdf.drawText(t) else: t = pdf.beginText(100, 280) text = [ "Good News! It seems you're getting enough sleep!", "Getting a good night’s sleep can improve your mental well-being and help you to better", "manage your anxiety." ] for line in text: t.textLine(line) pdf.drawText(t) if (stepsRecomendation == True): t = pdf.beginText(100, 200) text = [ "It is recommended to walk at least " + recomendedSteps + " steps per day to get lower chance of anxiety.", "Keep in mind that active people have lower rates of anxiety than sedentary people." ] for line in text: t.textLine(line) pdf.drawText(t) else: t = pdf.beginText(100, 200) text = [ "Great Work! You're considered an active person based on your average steps per day.", "Keep in mind that active people have lower rates of anxiety than sedentary people." ] for line in text: t.textLine(line) pdf.drawText(t) pdf.showPage() pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFont("Helvetica-Bold", 20) pdf.setFillColor(colors.HexColor('#808080')) pdf.drawString(100, 650, "Anxiety Level") pdf.drawImage("ALpdf.png", 57, 400, width=485, height=200) pdf.drawString(100, 320, "Locations With Highest Level of Anxiety") if (len(analysis_EN) > 0): pdf.drawImage("Location-EN.png", 30, 200, width=570, height=100) else: pdf.setFont("Helvetica", 15) pdf.setFillColor(colors.HexColor('#23495f')) t = pdf.beginText(130, 250) text = [ name + " condition was stable through this period,", "no locations with high anxiety level were detected." ] for line in text: t.textLine(line) pdf.drawText(t) pdf.showPage() pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFont("Helvetica-Bold", 20) pdf.setFillColor(colors.HexColor('#808080')) pdf.drawString(100, 650, "Events With Highest Level of Anxiety") if (GoogleCalendar == 'true' and len(finalEvents_EN) > 0): pdf.drawImage("Events-EN.png", 30, 500, width=550, height=100) elif (GoogleCalendar == 'true' and len(finalEvents_EN) < 0): pdf.setFont("Helvetica", 15) pdf.setFillColor(colors.HexColor('#23495f')) t = pdf.beginText(130, 550) text = ["No events found associated with high anxiety level"] for line in text: t.textLine(line) pdf.drawText(t) else: pdf.setFont("Helvetica", 15) pdf.setFillColor(colors.HexColor('#23495f')) t = pdf.beginText(130, 550) text = [ name + " have not synced Google Calendar events,", "with thier account." ] for line in text: t.textLine(line) pdf.drawText(t) if (len(notSyncedDates) != 0): pdf.setFont("Helvetica", 12) pdf.setFillColor(colors.HexColor('#d40027')) pdf.drawString( 75, 95, "Note: Below dates are missing, because they were not synced correctly:" ) i = 70 for row in notSyncedDates.itertuples(): pdf.drawString(85, i, '- ' + row.date) i = i - 20 pdf.save() # In[68]: def translate(text): reshaped_text = arabic_reshaper.reshape(text) bidi_text = get_display(reshaped_text) return bidi_text # In[69]: def from_en_to_ar(text): translator = Translator() result = translator.translate(text, dest='ar') return result.text # In[70]: pdf = canvas.Canvas('Patient-AR.pdf') pdf.setTitle('تقرير المريض') pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFillColor(colors.HexColor('#e1eff7')) pdf.roundRect(57, 620, 485, 50, 4, fill=1, stroke=0) pdf.setFont("Arabic", 22) pdf.setFillColor(colors.HexColor('#23495f')) reportDuration = translate(u'مدة التقرير من') to = translate(u'إلى') pdf.drawString( 125, 638, dates[len(dates) - 1] + ' ' + to + ' ' + dates[0] + ' ' + reportDuration) pdf.setFillColor(colors.HexColor('#e1eff7')) pdf.roundRect(57, 400, 485, 200, 4, fill=1, stroke=0) pdf.setFont("Arabic", 22) pdf.setFillColor(colors.HexColor('#23495f')) patient = translate(u'معلومات المريض') pdf.drawString(400, 570, patient) na = translate(u'الاسم:') ag = translate(u'العمر:') Es = translate(u'الحالة الوظيفية:') Ms = translate(u'الحالة الإجتماعية:') Mi = translate(u'الدخل الشهري:') Cd = translate(u'الأمراض المزمنة:') Cs = translate(u'مدخن:') ge = translate(u'الجنس:') pdf.setFont("Arabic", 18) pdf.drawString(445, 550, na) pdf.drawString(446, 530, ag) pdf.drawString(400, 510, Es) pdf.drawString(390, 490, Ms) pdf.drawString(400, 470, Mi) pdf.drawString(395, 450, Cd) pdf.drawString(445, 430, Cs) pdf.drawString(446, 410, ge) pdf.setFont("Arabic", 15) pdf.setFillColor(black) pdf.drawString(400, 550, name) pdf.drawString(420, 530, age) pdf.drawString(350, 510, translate(from_en_to_ar(emp))) pdf.drawString(355, 490, translate(from_en_to_ar(mar))) pdf.drawString(360, 470, income) pdf.drawString(360, 450, translate(from_en_to_ar(chronicD))) pdf.drawString(420, 430, translate(from_en_to_ar(smoke))) pdf.drawString(420, 410, translate(from_en_to_ar(gender))) pdf.setFillColor(colors.HexColor('#bfbfbf')) pdf.roundRect(100, 560, 120, 30, 4, fill=1, stroke=0) ga = translate(u'مقياس GAD-7 =') pdf.setFillColorRGB(1, 1, 1) pdf.drawString(120, 570, ga) pdf.setFont("Helvetica-Bold", 15) pdf.drawString(105, 570, gad) pdf.setFillColor(colors.HexColor('#e1eff7')) pdf.roundRect(57, 160, 485, 200, 4, fill=1, stroke=0) pdf.setFont("Arabic", 22) pdf.setFillColor(colors.HexColor('#23495f')) rec = translate(u' التوصيات:') stepRec = translate(u'توصيات النشاط:') sleepRec = translate(u'توصيات النوم:') pdf.drawString(420, 330, rec) pdf.drawString(380, 300, sleepRec) pdf.drawString(360, 220, stepRec) pdf.setFont("Arabic", 15) pdf.setFillColor(black) if (sleepRecomendation == True): text = translate( u'يوصى بالنوم لمدة تتراوح بين ٧-٩ ساعات في اليوم للحصول على أقل معدل قلق.' ) text2 = translate( u'يمكن أن يحسن النوم الجيد ليلاً من صحتك العقلية ويساعدك على إدارة قلقك بشكل أفضل.' ) pdf.drawString(120, 280, text) pdf.drawString(80, 260, text2) else: text = translate(u'أخبار جيدة! يبدو أنك تحصل على قسط كافٍ من اليوم.') text2 = translate( u'يمكن أن يحسن النوم الجيد ليلاً من صحتك العقلية ويساعدك على إدارة قلقك بشكل أفضل.' ) pdf.drawString(230, 280, text) pdf.drawString(80, 260, text2) if (stepsRecomendation == True): btwnText = translate(u'خطوة في اليوم للحصول على أقل معدل قلق. ') text = translate(u'يوصى بالمشي على الأقل') text2 = translate( u'تذكر أن الأشخاص الأكثر نشاطاً لديهم معدلات قلق أقل من أولئك الغير نشطين.' ) pdf.drawString(130, 200, btwnText + recomendedSteps + text) pdf.drawString(120, 180, text2) else: text = translate( u'عمل رائع! بناءاً على معدل خطواتك في اليوم الواحد، أنت تعتبر من الأشخاص النشطين.' ) text2 = translate( u'تذكر أن الأشخاص الأكثر نشاطاً لديهم معدلات قلق أقل من أولئك الغير نشطين.' ) pdf.drawString(100, 200, text) pdf.drawString(130, 180, text2) pdf.showPage() pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFont("Arabic", 28) pdf.setFillColor(colors.HexColor('#808080')) AL = translate(u'مستوى القلق') pdf.drawString(400, 650, AL) loc = translate(u'تحليل المواقع ذات مستوى قلق مرتفع ') pdf.drawImage("ALpdf.png", 57, 400, width=485, height=200) pdf.drawString(210, 320, loc) if (len(analysis_AR) > 0): pdf.drawImage("Location-AR.png", 10, 200, width=570, height=100) else: pdf.setFont("Arabic", 18) pdf.setFillColor(colors.HexColor('#23495f')) text = translate( u'كانت الحالة مستقرة خلال هذه الفترة, لم يتم الكشف عن أي مواقع ذات مستوى قلق مرتفع.' ) pdf.drawString(50, 250, text) pdf.showPage() pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFont("Arabic", 28) pdf.setFillColor(colors.HexColor('#808080')) EV = translate(u'تحليل الأحداث ذات مستوى قلق مرتفع ') pdf.drawString(200, 650, EV) if (GoogleCalendar == 'true' and len(finalEvents_AR) > 0): pdf.drawImage("Events-AR.png", 20, 500, width=550, height=100) elif (GoogleCalendar == 'true' and len(finalEvents_EN) < 0): pdf.setFont("Arabic", 18) pdf.setFillColor(colors.HexColor('#23495f')) text = translate(u' لم يتم الكشف أي أحداث ذات مستوى قلق مرتفع.') pdf.drawString(250, 550, text) else: pdf.setFont("Arabic", 18) pdf.setFillColor(colors.HexColor('#23495f')) text = translate( u'المستخدم لم يقم بمزامنة أحداث تقويم قوقل بحساب سيرين.') pdf.drawString(210, 550, text) if (len(notSyncedDates) != 0): pdf.setFont("Arabic", 15) pdf.setFillColor(colors.HexColor('#d40027')) note = translate( u'.ملاحظة: التواريخ المذكورة أدناه غير متضمنة في التحليل لعدم مزامنة البيانات' ) pdf.drawString(200, 95, note) i = 70 for row in notSyncedDates.itertuples(): pdf.setFont("Helvetica", 12) pdf.drawString(450, i, row.date + ' -') i = i - 20 pdf.save() # In[71]: #new method doct = storage.child(userID + "/lastGeneratedPatientReport/patientReport-EN").put( 'Patient-EN.pdf') doct = storage.child(userID + "/lastGeneratedPatientReport/patientReport-AR").put( 'Patient-AR.pdf') # In[72]: os.remove('Patient-EN.pdf') os.remove('Patient-AR.pdf') if (len(plot_df) <= 90): os.remove('ALpdf.png') else: for x in range(0, len(dfarray)): os.remove('AL' + str(x) + 'pdf.png') if (GoogleCalendar == 'true' and len(finalEvents_AR) > 0): os.remove('Events-AR.png') os.remove('Events-EN.png') if (len(analysis_AR) > 0): os.remove('Location-AR.png') os.remove('Location-EN.png') to_json = plot_df[['date', 'Anxiety']].to_json() return to_json
def _draw_time_series(indicator, indicator_name, indicator_hat_fourier, indicator_hat_lstsq, indicator_hat_sindy, node_labels, test_index): for node_id in range(indicator.shape[1]): # Time Series plot data_frame = pd.DataFrame({ 'index': np.arange(indicator.shape[0]), indicator_name: indicator[:, node_id], 'Fourier': indicator_hat_fourier[:, node_id], # 'Least_Squares': indicator_hat_lstsq[:, node_id], 'SINDy': indicator_hat_sindy[:, node_id], }) melted_data_frame = pd.melt( data_frame, id_vars=['index'], value_vars=[ indicator_name, 'Fourier', # 'Least-Squares', 'SINDy', ]) rc('font', weight=600) plt.subplots(figsize=(20, 10)) ax = sns.lineplot(x='index', y='value', hue='variable', style='variable', data=melted_data_frame, linewidth=4) node_instrument_id, node_name = node_labels[node_id].split('_') ax.set_title(get_display(arabic_reshaper.reshape(node_name)), fontsize=28, fontweight=500) ax.set_xlabel(get_display(arabic_reshaper.reshape('روزها')), fontsize=20, fontweight=500) ax.set_ylabel(indicator_name, fontsize=20, fontweight=500) for axis in ['top', 'bottom', 'left', 'right']: ax.spines[axis].set_linewidth(3) ax.tick_params(width=3, length=10, labelsize=16) plt.savefig( os.path.join( OUTPUT_DIR, 'node_%d_%s_%s.png' % (node_id, node_instrument_id, indicator_name))) plt.close('all') # Signal Plot lag = 5 threshold = 1 influence = 1 indicator_signal = thresholding_alg(indicator[:, node_id], lag, threshold, influence)[test_index:] indicator_hat_sindy_signal = \ thresholding_alg(indicator_hat_sindy[:, node_id], lag, threshold, influence)[test_index:] data_frame = pd.DataFrame({ 'index': np.arange(len(indicator_signal)), '%s' % indicator_name + get_display( arabic_reshaper.reshape('قلههای')): indicator_signal, 'SINDy' + get_display(arabic_reshaper.reshape('قلههای')): indicator_hat_sindy_signal, }) melted_data_frame = pd.melt( data_frame, id_vars=['index'], value_vars=[ '%s' % indicator_name + get_display(arabic_reshaper.reshape('قلههای')), 'SINDy' + get_display(arabic_reshaper.reshape('قلههای')), ]) rc('font', weight=600) plt.subplots(figsize=(20, 10)) ax = sns.lineplot(x='index', y='value', hue='variable', style='variable', data=melted_data_frame, linewidth=4) node_instrument_id, node_name = node_labels[node_id].split('_') ax.set_title(get_display(arabic_reshaper.reshape(node_name)), fontsize=28, fontweight=500) ax.set_xlabel(get_display( arabic_reshaper.reshape('روزهای پیشبینی شده')), fontsize=20, fontweight=500) ax.set_ylabel(get_display( arabic_reshaper.reshape('قلههای تشخیص داده شده')), fontsize=20, fontweight=500) for axis in ['top', 'bottom', 'left', 'right']: ax.spines[axis].set_linewidth(3) ax.tick_params(width=3, length=10, labelsize=16) plt.savefig( os.path.join( OUTPUT_DIR, 'node_%d_%s_%s_peaks.png' % (node_id, node_instrument_id, indicator_name))) plt.close('all')
def to_persion_text(text): return get_display(ar.reshape(text))
a = cv2.waitKey(1) if frame_counter % 5 == 0: score = 0 res = '' try: proba = model.predict(process_image(img_cropped))[0] mx = np.argmax(proba) score = proba[mx] * 100 res = categories[mx][0] sequence = categories[mx][1] except: continue reshaped_text = arabic_reshaper.reshape(sequence) bidi_text = get_display(reshaped_text) frame_counter += 1 img_pil = Image.fromarray(img) draw = ImageDraw.Draw(img_pil) draw.text((50, 600), bidi_text, (255, 255, 255), font=font) img = np.array(img_pil) #cv2.putText(img, '%s' % (res.upper()), (100,400), cv2.FONT_HERSHEY_SIMPLEX, 4, (255,255,255), 4) cv2.putText(img, '(score = %.5f)' % (float(score)), (100, 450), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255)) cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2) cv2.imshow("img", img) if a == 27: # when `esc` is pressed break
def OnConvert(self, evt): self.tc2.Clear() reshaped_text = arabic_reshaper.reshape(self.tc1.GetValue()) bidi_text = get_display(reshaped_text) self.tc2.WriteText(bidi_text)
def print_receipt(): receipt_from_tailpos = loads(request.get_data(as_text=True)) for_printing = receipt_from_tailpos['data'] print(for_printing) port_serial = "/dev/rfcomm0" bluetoothSerial = serial.Serial(port_serial, baudrate=115200, timeout=1) #fontPath = "/home/jiloysss/Documents/spiceco/aljazeera-font/FontAljazeeraColor-lzzD.ttf" fontPath = "/home/pi/FontAljazeeraColor-lzzD.ttf" tmpImage = 'receipt.png' #printWidth = 375 printWidth = 570 height = 600 draw = wDrawing() draw.font = fontPath #COMPANY ============== draw.font_size = 34 draw.text(x=180,y=75,body=for_printing['company']) #DATE ================== split_date = for_printing['date'].split() draw.font_size = 26 draw.text(x=5,y=110,body=split_date[0]) draw.text(x=260,y=110,body=split_date[1]) #ORDER TYPE ============== draw.font_size = 26 draw.text(x=5,y=145,body="Order Type: " + for_printing['ordertype']) y_value = 145 #HEADER ========== if for_printing['header']: header_value = 160 for x in for_printing['header'].split("\n"): y_value = y_value + 35 header_value = header_value + 25 draw.text_alignment = "center" draw.text(x=180,y=header_value,body=x) draw.text_alignment = "undefined" draw.text(x=5,y=y_value + 35 ,body="=====================================") #ITEM PURCHASES y_value = y_value + 30 for idx,i in enumerate(for_printing['lines']): if idx != 0: height += 35 draw.gravity = "north_east" draw.text(x=5,y=y_value + 10,body=format(float(i['qty'] * i['price']), '.2f')) draw.gravity = "forget" if len(i['item_name']) > 22: quotient = len(i['item_name']) / 22 for xxx in range(0,int(quotient)): if idx != 0: height += 35 y_value = y_value + 35 draw.text(x=5,y=y_value,body=i['item_name'][xxx * 22: (xxx+1) * 22]) translation_text = "" if i['translation_text']: textReshaped = arabic_reshaper.reshape(i['translation_text']) textDisplay = get_display(textReshaped) translation_text = "(" + textReshaped + ")" y_value = y_value + 35 draw.text(x=5,y=y_value,body=i['item_name'][(int(quotient)*22): len(i['item_name'])] + translation_text ) else: translation_text = "" if i['translation_text']: textReshaped = arabic_reshaper.reshape(i['translation_text']) textDisplay = get_display(textReshaped) translation_text = "(" + textReshaped + ")" y_value = y_value + 35 draw.text(x=5,y=y_value,body=i['item_name'] ) y_value = y_value + 35 draw.text(x=5,y=y_value,body= translation_text) draw.text(x=5,y=y_value+35,body="=====================================") y_value = y_value + 35 #SUBTOTAL draw.text(x=5,y=y_value + 35,body="Subtotal") draw.gravity = "north_east" draw.text(x=5,y=y_value + 5,body=for_printing['subtotal']) draw.gravity = "forget" y_value = y_value + 35 #DISCOUNT draw.text(x=5,y=y_value + 35,body="Discount") draw.gravity = "north_east" draw.text(x=5,y=y_value + 5,body=for_printing['discount']) draw.gravity = "forget" #TAXES VALUES if len(for_printing['taxesvalues']) > 0: y_value = y_value + 35 for idx,iii in enumerate(for_printing['taxesvalues']): if idx != 0: height += 35 y_value = y_value + 35 draw.text(x=5,y=y_value,body=iii['name']) draw.gravity = "north_east" draw.text(x=5,y=y_value - 25,body=str(format(round(float(iii['totalAmount']),2), '.2f'))) draw.gravity = "forget" #MODE OF PAYMENT for idx, ii in enumerate(loads(for_printing['mop'])): if idx != 0: height += 35 y_value = y_value + 70 type = ii['type'] if ii['translation_text']: textReshaped = arabic_reshaper.reshape(ii['translation_text']) textDisplay = get_display(textReshaped) type += "(" + textReshaped + ")" draw.text(x=5,y=y_value,body=type) draw.gravity = "north_east" draw.text(x=5,y=y_value - 25,body=str(format(float(ii['amount']), '.2f'))) draw.gravity = "forget" #TOTAL AMOUNT draw.text(x=5,y=y_value + 35,body="Total Amount") draw.gravity = "north_east" draw.text(x=5,y=y_value + 5,body=str(format(float(for_printing['total_amount']), '.2f'))) draw.gravity = "forget" #CHANGE draw.text(x=5,y=y_value + 70,body="Change") draw.gravity = "north_east" draw.text(x=5,y=y_value + 43,body=str(format(float(for_printing['change']), '.2f'))) draw.gravity = "forget" draw.text(x=5,y=y_value+105,body="=====================================") #FOOTER ========== if for_printing['footer']: header_value = y_value+105 for x in for_printing['footer'].split("\n"): y_value = y_value + 35 header_value = header_value + 25 draw.text_alignment = "center" draw.text(x=180,y=header_value,body=x) im = wImage(width=printWidth, height=height, background=wColor('#ffffff')) draw(im) im.save(filename=tmpImage) # Print an image with your printer library printertest = printer.File(port_serial) printertest.set(align="left") printertest.image(tmpImage) printertest.cut() print("SAMOKA GYUD Oi") bluetoothSerial.close() return {}
def bidiRegion(region, view, edit): txt = view.substr(region) reshaped_text = arabic_reshaper.reshape(txt) view.replace(edit, region, reshaped_text)
def Upload_Image(): file = asfile = askopenfile(title='Upload Image', filetypes=[('Image Files', [ '.jpeg', '.jpg', '.png', '.gif', '.tiff', '.tif', '.bmp' ])]) if file is not None: #print(file.name) image = cv2.imread(file.name) text = '' image = cv2.resize(image, (600, 500)) blob = cv2.dnn.blobFromImage(image, 0.00392, (416, 416), (0, 0, 0), True, crop=False) net.setInput(blob) layer_names = net.getLayerNames() output_layers = [ layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers() ] output_layers = net.forward(output_layers) class_ids = [] confidences = [] boxes = [] for out in output_layers: for detection in out: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > 0.5: center_x = int(detection[0] * image.shape[1]) center_y = int(detection[1] * image.shape[0]) w = int(detection[2] * image.shape[1]) h = int(detection[3] * image.shape[0]) x = center_x - w / 2 y = center_y - h / 2 class_ids.append(class_id) confidences.append(float(confidence)) boxes.append([x, y, w, h]) indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) for i in indices: i = i[0] box = boxes[i] x = box[0] y = box[1] w = box[2] h = box[3] text = str(classes[class_ids[i]][1]) font = ImageFont.truetype("font.ttf", 35) reshaped_text = arabic_reshaper.reshape(text) arabic_text = get_display(reshaped_text) #print(arabic_text) image_pil = Image.fromarray(image) draw = ImageDraw.Draw(image_pil) draw.text((round(x), round(y) - 50), arabic_text, (255, 255, 255), font=font) image = np.array(image_pil) cv2.rectangle(image, (round(x), round(y)), (round(x + w), round(y + h)), (255, 255, 255), 2) #cv2.putText(image, "{:.0f}%".format(confidences[i]*100), (round(x),round(y)-40), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,0,0), 2) cv2.imshow("Object Detection Using YOLOv3", image) cv2.waitKey() cv2.destroyAllWindows()
def getPicture(poem, poet): allBGs = glob.glob('/home/ali/Pictures/background/*.*') selected = randint(0, len(allBGs) - 1) pic = allBGs[selected] img = Image.open(pic) img = img.resize((800, 300)) offset = 200 draw = ImageDraw.Draw(img) sentences = poem maxSize = 0 boundingSize = (550, 300) for sent in sentences: if (len(sent) > maxSize): maxSize = len(sent) maxSent = sent t1 = arabic_reshaper.reshape(maxSent.decode('utf-8')) maxSent = get_display(t1) fontSize = 2 fontAddress = "/home/ali/Fonts/Mj_Sandbad.ttf" while (1): font = ImageFont.truetype(fontAddress, fontSize) draw.setfont(font) width = draw.textsize(maxSent)[0] height = len(sentences) * draw.textsize(maxSent)[1] if (width < boundingSize[0] and height < boundingSize[1]): fontSize = fontSize + 1 continue else: break font = ImageFont.truetype(fontAddress, fontSize - 1) draw.setfont(font) i = 0 W, H = boundingSize color = (255, 255, 255) for sent in sentences: t1 = arabic_reshaper.reshape(sent.decode('utf-8')) text = get_display(t1) size = draw.textsize(text) ## draw.text((x, y),"Sample Text",(r,g,b)) draw.text( (W / 2 - size[0] / 2 + offset, H / 2 - len(sentences) * size[1] / 2 - size[1] / 4 + i * size[1]), text, color) i = i + 1 font = ImageFont.truetype(fontAddress, 25) draw.setfont(font) t1 = arabic_reshaper.reshape(poet.decode('utf-8')) ppoet = get_display(t1) size = draw.textsize(ppoet) position = (W / 2 - size[0] / 2, H - size[1] - 20) #pixel = img.getpixel(position) #hsv = colorsys.rgb_to_hsv(float(pixel[0])/255,float(pixel[1])/255,float(pixel[2])/255) #rgb = colorsys.hsv_to_rgb(((hsv[0]-.5)*360%360)/360,1-hsv[1],1-hsv[2]) rgb = (1, 1, 1) draw.text(position, ppoet, (int(255 * rgb[0]), int(255 * rgb[1]), int(255 * rgb[2]))) avatar = getAvatar(poet) avatar = avatar.resize([offset, offset]) img.paste(avatar, (0, img.size[1] - offset), avatar) return img
def printit_ar(pname, ti, ofc, tnu, tas, cticket, lang=None): def fsizeit(text, t, f): ltxt = "A" * len(t) return f.getsize(t) def center(text, t, f): fs1, fs2 = fsizeit(text, t, f) return ((text.size[0] - fs1) / 2, (text.size[1] - fs2) / 2) if name == 'nt': fpath = absolute_path('static\\gfonts\\arial.ttf') else: fpath = absolute_path('static/gfonts/arial.ttf') fonts = [ ImageFont.truetype(fpath, 50), ImageFont.truetype(fpath, 30), ImageFont.truetype(fpath, 25) ] logo = 'FQM ' + VERSION[:4] title = u'نظام إدارة الحشود الحر' title = arabic_reshaper.reshape(title) title = get_display(title) link = 'http://fqms.github.io' border = "#" * 20 ticket = str(ti) officet = u'المكتب : ' + ofc office = arabic_reshaper.reshape(officet) office = get_display(office) try: taskt = u'المهمة : ' + tas except Exception: taskt = tas task = arabic_reshaper.reshape(taskt) task = get_display(task) datet = u'الوقت : ' datet += str(datetime.now())[:-7] date = arabic_reshaper.reshape(datet) date = get_display(date) aheadt = u'تذاكر قبلك : ' aheadt += str(tnu) ahead = arabic_reshaper.reshape(aheadt) ahead = get_display(ahead) cutit = u'التذكرة الحالية : ' cutit += str(cticket) cuti = arabic_reshaper.reshape(cutit) cuti = get_display(cuti) w = 400 bt_1 = Image.new('RGB', (w, 60), "white") bt_2 = Image.new('RGB', (w, 60), "white") bt_3 = Image.new('RGB', (w, 60), "white") st_1 = Image.new('RGB', (w, 50), "white") st_2 = Image.new('RGB', (w, 50), "white") st_3 = Image.new('RGB', (w, 50), "white") st_4 = Image.new('RGB', (w, 50), "white") st_5 = Image.new('RGB', (w, 50), "white") st_6 = Image.new('RGB', (w, 50), "white") st_7 = Image.new('RGB', (w, 50), "white") st_8 = Image.new('RGB', (w, 50), "white") tt = 50 * 8 tt += 60 * 3 mt = Image.new('RGB', (w, tt), "white") bd_1 = ImageDraw.Draw(bt_1) bd_2 = ImageDraw.Draw(bt_2) bd_3 = ImageDraw.Draw(bt_3) sd_1 = ImageDraw.Draw(st_1) sd_2 = ImageDraw.Draw(st_2) sd_3 = ImageDraw.Draw(st_3) sd_4 = ImageDraw.Draw(st_4) sd_5 = ImageDraw.Draw(st_5) sd_6 = ImageDraw.Draw(st_6) sd_7 = ImageDraw.Draw(st_7) sd_8 = ImageDraw.Draw(st_8) md = ImageDraw.Draw(mt) b = "black" bd_1.text(center(bt_1, logo, fonts[0]), logo, font=fonts[0], fill=b) bd_2.text(center(bt_2, title, fonts[1]), title, font=fonts[1], fill=b) bd_3.text(center(bt_3, ticket, fonts[0]), ticket, font=fonts[0], fill=b) sd_1.text(center(st_1, link, fonts[2]), link, font=fonts[2], fill=b) sd_2.text(center(st_2, border, fonts[2]), border, font=fonts[2], fill=b) sd_3.text(center(st_3, border, fonts[2]), border, font=fonts[2], fill=b) sd_4.text(center(st_4, office, fonts[2]), office, font=fonts[2], fill=b) sd_5.text(center(st_5, task, fonts[2]), task, font=fonts[2], fill=b) sd_6.text(center(st_6, date, fonts[2]), date, font=fonts[2], fill=b) sd_7.text(center(st_7, ahead, fonts[2]), ahead, font=fonts[2], fill=b) sd_8.text(center(st_8, cuti, fonts[2]), cuti, font=fonts[2], fill=b) tts = 0 mt.paste(bt_1, (0, 0)) tts += bt_1.size[1] mt.paste(bt_2, (0, tts)) tts += bt_2.size[1] mt.paste(st_1, (0, tts)) tts += st_1.size[1] mt.paste(st_2, (0, tts)) tts += st_2.size[1] mt.paste(bt_3, (0, tts)) tts += bt_3.size[1] mt.paste(st_3, (0, tts)) tts += st_3.size[1] mt.paste(st_4, (0, tts)) tts += st_4.size[1] mt.paste(st_8, (0, tts)) tts += st_8.size[1] mt.paste(st_7, (0, tts)) tts += st_7.size[1] mt.paste(st_5, (0, tts)) tts += st_5.size[1] mt.paste(st_6, (0, tts)) iname = 'dummy.jpg' finame = path.join(getcwd(), iname) mt.save(iname, format="JPEG") pname.image(iname, fragment_height=tt, high_density_vertical=True) pname.cut() pname.close() if path.isfile(finame): remove(finame)
def normalize_text(text): text = reverse_transcription(text) text = arabic_reshaper.reshape(text) text = reverse_transcription(text) return text
def _generate_v2_certificate(self, student_name, download_dir, verify_dir, filename='Certificate.pdf'): """ We have a new set of certificates that we want to generate which means brand new generation of certs """ # 8.5x11 page size 279.4mm x 215.9mm WIDTH = 279 # width in mm (8.5x11) HEIGHT = 216 # height in mm (8.5x11) verify_uuid = uuid.uuid4().hex download_uuid = uuid.uuid4().hex download_url = "{base_url}/{cert}/{uuid}/{file}".format( base_url=settings.CERT_DOWNLOAD_URL, cert=S3_CERT_PATH, uuid=download_uuid, file=filename ) filename = os.path.join(download_dir, download_uuid, filename) # This file is overlaid on the template certificate overlay_pdf_buffer = StringIO.StringIO() c = canvas.Canvas(overlay_pdf_buffer) c.setPageSize((WIDTH * mm, HEIGHT * mm)) # register all fonts in the fonts/ dir, # there are more fonts in here than we need # but the performance hit seems minimal for font_file in glob('{0}/fonts/*.ttf'.format(self.template_dir)): font_name = os.path.basename(os.path.splitext(font_file)[0]) pdfmetrics.registerFont(TTFont(font_name, font_file)) styleOpenSans = ParagraphStyle(name="opensans-regular", leading=10, fontName='OpenSans-Regular') styleArial = ParagraphStyle(name="arial", leading=10, fontName='Arial Unicode') # Text is overlayed top to bottom # * Issued date (top right corner) # * "This is to certify that" # * Student's name # * "successfully completed" # * Course name # * "a course of study.." # * honor code url at the bottom ######## # # New things below # ######## #### STYLE: typeface assets addMapping('AvenirNext-Regular', 0, 0, 'AvenirNext-Regular') addMapping('AvenirNext-DemiBold', 1, 0, 'AvenirNext-DemiBold') #### STYLE: grid/layout LEFT_INDENT = 23 # mm from the left side to write the text MAX_WIDTH = 150 # maximum width on the content in the cert, used for wrapping #### STYLE: template-wide typography settings style_type_metacopy_size = 13 style_type_metacopy_leading = 10 style_type_footer_size = 8 style_type_name_size = 36 style_type_name_leading = 53 style_type_name_med_size = 28 style_type_name_med_leading = 41 style_type_name_small_size = 22 style_type_name_small_leading = 27 style_type_course_size = 24 style_type_course_leading = 28 style_type_course_small_size = 16 style_type_course_small_leading = 20 #### STYLE: template-wide color settings style_color_metadata = colors.Color(0.541176, 0.509804, 0.560784) style_color_name = colors.Color(0.000000, 0.000000, 0.000000) #### STYLE: positioning pos_metacopy_title_y = 120 pos_metacopy_achivement_y = 88 pos_metacopy_org_y = 50 pos_name_y = 94 pos_name_med_y = 95 pos_name_small_y = 95 pos_name_no_wrap_offset_y = 2 pos_course_y = 68 pos_course_small_y = 66 pos_course_no_wrap_offset_y = 5 pos_footer_url_x = 83 pos_footer_url_y = 20 pos_footer_date_x = LEFT_INDENT pos_footer_date_y = 20 #### STYLE: verified settings v_style_color_course = colors.Color(0.701961, 0.231373, 0.400000) #### HTML Parser #### # Since the final string is HTML in a PDF we need to un-escape the html # when calculating the string width. html = HTMLParser() #### ================== #### #### ELEM: Metacopy styleAvenirNext = ParagraphStyle(name="avenirnext-regular", fontName='AvenirNext-Regular') styleAvenirNext.alignment = TA_LEFT styleAvenirNext.fontSize = style_type_metacopy_size styleAvenirNext.leading = style_type_metacopy_leading styleAvenirNext.textColor = style_color_metadata #### ELEM: Metacopy - Title: This is to certify that if self.template_type == 'verified': y_offset = pos_metacopy_title_y paragraph_string = 'This is to certify that' paragraph = Paragraph(paragraph_string, styleAvenirNext) paragraph.wrapOn(c, WIDTH * mm, HEIGHT * mm) paragraph.drawOn(c, LEFT_INDENT * mm, y_offset * mm) #### ================== #### ####### ELEM: Student Name # default is to use Avenir for the name, # will fall back to Arial if there are # unusual characters y_offset_name = pos_name_y y_offset_name_med = pos_name_med_y y_offset_name_small = pos_name_small_y styleAvenirStudentName = ParagraphStyle(name="avenirnext-demi", fontName='AvenirNext-DemiBold') styleAvenirStudentName.leading = style_type_name_small_size style = styleAvenirStudentName html_student_name = html.unescape(student_name) larger_width = stringWidth(html_student_name.decode('utf-8'), 'AvenirNext-DemiBold', style_type_name_size) / mm smaller_width = stringWidth( html_student_name.decode('utf-8'), 'AvenirNext-DemiBold', style_type_name_small_size) / mm #TODO: get all strings working reshaped and handling bi-directional strings paragraph_string = arabic_reshaper.reshape(student_name.decode('utf-8')) paragraph_string = get_display(paragraph_string) # Avenir only supports Latin-1 # Switch to using OpenSans if we can if self._use_non_latin(student_name): style = styleOpenSans larger_width = stringWidth(html_student_name.decode('utf-8'), 'OpenSans-Regular', style_type_name_size) / mm # if we can't use OpenSans, use Arial if self._use_unicode_font(student_name): style = styleArial larger_width = stringWidth(html_student_name.decode('utf-8'), 'Arial Unicode', style_type_name_size) / mm # if the name is too long, shrink the font size if larger_width < MAX_WIDTH: style.fontSize = style_type_name_size style.leading = style_type_name_leading y_offset = y_offset_name elif smaller_width < MAX_WIDTH: y_offset = y_offset_name_med + pos_name_no_wrap_offset_y style.fontSize = style_type_name_med_size style.leading = style_type_name_med_leading else: y_offset = y_offset_name_small style.fontSize = style_type_name_small_size style.leading = style_type_name_small_leading style.textColor = style_color_name style.alignment = TA_LEFT paragraph = Paragraph(paragraph_string, style) paragraph.wrapOn(c, MAX_WIDTH * mm, HEIGHT * mm) paragraph.drawOn(c, LEFT_INDENT * mm, y_offset * mm) #### ================== #### ##### ELEM: Metacopy - Achievement: successfully completed and received a passing grade in y_offset = pos_metacopy_achivement_y paragraph_string = 'successfully completed and received a passing grade in' paragraph = Paragraph("{0}".format(paragraph_string), styleAvenirNext) paragraph.wrapOn(c, WIDTH * mm, HEIGHT * mm) paragraph.drawOn(c, LEFT_INDENT * mm, y_offset * mm) #### ================== #### ##### ELEM: Course Name y_offset_larger = pos_course_y y_offset_smaller = pos_course_small_y styleAvenirCourseName = ParagraphStyle(name="avenirnext-demi", fontName='AvenirNext-DemiBold') styleAvenirCourseName.textColor = style_color_name if self.template_type == 'verified': styleAvenirCourseName.textColor = v_style_color_course paragraph_string = "{0}: {1}".format(self.course, self.long_course) html_paragraph_string = html.unescape(paragraph_string) larger_width = stringWidth(html_paragraph_string.decode('utf-8'), 'AvenirNext-DemiBold', style_type_course_size) / mm smaller_width = stringWidth(html_paragraph_string.decode('utf-8'), 'AvenirNext-DemiBold', style_type_course_small_size) / mm if larger_width < MAX_WIDTH: styleAvenirCourseName.fontSize = style_type_course_size styleAvenirCourseName.leading = style_type_course_leading y_offset = y_offset_larger elif smaller_width < MAX_WIDTH: styleAvenirCourseName.fontSize = style_type_course_small_size styleAvenirCourseName.leading = style_type_course_small_leading y_offset = y_offset_smaller + pos_course_no_wrap_offset_y else: styleAvenirCourseName.fontSize = style_type_course_small_size styleAvenirCourseName.leading = style_type_course_small_leading y_offset = y_offset_smaller styleAvenirCourseName.alignment = TA_LEFT paragraph = Paragraph(paragraph_string, styleAvenirCourseName) paragraph.wrapOn(c, MAX_WIDTH * mm, HEIGHT * mm) paragraph.drawOn(c, LEFT_INDENT * mm, y_offset * mm) #### ================== #### #### ELEM: Metacopy - Org: a course of study... y_offset = pos_metacopy_org_y paragraph_string = "{2} offered by {0}" \ ", an online learning<br /><br />initiative of " \ "{1} through edX.".format( self.org, self.long_org, self.course_association_text) paragraph = Paragraph(paragraph_string, styleAvenirNext) paragraph.wrapOn(c, WIDTH * mm, HEIGHT * mm) paragraph.drawOn(c, LEFT_INDENT * mm, y_offset * mm) #### ================== #### ##### ELEM: Footer styleAvenirFooter = ParagraphStyle(name="avenirnext-demi", fontName='AvenirNext-DemiBold') styleAvenirFooter.alignment = TA_LEFT styleAvenirFooter.fontSize = style_type_footer_size ##### ELEM: Footer - Issued on Date x_offset = pos_footer_date_x y_offset = pos_footer_date_y paragraph_string = "Emitido {0}".format(self.issued_date) # Issued # Right justified so we compute the width paragraph = Paragraph("{0}".format( paragraph_string), styleAvenirFooter) paragraph.wrapOn(c, WIDTH * mm, HEIGHT * mm) paragraph.drawOn(c, LEFT_INDENT * mm, y_offset * mm) ######## #### ================== #### ##### ELEM: Footer - Verify Authenticity URL y_offset = pos_footer_url_y x_offset = pos_footer_url_x paragraph_string = "<a href='https://{bucket}/{verify_path}/{verify_uuid}'>" \ "https://{bucket}/{verify_path}/{verify_uuid}</a>" paragraph_string = paragraph_string.format(bucket=BUCKET, verify_path=S3_VERIFY_PATH, verify_uuid=verify_uuid) paragraph = Paragraph(paragraph_string, styleAvenirFooter) paragraph.wrapOn(c, WIDTH * mm, HEIGHT * mm) paragraph.drawOn(c, x_offset * mm, y_offset * mm) c.showPage() c.save() # Merge the overlay with the template, then write it to file output = PdfFileWriter() overlay = PdfFileReader(overlay_pdf_buffer) # We need a page to overlay on. # So that we don't have to open the template # several times, we open a blank pdf several times instead # (much faster) blank_pdf = PdfFileReader( file("{0}/blank-letter.pdf".format(self.template_dir), "rb") ) final_certificate = blank_pdf.getPage(0) final_certificate.mergePage(self.template_pdf.getPage(0)) final_certificate.mergePage(overlay.getPage(0)) output.addPage(final_certificate) self._ensure_dir(filename) outputStream = file(filename, "wb") output.write(outputStream) outputStream.close() self._generate_verification_page( student_name, filename, verify_dir, verify_uuid, download_url ) return (download_uuid, verify_uuid, download_url)
def parse_link(self, response): item = ArticleItem() title = "" try: title = get_display( arabic_reshaper.reshape(u'' + self.cleanhtml( response.xpath("//*[@class='background_title']/h1/text()"). extract_first()).strip())) except (RuntimeError, TypeError, NameError): pass item["title"] = title author = "" try: author = get_display( arabic_reshaper.reshape(u'' + self.cleanhtml( response.xpath("//h4[@class='gold']/text()").extract_first( )).strip())) except (RuntimeError, TypeError, NameError): pass item["author"] = author item["link"] = response.url description = list() try: description.extend([ get_display(arabic_reshaper.reshape(u'' + self.cleanhtml(d))) for d in response.xpath( "//*[@class='fontstyleDivBody detailedBody']/div/div/div"). extract() ]) except (RuntimeError, TypeError, NameError): pass item["description"] = description comment = list() names = list() feeds = list() try: comment.extend([ get_display(arabic_reshaper.reshape(u'' + self.cleanhtml(d))) for d in response.xpath( "//*[@class='comment-text']/text()").extract() ]) names.extend([ get_display(arabic_reshaper.reshape(u'' + self.cleanhtml(d))) for d in response.xpath( "//*[@class='commenter']/text()").extract() ]) feeds.extend( map(add, [ int(self.cleanhtml(d)) for d in response.xpath( "//*[@class='comments-like clsLike clicked']/text()"). extract() ], [ int(self.cleanhtml(d)) for d in response.xpath( "//*[@class='comments-unlike clsLike clicked']/text()" ).extract() ])) except (RuntimeError, TypeError, NameError): pass item["comments"] = comment item["names"] = names item["feedbacks"] = feeds yield item
def order_and_shape(wc): return bidi.algorithm.get_display(arabic_reshaper.reshape(wc[0])), wc[1]
# a 1080x1920 jpg file imageFile = "./assets/images/input/tweetNegar.jpg" # load the profile photo urllib.request.urlretrieve(profile, "./assets/user/profile0.jpg") # Save Profile Photo from twitter user_profile = Image.open("./assets/user/profile0.jpg") # load profile photo profile = user_profile.resize((180, 180)) # load the font and theme image font = ImageFont.truetype(fontFile, 50) image = Image.open(imageFile) # firts you must prepare your text (you dont need this for english text) username = user_name reshaped_text = arabic_reshaper.reshape(username) # correct its shape bidi_name = get_display(reshaped_text) # tweet content # Content Should Wrap in a Better Way, this is just for test!! my_wrap = textwrap.TextWrapper(width = 30) wrap_list = my_wrap.wrap(text=tweet_content) text = '' reshaped_text = arabic_reshaper.reshape(text) # correct its shape bidi_text = get_display(reshaped_text) # correct its direction # get image size (w, h) = image.size # text location
def save(self, name): reshaped_text = arabic_reshaper.reshape(name) config_file = open("config.cfg", "a") config_file.write(self.id + " " + self.gp + " " + reshaped_text + "\n") config_file.close()
def make_farsi_text(x): reshaped_text = arabic_reshaper.reshape(x) farsi_text = get_display(reshaped_text) return farsi_text
def getPicture(poem,poet): allBGs = glob.glob('/home/ali/Pictures/background/*.*') selected = randint(0,len(allBGs)-1) pic = allBGs[selected] img = Image.open(pic) img= img.resize((800,300)) offset = 200; draw = ImageDraw.Draw(img) sentences = poem maxSize = 0 boundingSize = (550,300) for sent in sentences: if(len(sent) > maxSize): maxSize = len(sent) maxSent = sent t1 = arabic_reshaper.reshape(maxSent.decode('utf-8')) maxSent = get_display(t1) fontSize =2 fontAddress = "/home/ali/Fonts/Mj_Sandbad.ttf" while(1): font = ImageFont.truetype(fontAddress, fontSize) draw.setfont(font) width = draw.textsize(maxSent)[0] height = len(sentences)* draw.textsize(maxSent)[1] if(width<boundingSize[0] and height < boundingSize[1]): fontSize = fontSize+1 continue else: break font = ImageFont.truetype(fontAddress, fontSize-1) draw.setfont(font) i=0 W,H = boundingSize color = (255,255,255) for sent in sentences: t1 = arabic_reshaper.reshape(sent.decode('utf-8')) text = get_display(t1) size = draw.textsize(text) ## draw.text((x, y),"Sample Text",(r,g,b)) draw.text((W/2-size[0]/2+offset, H/2-len(sentences)*size[1]/2-size[1]/4+i*size[1]),text, color) i=i+1 font = ImageFont.truetype(fontAddress, 25) draw.setfont(font) t1 = arabic_reshaper.reshape(poet.decode('utf-8')) ppoet = get_display(t1) size = draw.textsize(ppoet) position = (W/2-size[0]/2,H-size[1]-20) #pixel = img.getpixel(position) #hsv = colorsys.rgb_to_hsv(float(pixel[0])/255,float(pixel[1])/255,float(pixel[2])/255) #rgb = colorsys.hsv_to_rgb(((hsv[0]-.5)*360%360)/360,1-hsv[1],1-hsv[2]) rgb = (1,1,1) draw.text(position,ppoet,(int(255*rgb[0]),int(255*rgb[1]),int(255*rgb[2]))) avatar = getAvatar(poet) avatar = avatar.resize([offset,offset]) img.paste(avatar, (0, img.size[1]-offset), avatar) return img
def run(self, edit): region = sublime.Region(0, self.view.size()) txt = self.view.substr(region) reshaped_text = arabic_reshaper.reshape(txt) bdiText = get_display(reshaped_text) self.view.replace(edit, region, bdiText)