Image format in Python

Create an image format program using Pillow in Python3.3 Get to get width and height automatically

# ==================================================
#Image format
#
# -Specify the file name with the first argument
# -Specify the conversion format with the second argument
# ==================================================

# encoding: UTF-8

from PIL import Image   #Image conversion module
import os, re, sys      #File Path,Regular expression module,Because it uses command line arguments

###
# Main
#
def main():
    #Argument check
    if len(sys.argv) > 4:
        fileName = sys.argv[1]
        format   = sys.argv[2]

        #File open
        img = Image.open(fileName, "r")
        width, height = img.size

        #Get before the file name extension,Change to file name after formatting
        fileName = re.search("(?<!\.)\w+", fileName).group(0) + "." + format

        #File existence check
        flag = os.path.exists(fileName)
        if flag == True:
            print("The file already exists.")
            sys.exit()

        #Create and paste a canvas to paste the image on
        canvas = Image.new("RGB", (width, height), (255, 255, 255))
        canvas.paste(img, (0, 0))

        #Save image
        canvas.save(fileName, returnFormat(format), quality=100, optimize=True)

    else:
        print("There are too few arguments.\n Specify the file name and the format of the file to be converted.\
            \n * Specify the format in lowercase.\
            \n examples) python imgf.py fileName.jpg bmp 100 100")

###
# returnFormat()
#Returns the passed format in uppercase
#
def returnFormat(format):
    if format == "bmp":
        return "BMP"
    elif format == "jpg":
        return "JPEG"
    elif format == "png":
        return "PNG"
    elif format == "gif":
        return "GIF"
    else:
        print(format + "Does not support.")
        sys.exit()

if __name__ == "__main__":
    main()

Reference site http://librabuch.jp/2013/05/python_pillow_pil/ http://d.hatena.ne.jp/fgshun/20080922/1222095288

Recommended Posts

Image format in Python
format in python
Easily format JSON in Python
Tweet with image in Python
Image Processing Collection in Python
Implemented image segmentation in python (Union-Find)
Automatically format Python code in Vim
Handle GDS II format in Python
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
Python string format
SendKeys in Python
python image processing
Meta-analysis in Python
Unittest in python
Discord in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Python string format
Constant in python
nCr in Python.
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
How to adjust image contrast in Python
Easy image processing in Python with Pillow
Image sending / receiving memo in Python (Flask)
CG image quality evaluation memo in Python
Download files in any format using Python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python