--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
Let's try it now. Enter the following at the command prompt to install.
pip install pyinstaller
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
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.
I thought that the program to be exed should not depend on the command prompt, but a GUI using PyQt or the like.
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)
-Python exe conversion, Pyinstaller usage memo -How to use the Python 3.5 compatible image processing library Pillow (PIL)
Recommended Posts