Python学習ノート(18)PIL画像処理とTkinterグラフィックインタフェース
参考資料:https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00140767171357714f87a053a824ffd811d98a83b58ec13000
https://www.cnblogs.com/xianyue/p/6588869.html
https://blog.csdn.net/tianxiawuzhei/article/details/44922843
https://blog.csdn.net/sinat_25704999/article/details/50118465
1、内蔵モジュールのほか、Pythonには大量のサードパーティモジュールがある.基本的には、すべてのサードパーティ製モジュールがPyPI-the Python Package Indexに登録されます.対応するモジュール名が見つかったらeasy_を使用できます.インストールまたはpipインストール.
2、PILはよく使われるサードパーティモジュールの一つで、フルネームPython Imaging Libraryは、すでにPythonプラットフォームの事実上の画像処理標準ライブラリである.WindowシステムでPILを使用する前に、次の準備が必要です.
(1)公式サイトからPILインストールパッケージをダウンロードする:http://pythonware.com/products/pil/(2)インストール時にレジストリにPython 2が見つからないというメッセージが表示されます.7、次のpythonプログラムを実行してPython 2.7インストール情報がレジストリに書き込まれます.
注:上記のコードは、自己参照資料2を参照してください.
(3)からhttps://pypi.python.org/pypi/Pillow/2.1.0#id2ダウンロードインストールPillow-2.1.0.win-amd64-py2.7.exe(md 5)64ビットバージョン.そうでない場合、PIL関連コードを実行すると、「The_imaging C module is not installed」というメッセージが表示されます.詳細については、リファレンス3を参照してください.
次は私の学習コードです.
注意:PIL関連モジュールを参照する場合は、「from PIL import...」を選択しないと、「IOError:cannot identify image file」というメッセージが表示されます.詳細については、リファレンス4を参照してください.
3、Pythonは、Tk、wxWidgets、Qt、GTKなど、さまざまなグラフィックインタフェースをサポートするサードパーティ製ライブラリです.しかし、Pythonが持っているライブラリはTkをサポートするTkinterで、Tkinterを使用して、パッケージをインストールする必要がなく、直接使用することができます.コードを直接見る:
https://www.cnblogs.com/xianyue/p/6588869.html
https://blog.csdn.net/tianxiawuzhei/article/details/44922843
https://blog.csdn.net/sinat_25704999/article/details/50118465
1、内蔵モジュールのほか、Pythonには大量のサードパーティモジュールがある.基本的には、すべてのサードパーティ製モジュールがPyPI-the Python Package Indexに登録されます.対応するモジュール名が見つかったらeasy_を使用できます.インストールまたはpipインストール.
2、PILはよく使われるサードパーティモジュールの一つで、フルネームPython Imaging Libraryは、すでにPythonプラットフォームの事実上の画像処理標準ライブラリである.WindowシステムでPILを使用する前に、次の準備が必要です.
(1)公式サイトからPILインストールパッケージをダウンロードする:http://pythonware.com/products/pil/(2)インストール時にレジストリにPython 2が見つからないというメッセージが表示されます.7、次のpythonプログラムを実行してPython 2.7インストール情報がレジストリに書き込まれます.
#
# script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
#
# written by Joakim Loew for Secret Labs AB / PythonWare
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm
#
# modified by Valentine Gogichashvili as described in http://www.mail-archive.com/[email protected]/msg10512.html
import sys
from _winreg import *
# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix
regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
installpath, installpath, installpath
)
def RegisterPy():
try:
reg = OpenKey(HKEY_CURRENT_USER, regpath)
except EnvironmentError as e:
try:
reg = CreateKey(HKEY_CURRENT_USER, regpath)
SetValue(reg, installkey, REG_SZ, installpath)
SetValue(reg, pythonkey, REG_SZ, pythonpath)
CloseKey(reg)
except:
print "*** Unable to register!"
return
print "--- Python", version, "is now registered!"
return
if (QueryValue(reg, installkey) == installpath and
QueryValue(reg, pythonkey) == pythonpath):
CloseKey(reg)
print "=== Python", version, "is already registered!"
return
CloseKey(reg)
print "*** Unable to register!"
print "*** You probably have another Python installation!"
if __name__ == "__main__":
RegisterPy()
注:上記のコードは、自己参照資料2を参照してください.
(3)からhttps://pypi.python.org/pypi/Pillow/2.1.0#id2ダウンロードインストールPillow-2.1.0.win-amd64-py2.7.exe(md 5)64ビットバージョン.そうでない場合、PIL関連コードを実行すると、「The_imaging C module is not installed」というメッセージが表示されます.詳細については、リファレンス3を参照してください.
次は私の学習コードです.
from PIL import Image, ImageFilter, ImageDraw, ImageFont
import os, random
def getNewFilename(src, mark, ext):
result = None
try:
o = os.path.splitext(src)
result = o[0] + mark + ext
except BaseException, e:
result = None
return result
#
def createThumbnail(src):
result = None
try:
im = Image.open(src)
w, h = im.size
im.thumbnail((w // 2, h // 2))
result = getNewFilename(src, '_thumb', '.jpg')
im.save(result, 'jpeg')
except BaseException, e:
result = None
return result
#
def createBlur(src):
result = None
try:
im = Image.open(src)
im2 = im.filter(ImageFilter.BLUR)
result = getNewFilename(src, '_blur', '.jpg')
im2.save(result, 'jpeg')
except BaseException, e:
result = None
return result
# :
def rndChar():
return chr(random.randint(65, 90))
# 1:
def rndColor():
return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))
# 2:
def rndColor2():
return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))
# w- h- count-
def createCode(w, h, count):
result = None
width = w * count
height = h
try:
image = Image.new('RGB', (width, height), (255, 255, 255))
# Font :
font = ImageFont.truetype('C:\\Windows\\Fonts\\Arial.ttf', 36)
# Draw :
draw = ImageDraw.Draw(image)
# :
for x in range(width):
for y in range(height):
draw.point((x, y), fill=rndColor())
# :
sCode = ''
for t in range(count):
c = rndChar()
sCode = sCode + c
draw.text((w * t + 10, 10), c, font = font, fill = rndColor2())
# :
image = image.filter(ImageFilter.BLUR)
result = getNewFilename('code.jpg', '_' + sCode, '.jpg')
image.save(result, 'jpeg')
except BaseException, e:
result = None
return result
def Test():
s = raw_input('input an image filename:')
thumfile = createThumbnail(s)
if thumfile != None:
print 'thumbnail filename is %s for %s' % (thumfile, s)
else:
print 'createThumbnail called failure'
blurfile = createBlur(s)
if blurfile != None:
print 'BLUR filename is %s for %s' % (blurfile, s)
else:
print 'createBlur called failure'
codefile = createCode(60, 60, 4)
if codefile != None:
print 'Code Filename is %s' % codefile
else:
print 'createCode called failure'
注意:PIL関連モジュールを参照する場合は、「from PIL import...」を選択しないと、「IOError:cannot identify image file」というメッセージが表示されます.詳細については、リファレンス4を参照してください.
3、Pythonは、Tk、wxWidgets、Qt、GTKなど、さまざまなグラフィックインタフェースをサポートするサードパーティ製ライブラリです.しかし、Pythonが持っているライブラリはTkをサポートするTkinterで、Tkinterを使用して、パッケージをインストールする必要がなく、直接使用することができます.コードを直接見る:
from Tkinter import *
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
def createWidgets(self):
self.helloLabel = Label(self, text='Hello, world!')
self.helloLabel.pack()
self.quitButton = Button(self, text='Quit', command=self.quit)
self.quitButton.pack()
def Test():
app = Application()
# :
app.master.title('Hello World')
# :
app.mainloop()
return 0
今日はここまで勉強して、次の節はネットプログラミングから学びます.