How to get absolute file path for linked texture image? I am writing a custom export script for blender and need to copy the textures from the materials texture slots to another location.
I am using shutil for copyfile:

copyfile(bpy.path.abspath(texture.image.filepath), export_path + filename)


This works, as long as the texture isn't linked from another .blend file. In this case, texture.image.filepath returns the path relative to the source blend file, not the current working file. bpy.path.abspath then just appends it to the current working directory, which is wrong of course. How to deal with this?
Not very nice, but it's working.

import os
import bpy

tex = bpy.data.images['WALL_1773.png']
# linked image from /project/assets/shaders/walls.blend

tex_path = tex.filepath
# '//../textures/walls/WALL_1773.png'

blend_dir_path = os.path.dirname(tex.library.filepath)
# '/project/assets/shaders'

full_path = blend_dir_path + tex_path[1:]
# '/project/assets/shaders/../textures/walls/WALL_1773.png'

norm_path = os.path.normpath(full_path)
# '/project/assets/textures/walls/WALL_1773.png'


Better code based on batFINGER's comment

import os
import bpy

tex = bpy.data.images['WALL_1773.png']
# linked image from /project/assets/shaders/walls.blend

full_path = bpy.path.abspath(tex.filepath, library=tex.library)
# '/project/assets/shaders/../textures/walls/WALL_1773.png'

norm_path = os.path.normpath(full_path)
# '/project/assets/textures/walls/WALL_1773.png'