from PIL import Image
def image_ratio(file, new_width, new_height, multiple=None):
img = Image.open('../tupian/' + file)
width, height = img.size
print('原图片大小:%s * %s' % (width, height))
if multiple:
new_width, new_height = int(width * multiple), int(height * multiple)
width_offset = new_width / width
height_offset = new_height / height
new_img = Image.new('RGB', (new_width, new_height), (255, 255, 255))
for x in range(new_width):
for y in range(new_height):
r, g, b = img.getpixel((x // width_offset, y // height_offset))
new_img.putpixel((x, y), (r, g, b))
print('新图片大小:%s * %s' % (new_width, new_height))
new_img.save('../tupian/new_' + file)
if __name__=="__main__":
image_ratio("tu.jpg", 1920, 1080)
Python写一个简单的图形像素修改
于 2023-11-07 16:19:19 首次发布