【教学类-72-02】20240819建筑对称图纸02(图案最大化)

背景需求

【教学类-72-01】20240803建筑对称图纸01-CSDN博客文章浏览阅读423次,点赞13次,收藏5次。【教学类-72-01】20240803建筑对称图纸01https://blog.csdn.net/reasonsummer/article/details/140893003

我感觉房子有大有小,有大量空白,我想让建筑变得最大

使用以下代码,把房子图片变成最大。

【教学类-75-01】20240817“通义万相图片最大化+透明png”的修图流程-CSDN博客文章浏览阅读1.6k次,点赞52次,收藏15次。【教学类-75-01】20240817“通义万相图片最大化+透明png”的修图流程https://blog.csdn.net/reasonsummer/article/details/141275880

看看修图的过程

一、原图(大于1MB,1024*1024)

二、PS白背景(几百K,1024*1024)

三、手动修图(手动PS,把背景黑点子,不要的装饰都修成白色 1024*1024,图案KB比PS白背景时一点)

四、把房子图案上下左右的白边都切掉

切边后,每张图片的长宽都不同,像素比手动修图的图片更小

五、变成透明图片(255,255,255白色变成透明)

原来是白色背景

现在是透明背景(浅灰色)

六、选择所有图片中宽度最大的一张,使用它的宽度和长度,把所有的图片统一格式

所以切边后的图案一样大了,KB变大(翻倍)

具体代码


'''
起吊白色背景255,255,255图片中黑色图案周围的一圈白边,确保图案最大化
星火讯飞、阿夏
20240817
'''





print('----1、切掉上下左右的白色图层------')

import os
from PIL import Image

import os,time
import shutil
from docx import Document
from docx.shared import Cm
from PIL import Image
from PyPDF2 import PdfFileMerger, PdfFileReader

from PIL import Image, ImageDraw, ImageFont
import os,random

path=r'C:\Users\jg2yXRZ\OneDrive\图片\20240818生日蛋糕'

# 留一点白边
white_edge=10
# C:\Users\jg2yXRZ\OneDrive\图片\20240817饮料甜甜圈\甜甜圈白色
import os
from PIL import Image

def find_non_white_pixel(image):
    width, height = image.size
    left, right, top, bottom = width, 0, height, 0

    for y in range(height):
        for x in range(width):
            r, g, b = image.getpixel((x, y))
            if r != 255 or g != 255 or b != 255:
                if x < left:
                    left = x
                if x > right:
                    right = x
                if y < top:
                    top = y
                if y > bottom:
                    bottom = y

    return left, right, top, bottom

def crop_image(image, left, right, top, bottom):
    return image.crop((left-white_edge, top-white_edge, right + white_edge, bottom + white_edge))



folder_path = path+r'\02PS白色手动'
output_folder = path+r'\03切边图'
os.makedirs(output_folder, exist_ok=True)


for file_name in os.listdir(folder_path):
    if file_name.endswith(".jpg") or file_name.endswith(".png"):
        input_path = os.path.join(folder_path, file_name)
        image = Image.open(input_path)
        left, right, top, bottom = find_non_white_pixel(image)
        cropped_image = crop_image(image, left, right, top, bottom)
        output_path = os.path.join(output_folder, file_name)
        cropped_image.save(output_path)




以上代码是用来切除白边,由于修图不稳定,白背景上会有看不见的黑点,导致切边位置不精确,所以代码单列。多次切边测试,确保所有图片都能切掉最大的白边。

在确保每张图片的图案都变成个最大(没有白边),再使用以下的“透明图+统一大小图”的代码

'''
去边后白色地方变成透明色
星火讯飞、阿夏
20240817
'''
from PIL import Image
import os

def process_image(file_path):
    img = Image.open(file_path)
    img = img.convert("RGBA")
    datas = img.getdata()

    new_data = []
    for item in datas:
        if item[0] == 255 and item[1] == 255 and item[2] == 255:
            new_data.append((255, 255, 255, 0))
        else:
            new_data.append(item)

    img.putdata(new_data)
    return img


path=r'C:\Users\jg2yXRZ\OneDrive\图片\20240818生日蛋糕'
input_folder = path+r'\03切边图'
output_folder = path+r'\04透明图'

if not os.path.exists(output_folder):
    os.makedirs(output_folder)

for file_name in os.listdir(input_folder):
    if file_name.endswith(".png") or file_name.endswith(".jpg") or file_name.endswith(".jpeg"):
        input_file_path = os.path.join(input_folder, file_name)
        output_file_path = os.path.join(output_folder, file_name)
        processed_image = process_image(input_file_path)
        processed_image.save(output_file_path)

'''
去边后白色地方变成透明色,透明图片统一大小
星火讯飞、阿夏
20240817
'''      

print('----2、图片放大成为1024*1024------')
import os
from PIL import Image

# path=r'C:\Users\jg2yXRZ\OneDrive\图片\20240817饮料圆形'
# input_folder = path+r'\圆形切边图'
# output_folder = path+r'\圆形切边图透明'

# 提取最大宽度的那张图片的尺寸
def get_max_width_and_height(fold_path):
    max_width = 0
    max_height = 0

    for file_name in os.listdir(fold_path):
        if file_name.endswith(".png") or file_name.endswith(".jpg") or file_name.endswith(".jpeg"):
            file_path = os.path.join(fold_path, file_name)
            img = Image.open(file_path)
            width, height = img.size
            if width > max_width:
                max_width = width
                max_height = height

    return max_width, max_height

fold_path = output_folder
max_width, max_height = get_max_width_and_height(fold_path)
print("最大宽度:", max_width)
print("最大高度:", max_height)

# 最大宽度: 724
# 最大高度: 869

# # 自定义长宽
# max_width=622
# max_height=877

# 统一所有图片大小
def resize_image(image_path, output_folder, new_image_name):
    img = Image.open(image_path)
    new_img = img.resize((max_width,max_height))
    new_img.save(os.path.join(output_folder, new_image_name))


newput_folder =path+r'\05统一图'
os.makedirs(newput_folder,exist_ok=True)



for file in os.listdir(output_folder):
    if file.endswith('.png'):
        input_image_path = os.path.join(output_folder, file)
        new_image_name = f"{file[:-4]}.png"
        resize_image(input_image_path, newput_folder, new_image_name)

作品效果:

左房图案最大了

没有修图前的左房(图案有大有小,有大量白边)

右房图案最大了

没有修图前的右房(图案有大有小,有大量白边)

最终效果对比

大图房子

小图房子

大图房子

小图房子

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿夏reasonsummer

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值