Skip to content

Instantly share code, notes, and snippets.

@conquistadorjd
Last active January 2, 2022 11:33
Show Gist options
  • Save conquistadorjd/2c93de1d95fa1a953c2221efc4420c4b to your computer and use it in GitHub Desktop.
Save conquistadorjd/2c93de1d95fa1a953c2221efc4420c4b to your computer and use it in GitHub Desktop.

Revisions

  1. Pravin revised this gist Feb 11, 2019. 1 changed file with 46 additions and 0 deletions.
    46 changes: 46 additions & 0 deletions 06_crop_image_01.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    ################################################################################################
    # 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 ***')
  2. Pravin revised this gist Feb 11, 2019. 1 changed file with 10 additions and 7 deletions.
    17 changes: 10 additions & 7 deletions 05_compress_image_01.py
    Original file line number Diff line number Diff line change
    @@ -13,18 +13,21 @@
    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'
    # 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'
    # 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('Input file name :', image_name_input )
    print('Output file name :', image_name_output)
    print('Input Image Size : ', os.path.getsize (image_path_input + image_name_input))

    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 ***')
  3. Pravin revised this gist Feb 11, 2019. 1 changed file with 30 additions and 0 deletions.
    30 changes: 30 additions & 0 deletions 05_compress_image_01.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@

    ################################################################################################
    # 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)

    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('Input file name :', image_name_input )
    print('Output file name :', image_name_output)
    print('Input Image Size : ', os.path.getsize (image_path_input + image_name_input))
    print('Output Image Size : ', os.path.getsize (image_path_output + image_name_output))

    print('*** Program Ended ***')
  4. Pravin revised this gist Feb 11, 2019. 2 changed files with 62 additions and 0 deletions.
    31 changes: 31 additions & 0 deletions 04_resize_image_01.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    ################################################################################################
    # 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 ***')
    31 changes: 31 additions & 0 deletions 04_resize_image_02.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    ################################################################################################
    # 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 ***')
  5. Pravin revised this gist Feb 11, 2019. 2 changed files with 63 additions and 0 deletions.
    30 changes: 30 additions & 0 deletions 03_add_text_to_image_01.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@

    ################################################################################################
    # 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 ***')
    33 changes: 33 additions & 0 deletions 03_add_text_to_image_02.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@

    ################################################################################################
    # 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 ***')
  6. Pravin revised this gist Feb 11, 2019. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions 02_create_blank_image.py
    Original file line number Diff line number Diff line change
    @@ -11,16 +11,17 @@
    # 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_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_input )
    im.save(image_path_output + image_name_output )

    im.show()

    print('*** Program Ended ***')
    print('*** Program Ended ***')
  7. Pravin revised this gist Feb 11, 2019. 1 changed file with 26 additions and 0 deletions.
    26 changes: 26 additions & 0 deletions 02_create_blank_image.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    ################################################################################################
    # 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'

    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_input )

    im.show()

    print('*** Program Ended ***')
  8. Pravin revised this gist Feb 11, 2019. 1 changed file with 10 additions and 3 deletions.
    13 changes: 10 additions & 3 deletions 01_read_image.py
    Original file line number Diff line number Diff line change
    @@ -12,13 +12,20 @@
    # 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 = '20170708-sky-1873934_1920.jpg'
    image_name_input = '01_read_image.jpg'

    im = Image.open(image_path_input + image_name_input)

    print('im : ',im)
    print('Attributes : ', im.format, im.size, im.mode,im.filename, im.width, im.height, im.info)
    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 ***')
  9. Pravin created this gist Feb 11, 2019.
    1 change: 1 addition & 0 deletions 0-image-processing
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    image processing
    24 changes: 24 additions & 0 deletions 01_read_image.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    ################################################################################################
    # 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 = '20170708-sky-1873934_1920.jpg'

    im = Image.open(image_path_input + image_name_input)

    print('im : ',im)
    print('Attributes : ', im.format, im.size, im.mode,im.filename, im.width, im.height, im.info)

    # im.show()

    print('*** Program Ended ***')