Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
image pricessing using PIL, pillow
################################################################################################
# name: 01_read_image.py
# desc:
# date: 2019-02-10
# Author: conquistadorjd
################################################################################################
from PIL import Image
print('*** Program Started ***')
# image_font_path = 'imagepath_input = '/home/conquistador/code/github/python-01-utilities/image/fonts/'
image_path_input = '/home/conquistador/code/github/python-01-utilities/image/input/'
# image_path_output = '/home/conquistador/code/github/python-01-utilities/image/output/'
image_name_input = '01_read_image.jpg'
im = Image.open(image_path_input + image_name_input)
print('im object: ', im)
print('format : ' , im.format)
print('size : ' , im.size)
print('mode : ' , im.mode)
print('filename : ' , im.filename)
print('width : ' , im.width)
print('height : ' , im.height)
print('info : ' , im.info)
# This will display image on screen
# im.show()
print('*** Program Ended ***')
################################################################################################
# name: 02_create_blank_image.py
# desc: Creates blank image files
# date: 2019-02-10
# Author: conquistadorjd
################################################################################################
from PIL import Image, ImageDraw
print('*** Program Started ***')
# image_font_path = 'imagepath_input = '/home/conquistador/code/github/python-01-utilities/image/fonts/'
# image_path_input = '/home/conquistador/code/github/python-01-utilities/image/input/'
image_path_output = '/home/conquistador/code/github/python-01-utilities/image/output/'
# image_name_input = '02_create_blank_image_01.jpg'
image_name_output = '02_create_blank_image_01.jpg'
mode = 'RGB' # for color image “L” (luminance) for greyscale images, “RGB” for true color images, and “CMYK” for pre-press images.
size = (640, 480)
color = (73, 109, 137)
im = Image.new(mode, size, color)
im.save(image_path_output + image_name_output )
im.show()
print('*** Program Ended ***')
################################################################################################
# name: 03_add_text_to_image_01.py
# desc:
# date: 2019-02-10
# Author: conquistadorjd
################################################################################################
from PIL import Image, ImageDraw, ImageFont
print('*** Program Started ***')
image_font_path = '/home/conquistador/code/github/python-01-utilities/image/fonts/'
image_path_input = '/home/conquistador/code/github/python-01-utilities/image/input/'
image_path_output = '/home/conquistador/code/github/python-01-utilities/image/output/'
image_name_input = '03_add_text_to_image_01_input.jpg'
######################################################################## Writing simple text on file
im = Image.open(image_path_input + image_name_input)
position = (50, 50)
message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
# initialise the drawing context with the image object as background
draw = ImageDraw.Draw(im)
draw.text(position, message)
im.show()
image_name_output = '03_add_text_to_image_01_input_01.jpg'
im.save(image_path_output + image_name_output)
print('*** Program Ended ***')
################################################################################################
# name: 03_add_text_to_image_02.py
# desc:
# date: 2019-02-10
# Author: conquistadorjd
################################################################################################
from PIL import Image, ImageDraw, ImageFont
print('*** Program Started ***')
image_font_path = '/home/conquistador/code/github/python-01-utilities/image/fonts/'
image_path_input = '/home/conquistador/code/github/python-01-utilities/image/input/'
image_path_output = '/home/conquistador/code/github/python-01-utilities/image/output/'
image_name_input = '03_add_text_to_image_01_input.jpg'
im = Image.open(image_path_input + image_name_input)
position = (50, 50)
message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
font = ImageFont.truetype(image_font_path + 'Unkempt-Regular.ttf', size=24)
color = (238, 242, 4)
# initialise the drawing context with the image object as background
draw = ImageDraw.Draw(im)
draw.text(position, message, fill=color, font=font)
im.show()
image_name_output = '03_add_text_to_image_02_input_01.jpg'
im.save(image_path_output + image_name_output)
print('*** Program Ended ***')
################################################################################################
# name: 04_resize_image_01.py
# desc: reducing the file size and keeping same aspect ratio
# date: 2019-02-10
# Author: conquistadorjd
################################################################################################
from PIL import Image, ImageDraw
print('*** Program Started ***')
# image_font_path = 'imagepath_input = '/home/conquistador/code/github/python-01-utilities/image/fonts/'
image_path_input = '/home/conquistador/code/github/python-01-utilities/image/input/'
image_path_output = '/home/conquistador/code/github/python-01-utilities/image/output/'
image_name_input = '04_resize_image_01_input.jpg'
######################################################
im = Image.open(image_path_input + image_name_input)
im_width, im_height = im.size
print('im.size', im.size)
im = im.resize((int(im_width/2), int(im_height/2)), Image.ANTIALIAS)
# im = im.resize((im_width,im_height), Image.LANCZOS)
print('im.size', im.size)
image_name_output = '04_resize_image_01_output.jpg'
im.save(image_path_output + image_name_output)
print('*** Program Ended ***')
################################################################################################
# name: 04_resize_image_02.py
# desc: reducing the file size but changing the aspect ratio
# date: 2019-02-10
# Author: conquistadorjd
################################################################################################
from PIL import Image, ImageDraw
print('*** Program Started ***')
# image_font_path = 'imagepath_input = '/home/conquistador/code/github/python-01-utilities/image/fonts/'
image_path_input = '/home/conquistador/code/github/python-01-utilities/image/input/'
image_path_output = '/home/conquistador/code/github/python-01-utilities/image/output/'
image_name_input = '04_resize_image_01_input.jpg'
######################################################
im = Image.open(image_path_input + image_name_input)
im_width, im_height = im.size
print('im.size', im.size)
im = im.resize(( 500,480 ), Image.ANTIALIAS)
# im = im.resize((im_width,im_height), Image.LANCZOS)
print('im.size', im.size)
image_name_output = '04_resize_image_02_output.jpg'
im.save(image_path_output + image_name_output)
print('*** Program Ended ***')
################################################################################################
# name: 05_compress_image_01.py
# desc: Compress image file using python
# date: 2019-02-10
# Author: conquistadorjd
################################################################################################
from PIL import Image, ImageDraw, ImageFont
import os
print('*** Program Started ***')
image_font_path = '/home/conquistador/code/github/python-01-utilities/image/fonts/'
image_path_input = '/home/conquistador/code/github/python-01-utilities/image/input/'
image_path_output = '/home/conquistador/code/github/python-01-utilities/image/output/'
# image_name_input = '05_compress_image_01_input.png'
image_name_input = '05_compress_image_01_input.jpg'
im = Image.open(image_path_input + image_name_input)
print('Input file size : ', im.size )
print('Input file name : ', image_name_input )
print('Input Image Size : ', os.path.getsize (image_path_input + image_name_input))
# image_name_output = '05_compress_image_01_output.png'
image_name_output = '05_compress_image_01_output.jpg'
im.save(image_path_output + image_name_output ,optimize=True,quality=50)
print('Output file size : ', im.size )
print('Output file name : ', image_name_output)
print('Output Image Size : ', os.path.getsize (image_path_output + image_name_output))
print('*** Program Ended ***')
################################################################################################
# name: 06_crop_image_01.py
# desc:
# date: 2019-02-10
# Author: conquistadorjd
################################################################################################
from PIL import Image, ImageDraw
print('*** Program Started ***')
# image_font_path = 'imagepath_input = '/home/conquistador/code/github/python-01-utilities/image/fonts/'
image_path_input = '/home/conquistador/code/github/python-01-utilities/image/input/'
image_path_output = '/home/conquistador/code/github/python-01-utilities/image/output/'
image_name_input = '06_crop_image_01_input.jpg'
######################################################
im = Image.open(image_path_input + image_name_input)
im_width, im_height = im.size
print('im.size', im.size)
###################################################### Left half of the image
im = im.crop((0, 0, im_width/2,im_height)) # (left, upper, right, lower)-tuple.
image_name_output = '06_crop_image_01_output_01.jpg'
im.save(image_path_output + image_name_output)
print('im.size', im.size)
###################################################### right half of the image
im = Image.open(image_path_input + image_name_input)
im = im.crop((im_width/2, 0, im_width, im_height))
image_name_output = '06_crop_image_01_output_02.jpg'
im.save(image_path_output + image_name_output)
print('im.size', im.size)
###################################################### right top half of the image
im = Image.open(image_path_input + image_name_input)
im = im.crop((im_width/2, 0, im_width, im_height/2))
image_name_output = '06_crop_image_01_output_03.jpg'
im.save(image_path_output + image_name_output)
print('im.size', im.size)
print('*** Program Ended ***')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment