Skip to main content

Posts

Showing posts from January, 2017

How to install matplotlib on xubuntu or Ubuntu

Hi, I have matplotlib, the library to draw graphics with python 3, on windows and I wanted it on linux. I forgot how I made on windows, but the sudo pip install matplotlib went wrong, so I searched on the web and I found this solution that worked for me, so I want to share it with you too. First try this   apt - cache search python3 - matplotlib     If you find it like its available then you can install it from       sudo apt - get install python3 - matplotlib ... I did so and now I got matplotlib on my xubuntu linux version.

Requests 1 - Text scraping in Pyhton

How to install declarativewidgets for Jupyter notebook

Go in the cmd and write pip install jupyter_declarativewidgets

Watermarking images PIL

So, now that we understood the basics of adding circles, arcs and text on an Image created brand new in PIL, withou using any image on the hard drive or elsewhere, let's watermark our image with a different font-size... I also changed some parts of the code that had no sense, due to some changes made making different version of this code... from PIL import Image , ImageDraw , ImageFont # sample text and font unicode_text = u"SMILE WORLD!" # This is used to shape the image around the text size font = ImageFont . truetype ( "consola.ttf" , 28 , encoding = "unic" ) font2 = ImageFont . truetype ( "consola.ttf" , 10 , encoding = "unic" ) # get the line size text_width , text_height = font . getsize ( unicode_text ) # create a blank canvas with extra space between lines # I added 100 to host the image and 30 for the watermark length canvas = Image . new ( 'RGB' , ( text_width + 30 , text_hei

Text and Images in PIL (again)

Let's put some of the last code together and see what happens... smile and text... from PIL import Image , ImageDraw , ImageFont # sample text and font unicode_text = u"Smile World!" font = ImageFont . truetype ( "consola.ttf" , 28 , encoding = "unic" ) # get the line size text_width , text_height = font . getsize ( unicode_text ) # create a blank canvas with extra space between lines canvas = Image . new ( 'RGB' , ( text_width + 10 , text_height + 100 ), "orange" ) # draw the text onto the text canvas, and use black as the text color draw = ImageDraw . Draw ( canvas ) draw . text (( 5 , 5 ), u'Hello World!' , 'blue' , font ) def smile ( x = 30 , y = 30 ): draw = ImageDraw . Draw ( canvas ) draw . ellipse (( 0 + x , 0 + y , 90 + x , 90 + y ), 'yellow' , 'blue' ) # face draw . ellipse (( 25 + x , 20 + y , 35 + x , 30 + y ), 'yellow&

Smiling faces in Python

from PIL import Image from PIL import ImageDraw image = Image . new ( 'RGB' ,( 91 , 91 ), 'blue' ) draw = ImageDraw . Draw ( image ) draw . ellipse (( 0 , 0 , 90 , 90 ), 'yellow' , 'blue' ) # face draw . ellipse (( 25 , 20 , 35 , 30 ), 'yellow' , 'blue' ) # left eye draw . ellipse (( 28 , 25 , 32 , 30 ), 'blue' , 'blue' ) # left ... draw . ellipse (( 50 , 20 , 60 , 30 ), 'yellow' , 'blue' ) # right eye draw . ellipse (( 53 , 25 , 58 , 30 ), 'blue' , 'blue' ) # right ... draw . arc (( 40 , 50 , 50 , 55 ), 0 , 360 , 0 ) # nose draw . arc (( 20 , 40 , 70 , 70 ), 0 , 180 , 0 ) # smile image . show ()

Using glob to create a list of files names

Here's the fast tip for today: 1. See the dir where you are 2. see the files jpg that are in there     >>> import os     >>> import glob     >>> directory = os.getcwd()     >>> files = glob.glob("{}/*.jpg".format(directory)) For python 3.6 you have this new way to format strings     >>> files = glob.glob(f"{directory}/*.jpg") # python 3.6 ... # You have a list of jpg files with full path # To show them in the console     >>> for f in files:         >>> print(f)

How to make a list of the file in a dir with Python

Python 3: for the list of files in the current work directory >>> import os >>> for file in os . listdir (): ... print ( file ) to include them in a list, just: x = [f for f in os.listdir()] If you want to have the list of a particular directory, not the directory where the terminal (or cmd) is: >>> files = os . listdir ( 'G:/python' ) >>> for file in files : print ( file ) >>> x = [ f for f in os . listdir ( 'G:/python' )] Python 2 works differently... >>> import os >>> for f in os . listdir ( os . getcwd ): ... print f >>> # create a list >>> x = [ f for f in os . listdir ( os . getcwd ())] # the current dir The code above will print all the files in the current directory The same here: >>> import os >>> for f in os . listdir ( '.' ): ... print f >>> # create a list

Write an Image with Text in Python

We have tried this before, but now we add the ImageFont class to make this thing better. from PIL import Image , ImageDraw , ImageFont # sample text and font unicode_text = u"Hello World!" font = ImageFont . truetype ( "consola.ttf" , 28 , encoding = "unic" ) # get the line size text_width , text_height = font . getsize ( unicode_text ) # create a blank canvas with extra space between lines canvas = Image . new ( 'RGB' , ( text_width + 10 , text_height + 10 ), "orange" ) # draw the text onto the text canvas, and use black as the text color draw = ImageDraw . Draw ( canvas ) draw . text (( 5 , 5 ), u'Hello World!' , 'blue' , font ) # save the blank canvas to a file #canvas.save("unicode-text.png", "PNG") canvas