序列帧图片缩小后抖动问题

[复制链接]
查看11 | 回复0 | 2021-1-27 07:15:43 | 显示全部楼层 |阅读模式
目前我有个ksyx.json和ksyx.png的合图图片,ksyx.json里记录的是png图片的裁剪区域和按帧数执行的动画。
我将每张图裁剪出来,并按照比例缩小后,再将缩小的图片粘贴到一张空白的和裁剪出大小相同的图片中,并将
最后的图粘贴回大图片中。
最后缩小后图片按帧表现出来会或上或下偏移,出现抖动现象。正确结果是表现与原图表现相同。求提供下思路,哪里有问题
以下是主要函数代码:
importmath
importjson
importos
fromPILimportImageFileasPILImage,Image,ImageChops,ImageOps
fromresizeimageimportresizeimage
importcv2
importnumpyasnp
defscale_pic():
img_url=url.replace('/','\\')
img_type=img_url[img_url.rfind("."):]
json_url=img_url.replace(img_type,'.json')
#打开一张图
img=Image.open(testUrl+img_url)
ifimg.mode=="P":
img=img.convert("RGBA")
img_to_url=img_url[img_url.rfind("\\"):]
to_url=to_ima_url+img_url[0:img_url.rfind("\\")]
ifnotos.path.exists(to_url):
os.makedirs(to_url)
#rRate=1/target_size_rate
#out_image=region.thumbnail(size=(int(wRel),int(hRel)),resample=Image.ANTIALIAS)
out_image=np.zeros((img.height,img.width,4),np.uint8)
#out_image=Image.new("RGBA",size=img.size)
ifos.path.exists(testUrl+json_url):
file=open(testUrl+json_url,"rb")
file_json=json.load(file)
res=file_json['res']

forimg_datainres:#image_data就是id
x=res[img_data]['x']
y=res[img_data]['y']
h=res[img_data]['h']
w=res[img_data]['w']
region=img.crop((x,y,w+x,h+y))
region.format=img.format
#to_region_url1=os.path.join(to_url,img_data+"_1_.png");
#region.save(to_region_url1)
imgcv2=np.asarray(region)
imgcv2=cv2.cvtColor(imgcv2,cv2.COLOR_RGBA2BGRA)
#imgcv2=cv2.imread(to_region_url1,cv2.IMREAD_UNCHANGED)
gray_img=cv2.cvtColor(imgcv2,cv2.COLOR_BGRA2GRAY)
moment=cv2.moments(gray_img)
X1=int(moment["m10"]/moment["m00"])
Y1=int(moment["m01"]/moment["m00"])
cv2.circle(imgcv2,(X1,Y1),15,(205,114,101),1)
imgcv2=cv2.resize(imgcv2,(0,0),fx=target_size_rate,fy=target_size_rate,interpolation=cv2.INTER_AREA)
cv2.putText(imgcv2,img_data,(0,0),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255),2)
gray_img=cv2.cvtColor(imgcv2,cv2.COLOR_BGRA2GRAY)
moment=cv2.moments(gray_img)
X=int(moment["m10"]/moment["m00"])
Y=int(moment["m01"]/moment["m00"])
cv2.circle(imgcv2,(X,Y),15,(205,114,101),1)
#cv2.imshow("CenteroftheImage",imgcv2)
#cv2.waitKey(0)
#to_region_url=os.path.join(to_url,img_data+".png")
#cv2.imwrite(to_region_url,imgcv2)
x_offset,y_offset=X1-X,Y1-Y
imgcv2=cv2.cvtColor(imgcv2,cv2.COLOR_BGRA2RGBA)
img_blank=np.zeros((h,w,4),np.uint8)
img_blank[y_offset:y_offset+imgcv2.shape[0],x_offset:x_offset+imgcv2.shape[1]]=imgcv2
out_image[y:y+img_blank.shape[0],x:x+img_blank.shape[1]]=img_blank
#img_paste=Image.new("RGBA",(w,h))
#imgcv2=cv2.cvtColor(imgcv2,cv2.COLOR_BGRA2RGBA)
#img_paste.paste(Image.fromarray(np.asarray(imgcv2)))
#tmp_img=ImageChops.offset(img_paste,x_offset,y_offset)
#out_image.paste(tmp_img,(x,y))

#res[img_data]['w']=region.size[0]
#res[img_data]['h']=region.size[1]

#pos_info=file_json['mc']
#fornameinpos_info:
#image_pos_info=pos_info[name]["frames"]
#index=0
#foriteminimage_pos_info:
#image_pos=item
#ifimage_pos['res']==img_data:
#pos_x=image_pos['x']
#pos_y=image_pos['y']
#
#image_pos['x']=pos_x+x_offset2
#image_pos['y']=pos_y+y_offset2
#
#
#file_json['mc'][name]["frames"][index]=image_pos
#index+=1
#
#file_json['res']=res
Image.fromarray(out_image).save(to_url+img_to_url);
#out_image.save(to_url+img_to_url)
#json_to_url=json_url[json_url.rfind("\\"):]
#
#file_json=json.dumps(file_json)
#file_object=open(to_url+json_to_url,'w')
#file_object.write(file_json)
#file_object.close()


json文件的内容如下:
{"mc":{
"ksyx":{
"frameRate":26,
"frames":[
{
"res":"2CAB1992",
"x":412,
"y":882,
"duration":3
},
{
"res":"38B06D93",
"x":406,
"y":885,
"duration":3
},
{
"res":"988DFC7",
"x":408,
"y":886,
"duration":3
},
{
"res":"ADAA982B",
"x":409,
"y":889,
"duration":3
},
{
"res":"94E72AE9",
"x":409,
"y":892,
"duration":3
},
{
"res":"1EB5B985",
"x":410,
"y":892,
"duration":3
},
{
"res":"493F6E1",
"x":412,
"y":891,
"duration":3
},
{
"res":"38932583",
"x":412,
"y":889,
"duration":3
},
{
"res":"774BACA0",
"x":412,
"y":886,
"duration":3
},
{
"res":"31ACB3B9",
"x":412,
"y":883,
"duration":3
}
]
}},
"res":{
"2CAB1992":{"x":389,"y":194,"w":383,"h":193},
"38B06D93":{"x":1,"y":1,"w":386,"h":192},
"988DFC7":{"x":389,"y":1,"w":388,"h":191},
"ADAA982B":{"x":1,"y":195,"w":384,"h":186},
"94E72AE9":{"x":390,"y":576,"w":382,"h":182},
"1EB5B985":{"x":387,"y":760,"w":381,"h":180},
"493F6E1":{"x":1,"y":764,"w":379,"h":179},
"38932583":{"x":1,"y":581,"w":384,"h":181},
"774BACA0":{"x":390,"y":389,"w":389,"h":185},
"31ACB3B9":{"x":1,"y":389,"w":387,"h":190}
}}

分 -->
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行