# coding=utf-8
from PIL import Image
def color_picture(filepath):
print('修改截圖顏色。。。')
# 圖片放大
img = Image.open(filepath)
# 圖片轉化成灰度圖像
xin_img = img.convert('L')
# xin_img.show()
# 圖片二值化處理, 圖片由 灰色背景 變量
threshold = 127 # 二值化阈值 , 默認的是127
t_list = []
for i in range(256):
if i < threshold:
t_list.append(0)
else:
t_list.append(1)
# print(t_list)
new_img = xin_img.point(t_list, '1')
# 保存 更改後的 圖片
new_name = filepath.replace('%s' % filepath[-4:], '') + '_new_' + filepath[-4:]
new_img.save(new_name)
# new_img.show()
return new_name
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
# coding=utf-8
from PIL import Image
def size_picture(filepath):
print('修改截圖大小。。。')
# 圖片放大
img = Image.open(filepath)
width, height = img.size
# print(img.size, width, height) # (553, 432) 553 432
new_img = img.resize((960, 720), Image.ANTIALIAS)
# 保存 更改後的 圖片
new_name = filepath.replace('%s' % filepath[-4:], '') + '_new_' + filepath[-4:]
new_img.save(new_name)
# new_img.show()
return new_name
if __name__ == '__main__':
'''
更改圖片 大小 或 顏色 等, 並保存 新圖片, 返回新圖片名字
'''
filepath = 'img_chi_tu_canshu.jpg'
size_picture(filepath)
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
# coding=utf-8
from PIL import Image
import os
def mergei(images):
heigh_size = 0
width_size = 0
for i in range(len(images)):
img = Image.open(images[i])
print(img.size)
heigh_size = img.size[1]
width_size += img.size[0]
# heigh_size = 720
# width_size = 960
print('heght_size = %s <--> width_size = %s ' % (heigh_size, width_size))
imagefile = []
for j in range(2):
imagefile.append(Image.open(images[j]))
target = Image.new('RGB', (width_size, heigh_size))
# target.save('9999999.jpg')
print(target.size)
left = 0
right = width_size/2
image = Image.open(images[0])
target.paste(image, (0, 0))
# for image in imagefile:
# print('--------', left, 0, right, heigh_size)
# target.paste(image, (left, 0, right, heigh_size))
# left += heigh_size
# right += heigh_size
quality_value = 100
target.save('img_00_%s.jpg' % (left), quality=quality_value)
left = 1
image = Image.open(images[1])
target = Image.open('img_00_0.jpg')
target.paste(image, (928, 0, 928*2, 1920))
target.save('img_00_%s.jpg' % (left), quality=quality_value)
'''
(928, 1920)
(928, 1920)
heght_size = 1920 <--> width_size = 1856
(1856, 1920)
'''
if __name__ == '__main__':
# 圖片拼接
images = ['img_shen_fen_zheng01.jpg', 'img_shen_fen_zheng02.jpg']
mergei(images)