程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

編輯圖片像素,要錢?不存在的,教你用python改變圖片size、color,拼接圖片

編輯:Python
# 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)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved