def crawling_dengue_data( url='https://data.gov.sg/dataset/e7536645-6126-4358-b959-a02b22c6c473/download' ): global today resp = requests.get(url).content zipfile = ZipFile(BytesIO(resp)) dengue_data = json.load( zipfile.open('dengue-clusters-geojson.geojson', 'r')) dengue_status = {'LOCATION': [], 'CASE_SIZE': []} today = datetime.today().replace(microsecond=0) pickle.dump(today, open('./today.obj', 'wb')) for feature in dengue_data['features']: soup = BS(feature['properties']['Description'], 'html.parser') iterator = soup.table.children while True: child = next(iterator) if child.th.string == 'LOCALITY': dengue_status['LOCATION'].append(child.td.string) break while True: child = next(iterator) if child.th.string == 'CASE_SIZE': dengue_status['CASE_SIZE'].append(child.td.string) break dengue_status = pd.DataFrame(dengue_status) text_file = open('./dengue_status.html', 'w+') text_file.write(css) text_file.write(dengue_status.to_html()) text_file.close() imgkit.from_file("./dengue_status.html", "./dengue_status.png", options=imgkitoptions)
async def subroutine_markdown_to_image(self, msg: discord.Message, args): if len(msg.attachments) == 0: msg.channel.send(":error: Nothing to expand") else: GRIP_TOK = os.environ.get("MESSAGE_UTILITY_BOT_GRIP_TOKEN", "") attach = msg.attachments[0] content = requests.get(attach.url).text title = attach.filename if len(args) == 0 else " ".join(args) tmpfilepath = tempfile.mktemp() with open(tmpfilepath, "w") as tmpfile: tmpfile.write(content) html_tmp_filepath = tempfile.mktemp(suffix=".html") subprocess.call([ "grip", "--title", title, "--pass", GRIP_TOK, "--export", tmpfilepath, html_tmp_filepath ]) img_tmp_filepath = tempfile.mktemp(suffix=".jpg") imgkit.from_file(html_tmp_filepath, img_tmp_filepath) file = discord.File(img_tmp_filepath) await msg.channel.send(title, file=file) await msg.delete() os.remove(tmpfilepath) os.remove(html_tmp_filepath) os.remove(img_tmp_filepath)
def DataFrame_to_image(data, css=css, outputfile="financial_data.png", format="png"): fn = "filename.html" try: os.remove(fn) except: None # text_file = codecs.open(fn, "ba", encoding='utf-8', errors='ignore') text_file = open(fn, "a") #text_file =decode("utf-8", "ignore") # write the CSS text_file.write(css) # write the HTML-ized Pandas DataFrame text_file.write(data.to_html(index=False)) text_file.close() imgkitoptions = {"format": format} path_for_wkhtml = r'wkhtmltopdf\wkhtmltoimage.exe' config = imgkit.config(wkhtmltoimage=path_for_wkhtml) imgkit.from_file(fn, outputfile, options=imgkitoptions, config=config) os.remove(fn)
def create_video_from_file(repository_path, file_path, branch, output, tmp_dir): """ Generate a video from a file git history on a given branch of a given local repository. """ file_name_without_ext, ext = splitext(os.path.basename(file_path)) os.makedirs(tmp_dir, exist_ok=True) file_history = get_file_history(repository_path, file_path, branch) file_generated_history = generate_all_sub_versions_from_list(file_history) images_paths = [] for index, file_content in enumerate(tqdm(file_generated_history, desc='Generating images')): html_file_path = os.path.join(tmp_dir, f'{file_name_without_ext}_{index}.html') css_file_path = os.path.join(tmp_dir, f'{file_name_without_ext}_{index}.css') image_file_path = os.path.join(tmp_dir, f'{file_name_without_ext}_{index}.png') code_to_html(file_content, file_path, html_file_path, css_file_path) imgkit.from_file( str(html_file_path), str(image_file_path), options={ 'quiet': 1, 'width': 1, # `width` option is handled as a min_width. Setting a value of 1 instead of 1024 default value allows to # output an image with the smallest width still containing all html content. } ) images_paths.append(str(image_file_path)) images_to_video(images_paths, output) click.launch(output)
def create_base_table(title, x_list, data_list): headers = [" "] + x_list rows = [] for key in data_list.keys(): #print(key) row = [] row.append(key) for x in data_list[key]: row.append(x) #print(row) rows.append(row) table = Table() table.add(headers, rows) table.set_global_opts( title_opts=opts.ComponentTitleOpts(title=title) ) src_path = "./test/" html_file_name = src_path + title + ".html" img_file_name = src_path + title + ".png" table.render(html_file_name) path_wkimg = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltoimage.exe' # 工具路径 cfg = imgkit.config(wkhtmltoimage=path_wkimg) imgkit.from_file(html_file_name, img_file_name, config=cfg) #make_snapshot(snapshot, table.render(html_file_name), img_file_name) print(img_file_name+"生成完毕...")
def mostrartablaTTindiv(): scope = ['https://spreadsheets.google.com/feeds'] credentials = ServiceAccountCredentials.from_json_keyfile_name( 'CEMK-0ed44d25f869.json', scope) gc = gspread.authorize(credentials) wks = gc.open("TTMontewario").sheet1 val = wks.get_all_values() val.sort(key=lambda x: x[3]) table = BeautifulTable() table.column_headers = ["Fecha", "Jugador", "Equipo", "Tiempo"] jugadores = [] tablita = [] for x in range(0, len(val) - 1): if not str(val[x][1]) in jugadores: tablita.append([ str(val[x][0]), str(val[x][1]), str(val[x][2]), cargar(val[x][3]) ]) jugadores.append(str(val[x][1])) savehtml(tablita) options = {'xvfb': ''} imgkit.from_file('Tablita.html', 'Tablita.jpg', options=options)
async def customization_ascii(call: CallbackQuery): await call.answer(cache_time=3) username = call.from_user.username await call.message.edit_text('Wait some seconds...') # Customization for default ascii output = ascii_magic.from_image_file(f'docs/{username}/picture.png', columns=129, mode=ascii_magic.Modes.ASCII) with open(f'docs/{username}/ascii.txt', "w") as f: f.write(output) # txt to to png imgkit.from_file(f'docs/{username}/ascii.txt', f'docs/{username}/ascii.png') await call.message.edit_text('Done! Sending ...') size_b = os.path.getsize(f'docs/{username}/ascii.png') # Size of screenshot in bytes size_mb = size_b / 1_000_000 # Size of screenshot in mb if size_mb < 10: # restriction of Telegram. Image can't be more than 10mb await call.message.answer_photo(InputFile(path_or_bytesio=f'docs/{username}/ascii.png')) await call.message.answer_document(InputFile(f'docs/{username}/ascii.png')) await call.message.answer_document(InputFile(f'docs/{username}/ascii.txt')) else: await call.message.answer('So big size of photo. Sending only photo-file...') await call.message.answer_document(InputFile(f'docs/{username}/ascii.png')) await call.message.answer_document(InputFile(f'docs/{username}/ascii.txt')) await call.message.delete() shutil.rmtree(f'docs/{username}/') # Removing dir with user files
def createScreenshotFromHtmlFile(fileNames, index): for filename in fileNames[index]: print(filename) try: imgkit.from_file("markup/" + filename, 'img/' + filename + '.jpg') except Exception as e: print(e)
def generateImage(): options = {'format': 'jpg', 'quiet': ''} imgkit.from_file('page/index.html', 'page/calendar.jpg', options=options) return True
def DataFrame_to_image(data, css, outputfile="weekly_mbc_growth.png", format="png"): ''' For rendering a Pandas DataFrame as an image. data: a pandas DataFrame css: a string containing rules for styling the output table. This must contain both the opening an closing <style> tags. *outputimage: filename for saving of generated image *format: output format, as supported by IMGKit. Default is "png" ''' fn = str(random.random() * 100000000).split(".")[0] + ".html" try: os.remove(fn) except: None text_file = open(fn, "a") # write the CSS text_file.write(css) # write the HTML-ized Pandas DataFrame text_file.write(data.to_html()) text_file.close() # See IMGKit options for full configuration, # e.g. cropping of final image imgkitoptions = {"format": format} imgkit.from_file(fn, outputfile, options=imgkitoptions) os.remove(fn)
def fq(avatar, username, timestamp, content, max_width=400, scale=1.2, color='#dcddde'): dir_path = change_cwd() inject_js(avatar, username, color, timestamp, content, max_width) # 設定wkhtmltoimage程式位置 config = imgkit.config(wkhtmltoimage=path.join(dir_path, 'wkhtmltoimage.exe')) options = { 'format': 'png', 'encoding': 'UTF-8', 'quality': '100', 'transparent': '', 'zoom': str(scale), 'quiet': '', 'crop-y': '0', } output = 'out.png' imgkit.from_file(filename='sample.html', output_path=output, config=config, options=options) # 調用47行會在終端出現一行字,目前找不到解法 # 已經試過調用stdout跟print('\r')等 im = Image.open(output) size = (0, 3, im.size[0], im.size[1]-3) im = im.crop(size) im2 = im.crop(im.getbbox()) # 去除透明部分 im2.save(output) result = open(output, 'rb') # 返回byte形式檔案,方便傳送至Discord return result
def simple_example(): #imgkit.from_url('http://google.com', './out.jpg') #imgkit.from_file('./test.html', './out.jpg') imgkit.from_string('Hello!', './out.jpg') #imgkit.from_string(html_code_str, './out.jpg') input_filepath, output_filepath = './file.html', './file.jpg' try: with open(input_filepath) as fd: imgkit.from_file(fd, output_filepath) except UnicodeDecodeError as ex: print('Unicode decode error in {}: {}.'.format(input_filepath, ex)) except FileNotFoundError as ex: print('File not found, {}: {}.'.format(input_filepath, ex)) #-------------------- # Use False instead of output path to save PDF to a variable. #img_bytes = imgkit.from_url('http://google.com', False) #img_bytes = imgkit.from_file('./test.html', False) img_bytes = imgkit.from_string('Hello!', False) #img_bytes = imgkit.from_string(html_code_str, False) img = PIL.Image.open(io.BytesIO(img_bytes)) plt.figure() plt.imshow(img) plt.tight_layout() plt.axis('off') plt.show()
def DataFrame_to_image(data, css=css, outputfile=TLG_IMAGE_PATH, format="png"): ''' For rendering a Pandas DataFrame as an image. data: a pandas DataFrame css: a string containing rules for styling the output table. This must contain both the opening an closing <style> tags. *outputimage: filename for saving of generated image *format: output format, as supported by IMGKit. Default is "png" ''' fn = str(random.random() * 100000000).split(".")[0] + ".html" try: os.remove(fn) except Exception: pass text_file = open(fn, "a") # write the CSS text_file.write(css) # write the HTML-ized Pandas DataFrame text_file.write(data.to_html()) text_file.close() # See IMGKit options for full configuration, # e.g. cropping of final image imgkitoptions = {"format": format} imgkit.from_file(fn, outputfile, options=imgkitoptions) os.remove(fn) img = Image.open(outputfile) width, height = img.size img = img.resize((int(width / 1.2), int(height / 1.2)), Image.ANTIALIAS) img.save(outputfile) return outputfile
def write_html(html, outfile, infile="schedule.html", css=None, format="png"): if not css: css = default_css with open(infile, "w") as text_file: text_file.write("<style>" + css + "</style>\n") text_file.write(html) outfile = os.path.join(BASE_PATH, outfile) if format == "png": if not DEBUG: imgkitoptions = {"format": "png", "xvfb": ""} imgkit.from_file(infile, outfile, options=imgkitoptions) else: imgkitoptions = {"format": "png"} imgkit.from_file(infile, outfile, options=imgkitoptions) elif format == "pdf": options = {"page-width": "210", "page-height": "400"} if not DEBUG: pdfkitconfig = pdfkit.configuration(wkhtmltopdf="./wkhtmltopdf.sh") pdfkit.from_file(infile, outfile, configuration=pdfkitconfig, options=options) else: pdfkit.from_file(infile, outfile, options=options)
def refresh_thumbs(user): """Refresh the thumbnail images for the writer homepage. Arguments: user {str} -- The user to refresh the thumbs for. Passing 'all' will update all users thumbnails. """ if user != "all": WRITER_PATH = "userdata/{}/writer/documents".format(user) THUMB_PATH = "userdata/{}/writer/thumbnails".format(user) # Windows needs executable path to be specified. if os.name == "nt": config = imgkit.config( wkhtmltoimage= b"C:/Program Files/wkhtmltopdf/bin/wkhtmltoimage.exe") else: config = imgkit.config() # xvfb is needed by Linux if os.name == "posix": options.update(xvfb="") files = os.listdir(WRITER_PATH) for i in files: # Don't create a thumbnail for the backup folder. if i != "oopsie": imgkit.from_file( WRITER_PATH + "/" + i, THUMB_PATH + "/" + i + "_thumb.png", options=options, config=config, ) else: # List every user and recursively refresh thumbnails. users = os.listdir("userdata/") for user in users: refresh_thumbs(user)
def main(): # Let's use the iris dataset for our test runs df = pd.read_csv( 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', header=None) # Function only does numerical columns (this could be changed) # Notice in figure table is too dense to read for big tables # If there are long column/index names Matplotlib tables will not display correctly even for small tables checkerboard_table(df.loc[:, 0:3]) plt.savefig('matplotlibTable.png') # Alternative is to create an html file and convert that an images # This will keep it looking good/legible but may be a mute point if you just keep your tables small # This method is preferred/needed if column titles are long/descriptive df.to_html('table.html') # Wrapper is needed for imgemagick # html = pt.to_html() # htmlfile = open('table.html', 'w') # htmlfile.write('<html>'+html+'</html>') # htmlfile.close() # wkhtmltoimage/wkhtmltopdf simple program to convert html to an alternative filters # from command line, gives crisp image subprocess.call( 'wkhtmltoimage -f png --width 0 table.html wkhtmltoimage.png', shell=True) # image has extra white space (can fix this with four point transform) imgkit.from_file('table.html', 'imgkit.png')
def getJPG(self,file): html = self.getHtml() with open('table.html', 'w') as f: f.write(html) f.close() pass imgkit.from_file('table.html',file) return
def createimage(html): f = open('image.html', 'w') f.write(html) f.close() options = {'format': 'jpg', 'crop-w': '320'} imgkit.from_file('image.html', 'out.jpg', options=options)
def convert(self, name=None): """Convert the html file into a .jpg file""" if not name: jpg_file_name = self.html_file.split('.')[0] + '.jpg' print(f'No name given. Using {jpg_file_name}') else: jpg_file_name = name imgkit.from_file(self.html_file, jpg_file_name)
def generate_match_score_img(teams, match, matchday, match_number): with open("scores/{}-{}.html".format(match["Local"], match["Visitante"]), "w") as file: file.write('<meta charset="UTF-8">\n') file.write(data.SCORE_HTML.format(round=matchday, game=match_number, home_src="../{}".format(teams.loc[match["Local"]]["Escudo"]), away_src="../{}".format(teams.loc[match["Visitante"]]["Escudo"]), home_score=match["HScore"], away_score=match["AScore"], home_team=match["Local"], away_team=match["Visitante"], home_twitter=teams.loc[match["Local"]]["Twitter"], away_twitter=teams.loc[match["Visitante"]]["Twitter"])) file.close() options = {'format': 'jpg', 'width': 1440, 'disable-smart-width': ''} imgkit.from_file("scores/{}-{}.html".format(match["Local"], match["Visitante"]), "scores/{}-{}.jpg".format(match["Local"], match["Visitante"]), options=options) return "scores/{}-{}.jpg".format(match["Local"], match["Visitante"])
def createScreenshotFromHtmlFile(fileNames, index): for filename in fileNames[index]: print(filename) file_out = filename.replace(".html", "") try: imgkit.from_file(args.folder_path_html + filename, args.folder_path_out + file_out + FILE_TYPE) except Exception as e: print(e)
def get_menu_image(trigger_data, customer): try: print("_______Get Menu_________") trigger_name = trigger_data.trigger_name dealer = trigger_data.dealer html = "<html><body style='color:red;'><div style='width:90%;margin:5%; auto;'><table style='width:100%;color:#191919;font-size:22px;' border=1><h2 style='color:#191919;'>" + str( trigger_name ) + "</h2><tr><th style='width:20%;background-color:#191919;color:#c1c1c1;padding:1%;'>Item Number</th><th style='background-color:#191919;color:#c1c1c1;padding:1%;'>Name</th><th style='width:20%;background-color:#191919;color:#c1c1c1;padding:1%;'>Price</th></tr>" menu_list = MenuItems.objects.filter(dealer=dealer) #==========================================# # Logic to set item number always 1,2,3.. # #========================================# item_number = 0 menu_dict = {} for menu in menu_list: if menu.quantity_available > 0 and menu.item_price > 0: item_number = item_number + 1 menu_dict[item_number] = menu.id html += "<tr><td style='padding:1%;'>" + str( item_number ) + "</td><td style='padding-left:10px;'>" + str( menu.item_name ) + "</td><td style='padding-left:10px;'>" + str( menu.item_price) + "</td></tr>" pass html += "</table></div></body></html>" menu_custom_obj, created = MenuCustomerMappping.objects.get_or_create( trigger=trigger_data, dealer=dealer, customer=customer) menu_custom_obj.menu_data = menu_dict menu_custom_obj.save() #=================================================================# # return False, If no menu item have quantinty greater than zero # #===============================================================# if item_number <= 0: return False Html_file = open("test.html", "w") Html_file.write(html) Html_file.close() try: imgkit.from_file('test.html', 'out.jpg') except: #pass imgkit exception pass file = open('out.jpg') image_file = File(file) image_obj, created = MenuListImages.objects.get_or_create( trigger=trigger_data, dealer=dealer) image_obj.image = image_file image_obj.save() return image_obj except: return False
def generate_standing_image(standing, matchday_number): file = open("standings/{}.html".format(matchday_number), "w") file.write('<meta charset="UTF-{}">\n'.format(8)) file.write(data.CSS) file.write(standing.to_html(index=True)) file.close() options = {'format': 'jpg', 'width': 420, 'disable-smart-width': ''} imgkit.from_file('standings/{}.html'.format(matchday_number), 'standings/{}.jpg'.format(matchday_number), options=options) return 'standings/{}.jpg'.format(matchday_number)
def generate_next_matchday_img(next_matchday, next_matchday_number): file = open("next-matchday/{}.html".format(next_matchday_number), "w") file.write('<meta charset="UTF-{}">\n'.format(8)) file.write(data.CSS) file.write(next_matchday.to_html(index=False)) file.close() options = {'format': 'jpg', 'width': 420, 'disable-smart-width': ''} imgkit.from_file('next-matchday/{}.html'.format(next_matchday_number), 'next-matchday/{}.jpg'.format(next_matchday_number), options=options) return 'next-matchday/{}.jpg'.format(next_matchday_number)
def mdx2jpg(mdx_name, input_name, output_name, invalid_action=InvalidAction.Collect): mdx2html(mdx_name, input_name, TEMP_FILE, invalid_action, False) imgkit.from_file(TEMP_FILE, output_name, options={'enable-local-file-access': ''}) os.remove(TEMP_FILE)
def htmltoimage_edge(self, fill_color): if os.path.isfile(self.fill_color(fill_color) + '_edge.html'): imgkit.from_file( self.fill_color(fill_color) + '_edge.html', self.fill_color(fill_color) + '_edge.jpg') os.system('rename ' + self.fill_color(fill_color) + '_edge.jpg ' + self.fill_color_rename(fill_color) + '_edge.jpg') else: pass
def html_image(html_list, image_path): index = 0 for i in html_list: img_obj = image_path + str(index) + ".png" with open(i, 'r') as html_file: imgkit.from_file(html_file, img_obj, options={"encoding": "UTF-8"}) index += 1
def generate_jpeg(filename, **kwargs): outfile = "../data/jpeg/{}.jpeg".format(filename) infile = "../data/html_jpeg/{}.html".format(filename) try: imgkit.from_file(infile, outfile, **kwargs) except Exception as ex: logger.warning("Exception caught for the file {}.\n" "Exception = {}".format(filename, ex))
def render_output(self, content, blobs): content = content.get_format("html") os.makedirs(self._render_dir, exist_ok=True) report_file = os.path.join(self._render_dir, "index.html") with open(report_file, "w") as f: f.write(content) for blob in blobs: blob_path = os.path.join(self._render_dir, blob.id) os.makedirs(os.path.dirname(blob_path), exist_ok=True) with open(blob_path, "wb") as f: f.write(blob.content.getvalue()) if self.output_format == "html": output_paths = [ os.path.join(self.output_dir, self.report.id), # Also write to "latest" alias os.path.join(self.output_dir, f"latest-{self.report.title_slug}"), ] for path in output_paths: _copy(self._render_dir, path) elif self.output_format == "png": output_file = os.path.join(self._render_dir, f"{self.report.id}.png") theme = self.report.theme # It works better to render at a larger width and then # crop down, don't ask me why. The fonts render at strange # sizes otherwise. width = 1024 crop_width = (theme.num_columns * theme.column_width) + (theme.padding_width * 2) imgkit_options = { "width": width, # Crop center portion of rendered image "crop-x": int((width - crop_width) / 2), "crop-w": int(crop_width), "crop-y": int(theme.padding_width), "format": "png", } if self._has_xvfb: imgkit_options["xvfb"] = "" with open(report_file, "r") as f: imgkit.from_file(f, output_file, options=imgkit_options) output_paths = [ os.path.join(self.output_dir, f"{self.report.id}.png"), os.path.join(self.output_dir, f"latest-{self.report.title_slug}.png"), ] for path in output_paths: _copy(output_file, path) self._cleanup()
def msImage(): options = { "enable-local-file-access": "", "encoding": "UTF-8", "quiet": "" } imgkit.from_file('milestone/milestone.html', 'milestone.png', options=options)