编辑图片像素,要钱?不存在的,教你用python改变图片size、color,拼接图片

本文介绍了一种使用Python的PIL库进行图片处理的方法,包括图片颜色调整、尺寸改变及图片拼接技术。通过代码实例展示了如何将图片转换为灰度、进行二值化处理、调整图片大小以及将多张图片拼接成一张。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# 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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坦桑尼亚奥杜威峡谷能人

感谢您的认可

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值