Python txt to jpg

destan / text2png.py

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

# coding=utf8
import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
def text2png ( text , fullpath , color = «#000» , bgcolor = «#FFF» , fontfullpath = None , fontsize = 13 , leftpadding = 3 , rightpadding = 3 , width = 200 ):
REPLACEMENT_CHARACTER = u’ \uFFFD ‘
NEWLINE_REPLACEMENT_STRING = ‘ ‘ + REPLACEMENT_CHARACTER + ‘ ‘
#prepare linkback
linkback = «created via http://ourdomain.com»
fontlinkback = ImageFont . truetype ( ‘font.ttf’ , 8 )
linkbackx = fontlinkback . getsize ( linkback )[ 0 ]
linkback_height = fontlinkback . getsize ( linkback )[ 1 ]
#end of linkback
font = ImageFont . load_default () if fontfullpath == None else ImageFont . truetype ( fontfullpath , fontsize )
text = text . replace ( ‘ \n ‘ , NEWLINE_REPLACEMENT_STRING )
lines = []
line = u»»
for word in text . split ():
print word
if word == REPLACEMENT_CHARACTER : #give a blank line
lines . append ( line [ 1 :] ) #slice the white space in the begining of the line
line = u»»
lines . append ( u»» ) #the blank line
elif font . getsize ( line + ‘ ‘ + word )[ 0 ]
line += ‘ ‘ + word
else : #start a new line
lines . append ( line [ 1 :] ) #slice the white space in the begining of the line
line = u»»
#TODO: handle too long words at this point
line += ‘ ‘ + word #for now, assume no word alone can exceed the line width
if len ( line ) != 0 :
lines . append ( line [ 1 :] ) #add the last line
line_height = font . getsize ( text )[ 1 ]
img_height = line_height * ( len ( lines ) + 1 )
img = Image . new ( «RGBA» , ( width , img_height ), bgcolor )
draw = ImageDraw . Draw ( img )
y = 0
for line in lines :
draw . text ( ( leftpadding , y ), line , color , font = font )
y += line_height
# add linkback at the bottom
draw . text ( ( width — linkbackx , img_height — linkback_height ), linkback , color , font = fontlinkback )
img . save ( fullpath )
#show time
text2png ( u»This is \n a \n test şğıöç zaa xd ve lorem hipster» , ‘test.png’ , fontfullpath = «font.ttf» )

Источник

text-to-image 0.0.5

Easily convert your text to grayscale images and vice versa.

With this tool you can encode text or plain text files to a grayscale image to be easily shared. Each pixel represents a single character’s decimal value. When decoding an image you can decode text straight to the console or to a plain text file.

Images use a PNG file extension. The image size will be automatically be set depending on the text length to be encoded.

Text should use a character encoding scheme using 8 bits or less until additional functionality is added to support UTF-8 (16 bit). If a character’s decimal value is greater than the limit (see below) the it will be divided by the limit and the new value used. When the character value equals the limit value the new value will be 1.

The limit value passed using either command line argument -l(–limit) specifies the decimal value limit for pixel values starting from 1. The default is 256 allowing for numbers from 0 to 255 (i.e. 8 bit pixels). If the limit value is greater than 8 bits then the value will still be wrapped around since the output image is grayscale.

Requirements

Install

You can install text_to_image using pip or setup.py

$> pip3 install text_to_image

How to Use

Once installed you can either directly use the encode.py and decode.py file or import and use the relevant functions.

$> python3 encode.py -t image.png  $> python3 encode.py -f my-text-file.txt image.png  $> python3 encode.py --help 
$> python3 decode.py image.png  $> python3 decode.py -f my-text-file.txt image.png  $> python3 decode.py --help 

Tests

To run tests, navigate to the root directory where setup.py is located and run

$> python3-m unittest discover tests -v
TODO:

Источник

Convert Text to PNG, JPEG, or GIF Image in Python

Convert Text to PNG, JPEG, or GIF Image in Python

As a Python developer, you may need text to image conversion in various cases, such as to make the text read-only, to render text, etc. If you are dealing with such a scenario, this article is going to provide you with a free, high-speed and high-quality text to image conversion solution for Python. So let’s see how to convert text to PNG, JPG, or GIF images in Python.

Python Library to Convert Text to Image#

To convert text to images, we will use Aspose.Words for Python. It is a powerful Python library that lets you create and manipulate text documents seamlessly. You can install the library in your Python application from PyPI using the following pip command.

How to Convert Text to Image in Python#

TXT files provide the simplest and easiest way to store plain text without any formatting. Therefore, we will use a TXT file and convert its text into PNG, JPEG, or GIF images.

The following are the steps to convert text to images in Python.

  • Load the text file using the Document class.
  • Loop through all the pages in the document.
  • Extract each page using Document.extract_pages() method.
  • Save page as PNG (or another image format) using Document.save() method.

The following code sample shows how to perform text to image conversion in Python.

Free Python Text to Image Converter#

You can get a free temporary license to convert text to image without evaluation limitations.

Conclusion#

In this article, you have learned how to convert text to image programmatically in Python. You can simply install the library and use the provided code sample to convert the text to PNG, JPEG, or GIF images. In addition, you can learn more about the library using the documentation. Also, you can share your questions or queries via our forum.

See Also#

Источник

Convert Word to JPEG, PNG, or GIF Image File in Python

Convert Word to JPEG, PNG, or GIF Image File in Python

Word is one of the popular formats for sharing and printing documents. We often need to convert word documents to different image formats. It is better to use already developed specialized tools that provide an easily maintainable, flexible conversion solution to your needs. In this article, we will learn how to convert word to JPEG, PNG, or GIF image file in Python.

The following topics shall be covered in this article:

Word to Images Conversion REST API — Python SDK#

For converting JPG, PNG and GIF images in Python, we will be using the Ruby SDK of GroupDocs.Conversion Cloud API. Python SDK of GroupDocs.Conversion provides the best way to convert Word DOCX to JPG, PNG and GIF files in seconds. It is 100% free, secure and easy to use Python SDK for files conversion. It allows converting documents of supported formats to image programmatically on the cloud. You can install it using the following command in the console:

pip install groupdocs_conversion_cloud 

Firstly, get your Client ID and Secret from the dashboard before following the below mentioned steps. Once you have your ID and secret, add below code in your application as shown below:

How to Convert Word to JPEG using REST API in Python#

You can convert Word to JPEG format programmatically by following the steps given below:

  • Firstly, create an instance of the ConvertApi
  • Next, create an instance of the ConvertSettings
  • Then, set the storage name and word file path
  • Set resultant image file format as “jpeg”
  • Create an instance of the DocxLoadOptions
  • Set the word file password and load_options
  • Create an instance of the JpegConvertOptions
  • Define the grayscale, from_page, pages_count, quality, rotate_angle and use_pdf etc
  • Set convertOptions to settings
  • Now, provide the output file path
  • After that, create ConvertDocumentRequest with ConvertSettings as argument
  • Finally, call the ConvertApi.convert_document() method with ConvertDocumentRequest to save the converted file

The following example code demonstrate how to convert word documents to JPEG image format using REST API in Python:

The above code sample will save the converted JPEG file on the cloud.

Convert DOC/DOCX to PNG in Python using REST API#

You can also convert Word Doc/Docx to PNG file format programmatically by following the steps given below:

  • Create an instance of the ConvertApi
  • Next, create an instance of the ConvertSettings
  • Then, set the storage name and word file path
  • Also, set “png” as output image format
  • Create an instance of the DocxLoadOptions
  • Set the word file password and load_options
  • Create an instance of the PngConvertOptions
  • Define the grayscale, from_page, pages_count, quality, rotate_angle and use_pdf values
  • Set convertOptions to settings
  • Now, provide the output file path
  • After that, create ConvertDocumentRequest with ConvertSettings as argument
  • Finally, call the ConvertApi.convert_document() method with ConvertDocumentRequest to save the converted file

The following example code shows how to convert word to PNG format using REST API in Python:

Convert Word DOC/DOCX to GIF in Python using REST API#

You can convert Word Docx to JPG programmatically by following the steps given below:

  • Create an instance of the ConvertApi
  • Next, create an instance of the ConvertSettings
  • Then, set the storage name and word file path
  • Now, provide “gif” as output image format
  • Create an instance of the DocxLoadOptions
  • Set the word file password and load_options
  • Create an instance of the GifConvertOptions
  • Define the grayscale, from_page, pages_count, quality, rotate_angle and use_pdf etc.
  • Set convertOptions to settings
  • Next, provide the output file path
  • After that, create ConvertDocumentRequest with ConvertSettings as argument.
  • Finally, call the ConvertApi.convert_document() method with ConvertDocumentRequest to save the converted file.

The following code sample shows how to convert word Doc/Docx to GIF image file format using REST API in Python:

Online Word to Image Converter for Free#

Please try the following free online JPG, PNG and GIF conversion tool, which is developed using the above API.

Conclusion#

In this article, we have learned how to convert word to image formats on the cloud. Now you know:

  • how to convert word documents to jpeg/jpg in Python;
  • how to convert word doc/docx to png image format using Python;
  • how to convert word doc/docx to gif file format in Python;

You can learn more about GroupDocs.Conversion Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser.

Ask a question#

If you have any questions about word docx to image converter, please feel free to ask us on the Free Support Forum.

See Also#

Источник

Читайте также:  Get string from another string php
Оцените статью