I want to exe and distribute a program that resizes images Python3 + pyinstaller

Planning / Overview

--I want to run a python3 program on a PC without python3 ――If you use the software, it's a forbidden phrase (it makes sense to write it) --It seems that you can make it into an exe by using pyinstaller

environment

Run

Let's try it now. Enter the following at the command prompt to install.

pip install pyinstaller

Program to be exe

What I wrote this time is a program to resize images. You don't have to copy it because you're writing something that you can do in an instant with free software. Please use your favorite program.

resize.py


#! python3
# -*- coding: utf-8 -*-
from PIL import Image
import sys


print("Resize the image")

try:
    inp_img = str(input("Enter the file name:"))
    filename = "picture/" + inp_img + ".jpg "
    img = Image.open(filename, "r")
except FileNotFoundError:
    print("[Error] File not found!\n"
          "Put the file in the picture folder")
    exit()

try:
    inp_a = int(input("Vertical:"))
    inp_b = int(input("side:"))
except ValueError:
    print("[Error] Please enter half-width numbers")
    exit()

resize_img = img.resize((inp_a, inp_b))

# 2017/07/21
# .I have to delete the jpg'pic01.jpg_resize2.jpg'become that way
filename = filename.replace(".jpg ", "")
filename = filename + "_resize2.jpg "

resize_img.save(filename, "JPEG", quality=100, optimize=True)
print("【result】: " + filename)

I read that thumbnails can be used to resize beautifully, so I tried it, but I gave up because it was not resized to the specified size (see the code as a bonus). If it is resize (), it will be the specified size.

FileNotFoundError is the error when the file is not found. Value Error is an error countermeasure when you enter a value other than a number.

You can improve the quality by increasing the quality number.

Resize the image
Enter the file name:pic01
Vertical:100
side:200
【result】: picture/pic02_resize2.jpg
pip install pillow

Let's use pyinstaller

Enter the following at the command prompt.

pyinstaller.exe resize.py --onefile --clean

(* 1) Write the name of the program you want to exe in the resize.py part. (* 2) Of course, do it in the hierarchy where the specified program is located.

I think that / dist is created, so double-click the exe in it. Since / dist / picture is required in resize.py, it is necessary to create it in advance and insert the image. I don't remember writing such a process as creating a folder automatically.

By the way, if you leave it as it is, the screen will disappear instantly when an error occurs and you will not be able to read the comments. So I will rewrite it a little more.

def end():
    while True:
        key = str(input("'e'Enter to exit:"))
        try:
            if key == "e":
                sys.exit()
        except ValueError:
            break

If you rewrite exit () of the previous program to end (), it will not end until the e key is input.

Reflection

I thought that the program to be exed should not depend on the command prompt, but a GUI using PyQt or the like.

bonus

Resize program using thumbnail. If you want to resize to a suitable size, use thumbnail, and if you want to resize as specified, use resize.

from PIL import Image

print("Resize.Only jpg is supported.")
inp_img = str(input("Please enter only the file name:"))
filename = "picture/" + inp_img + ".jpg "
img = Image.open(filename, 'r')

a = int(input("Vertical:"))
b = int(input("side:"))

img.thumbnail((a, b), Image.ANTIALIAS)
img.save(filename+"_resize.jpg ", "JPEG", quality=100, optimize=True)

Reference material

-Python exe conversion, Pyinstaller usage memo -How to use the Python 3.5 compatible image processing library Pillow (PIL)

Recommended Posts

I want to exe and distribute a program that resizes images Python3 + pyinstaller
I made a program to convert images into ASCII art with Python and OpenCV
I want to use a wildcard that I want to shell with Python remove
I want to build a Python environment
A python program that resizes a video and turns it into an image
I want to make a web application using React and Python flask
I want to create a window in Python
I want to make a game with Python
I want to write to a file with Python
I want to create a priority queue that can be updated in Python (2.7)
Python program is slow! I want to speed up! In such a case ...
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to iterate a Python generator many times
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to write in Python! (2) Let's write a test
I want to randomly sample a file in Python
I want to work with a robot in python.
[Python] I want to make a nested list a tuple
I want to run a quantum computer with Python
I made a program in Python that reads CSV data of FX and creates a large amount of chart images
[Mac] I want to make a simple HTTP server that runs CGI with Python
I want to write a triple loop and conditional branch in one line in python
I wrote a program quickly to study DI with Python ①
I want to start a lot of processes from python
I tried "a program that removes duplicate statements in Python"
A standard way to develop and distribute packages in Python
I want to send a message from Python to LINE Bot
I want to know the features of Python and pip
How to write a metaclass that supports both python2 and python3
I want to make input () a nice complement in python
I made a toolsver that spits out OS, Python, modules and tool versions to Markdown
A program that automatically resizes the iOS app icon to the required image size in Python
I want to create a karaoke sound source by separating instruments and vocals using Python
I want to make a voice changer using Python and SPTK with reference to a famous site
[Python] A program to find the number of apples and oranges that can be harvested
I want to scrape images to learn
I want to debug with Python
I want to record the execution time and keep a log.
[Python3] I made a decorator that declares undefined functions and methods.
I tried to convert a Python file to EXE (Recursion error supported)
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
[Python] A memo that I tried to get started with asyncio
Don't you want to say that you made a face recognition program?
I want to do a full text search with elasticsearch + python
I tried to make a periodical process with Selenium and Python
Try to write a program that abuses the program and sends 100 emails
Scraping and tabelog ~ I want to find a good restaurant! ~ (Work)
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I tried to create Bulls and Cows with a shell program
I want to create a pipfile and reflect it in docker
A quick guide to PyFlink that combines Apache Flink and Python
I made a library that adds docstring to a Python stub file.
[Python] A program that rotates the contents of the list to the left
[Python] I tried to make a simple program that works on the command line using argparse.
I want to clear up the question of the "__init__" method and the "self" argument of a Python class.
How to package and distribute Python scripts
[Python] A program that creates stairs with #
[Python] I made a decorator that doesn't seem to have any use.
I want to print in a comprehension
I made a payroll program in Python!