async def default_html(call: CallbackQuery): """Converting image to html, and taking screenshot of html page""" await call.answer(cache_time=3) username = call.from_user.username await call.message.edit_text('Wait some seconds...') output = ascii_magic.from_image_file( f'docs/{username}/picture.png', columns=1000, mode=ascii_magic.Modes.HTML) # Customization for default html doc ascii_magic.to_html_file(f'docs/{username}/ascii.html', output) # Reversing from image to html imgkit.from_file(f'docs/{username}/ascii.html', f'docs/{username}/html.png') # Screenshot from html page ########################################################################################### # Cropping Image # image = Image.open(f'docs/{username}/html.png') width, height = image.size # Cropping coordinates left = 8 top = 16 right = width bottom = height - top # Cropped image of above dimension im = image.crop((left, top, right, bottom)) # Shows the image in image viewer im.save(f'docs/{username}/html.png') ########################################################################################### size_b = os.path.getsize( f'docs/{username}/html.png') # Size of screenshot in bytes size_mb = size_b / 1_000_000 # Size of screenshot in mb await call.message.edit_text('Done! Sending ...') 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}/html.png')) await call.message.answer_document( InputFile(f'docs/{username}/html.png')) await call.message.answer_document( InputFile(f'docs/{username}/ascii.html')) else: await call.message.answer( 'So big size of photo. Sending only photo-document...') await call.message.answer_document( InputFile(f'docs/{username}/html.png')) await call.message.answer_document( InputFile(f'docs/{username}/ascii.html')) await call.message.delete() shutil.rmtree(f'docs/{username}/') # Removing dir with user files7
import ascii_magic output = ascii_magic.from_image_file('img/py2.jpg', mode=ascii_magic.Modes.HTML) ascii_magic.to_html_file('nicholas2.html', output,additional_styles='background: #222;')
async def set_ratio(message: Message, state: FSMContext): await state.update_data(ratio=message.text) username = message.from_user.username data = await state.get_data() columns = data.get("size") ratio = data.get("ratio") try: if float(ratio) < 0 or float( ratio) > 10: # If user entered incorrect number await message.answer("Chosen default ratio") await message.answer('Wait some seconds...') output = ascii_magic.from_image_file( f'docs/{username}/picture.png', columns=int(columns), mode=ascii_magic.Modes.HTML ) # Customization for default html doc ascii_magic.to_html_file(f'docs/{username}/ascii.html', output) # Reversing from image to html imgkit.from_file( f'docs/{username}/ascii.html', f'docs/{username}/html.png') # Screenshot from html page ########################################################################################### # Cropping Image # image = Image.open(f'docs/{username}/html.png') width, height = image.size # Cropping coordinates left = 8 top = 16 right = width bottom = height - top # Cropped image of above dimension im = image.crop((left, top, right, bottom)) # Shows the image in image viewer im.save(f'docs/{username}/html.png') ########################################################################################### size_b = os.path.getsize( f'docs/{username}/html.png') # Size of screenshot in bytes size_mb = size_b / 1_000_000 # Size of screenshot in mb await message.answer('Done! Sending ...') if size_mb < 10: # restriction of Telegram. Image can't be more than 10mb await message.answer_photo( InputFile(path_or_bytesio=f'docs/{username}/html.png')) await message.answer_document( InputFile(f'docs/{username}/html.png')) await message.answer_document( InputFile(f'docs/{username}/ascii.html')) else: await message.answer( 'So big size of photo. Sending only photo-document...') await message.answer_document( InputFile(f'docs/{username}/html.png')) await message.answer_document( InputFile(f'docs/{username}/ascii.html')) await message.delete() shutil.rmtree(f'docs/{username}/') # Removing dir with user files else: # If user entered correct number await message.answer("Chosen default ratio") await message.answer('Wait some seconds...') output = ascii_magic.from_image_file( f'docs/{username}/picture.png', columns=int(columns), width_ratio=float(ratio), mode=ascii_magic.Modes.HTML ) # Customization for default html doc ascii_magic.to_html_file(f'docs/{username}/ascii.html', output) # Reversing from image to html imgkit.from_file( f'docs/{username}/ascii.html', f'docs/{username}/html.png') # Screenshot from html page ########################################################################################### # Cropping Image # image = Image.open(f'docs/{username}/html.png') width, height = image.size # Cropping coordinates left = 8 top = 16 right = width bottom = height - top # Cropped image of above dimension im = image.crop((left, top, right, bottom)) # Shows the image in image viewer im.save(f'docs/{username}/html.png') ########################################################################################### size_b = os.path.getsize( f'docs/{username}/html.png') # Size of screenshot in bytes size_mb = size_b / 1_000_000 # Size of screenshot in mb await message.answer('Done! Sending ...') if size_mb < 10: # restriction of Telegram. Image can't be more than 10mb await message.answer_photo( InputFile(path_or_bytesio=f'docs/{username}/html.png')) await message.answer_document( InputFile(f'docs/{username}/html.png')) await message.answer_document( InputFile(f'docs/{username}/ascii.html')) else: await message.answer( 'So big size of photo. Sending only photo-document...') await message.answer_document( InputFile(f'docs/{username}/html.png')) await message.answer_document( InputFile(f'docs/{username}/ascii.html')) await message.delete() shutil.rmtree(f'docs/{username}/') # Removing dir with user files except ValueError: # If user entered characters instead digits await message.answer("Chosen default ratio") await message.answer('Wait some seconds...') output = ascii_magic.from_image_file( f'docs/{username}/picture.png', columns=int(columns), mode=ascii_magic.Modes.HTML) # Customization for default html doc ascii_magic.to_html_file(f'docs/{username}/ascii.html', output) # Reversing from image to html imgkit.from_file( f'docs/{username}/ascii.html', f'docs/{username}/html.png') # Screenshot from html page ########################################################################################### # Cropping Image # image = Image.open(f'docs/{username}/html.png') width, height = image.size # Cropping coordinates left = 8 top = 16 right = width bottom = height - top # Cropped image of above dimension im = image.crop((left, top, right, bottom)) # Shows the image in image viewer im.save(f'docs/{username}/html.png') ########################################################################################### size_b = os.path.getsize( f'docs/{username}/html.png') # Size of screenshot in bytes size_mb = size_b / 1_000_000 # Size of screenshot in mb await message.answer('Done! Sending ...') if size_mb < 10: # restriction of Telegram. Image can't be more than 10mb await message.answer_photo( InputFile(path_or_bytesio=f'docs/{username}/html.png')) await message.answer_document( InputFile(f'docs/{username}/html.png')) await message.answer_document( InputFile(f'docs/{username}/ascii.html')) else: await message.answer( 'So big size of photo. Sending only photo-document...') await message.answer_document( InputFile(f'docs/{username}/html.png')) await message.answer_document( InputFile(f'docs/{username}/ascii.html')) await message.delete() shutil.rmtree(f'docs/{username}/') # Removing dir with user files await state.finish()