问题描述
我想创建一个带有参数(输入文件名,输出文件名,origin_x,origin_y,new_height,new_width)的函数。到目前为止,我将文件读取为字节,并将其追加到单独的单独行列表中。但是有些不对劲,因为输出图像变得歪斜了。而且我正在使用pil库保存它,我不能使用。
Context
import numpy as np
from PIL import Image #just to save the file. but dont want to use it
import struct
def read_rows(path,origin_x,origin_y,height,width):
image_file = open(path,"rb")
image_file.seek(18,0)
bmp_w = struct.unpack('I',image_file.read(4))[0]
# print(bmp_w)
bmp_h = struct.unpack('I',image_file.read(4))[0]
# print(bmp_h)
image_file.seek(54)
if origin_x+width > bmp_w or origin_y+height > bmp_h:
print(f'Dimensions are outside the figure size,choose between {bmp_w}x{bmp_h} pixels')
return
# We need to read pixels in as rows to later swap the order
# since BMP stores pixels starting at the bottom left.
rows = []
row = []
pixel_index = 0
while True:
if pixel_index == bmp_w:
pixel_index = 0
rows.insert(0,row)
if len(row) != bmp_w * 3:
raise Exception(f"Row length is not {bmp_w*3} but " + str(len(row)) + " / 3.0 = " + str(len(row) / 3.0))
row = []
pixel_index += 1
r_string = image_file.read(1)
g_string = image_file.read(1)
b_string = image_file.read(1)
if len(r_string) == 0:
# This is expected to happen when we've read everything.
if len(rows) != bmp_h:
print (f"Warning!!! Read to the end of the file at the correct sub-pixel (red) but we've not read {bmp_h} rows!")
break
if len(g_string) == 0:
print( "Warning!!! Got 0 length string for green. Breaking.")
break
if len(b_string) == 0:
print ("Warning!!! Got 0 length string for blue. Breaking.")
break
r = ord(r_string)
g = ord(g_string)
b = ord(b_string)
row.append(b)
row.append(g)
row.append(r)
image_file.close()
return rows
def repack_sub_pixels(rows,width):
print ("Repacking pixels...")
sub_pixels = []
for jdx,row in enumerate(rows):
if jdx >=origin_y and jdx <origin_y+height:
for idx,sub_pixel in enumerate(row):
if idx >= (origin_y*3) and idx< ((origin_y+width)*3):
sub_pixels.append(sub_pixel)
print ("Packed",len(sub_pixels),"sub-pixels.")
return sub_pixels
def crop(file_name,output_name,a,b,h,w):
'''
Takes input file name,output file name,origin x from left,origin y from top,height of cropped image from origin,width of cropped image from origin.
'''
rows = read_rows(file_name,w)
sub_pixels = repack_sub_pixels(rows,w)
print(type(sub_pixels[0]))
dim = int((len(sub_pixels)//3)**(1/2))
print(dim)
l = np.array(sub_pixels).astype(np.uint8)
l = l.reshape((h,w,3))
im = Image.fromarray(l)
im.save(f'{output_name}')
print(f'File saved as {output_name}')
解决方法
Untitled