-
Hi there, Any pointers in the right direction would be welcome. Cheers, Florian Here is my code:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi Florian, Thanks for using the PhotoshopAPI and apologies for the late reply. The issue you are encountering is due to the ordering of the channels coming from cv2 which is (height, width, channels). PhotoshopAPI on the other hand requires (channels, height, width) which leads to the error you are encountering. I have updated your code to show how this can be done fairly trivially using numpy. import psapi
import numpy as np
import cv2
document_color_mode = psapi.enum.ColorMode.rgb
width = 1920
height = 1080
file = psapi.LayeredFile_8bit(document_color_mode, width, height)
# CV2 reads images in packed BGR order by default, we now need to go from packed to planar
# e.g. from BGR BGR BGR BGR -> RRRR BBBB GGGG
image = cv2.imread("image.png")
transformed_image = np.zeros((image.shape[2], image.shape[0], image.shape[1]), np.uint8)
transformed_image[0] = image[:, :, 2] # Use numpy slicing to get the third channel of bgr
transformed_image[1] = image[:, :, 1]
transformed_image[2] = image[:, :, 0]
img_lr = psapi.ImageLayer_8bit(
transformed_image,
layer_name="layer 001",
width=width,
height=height,
color_mode=document_color_mode)
# Add to the file and write out
file.add_layer(img_lr)
file.write("./Out.psd") Another little caveat when using numpy which in this case doesnt apply but is good to know is that if you were to transpose the axis by default numpy returns a view on the original data and doesnt actually modify the data which will lead to incorrect results. image = np.ndarray((1080, 1920, 3), np.uint8) # We assume this is in RGB order
transformed_image = image.transpose(2, 0, 1) # This is incorrect as it doesnt actually transform the data itself!
transformed_image = image.transpose(2, 0, 1).copy() # Copy the data over in order to make the array contiguous again, this will work The error message you are encountering is however not very helpful so I will work towards making this more clear in an upcoming update. I hope that helps :) |
Beta Was this translation helpful? Give feedback.
-
Hey @vonbehr, just wanted to give you an update on this. I made the error raised much more explicit now which should help clear this up if this error raises. The error message will now read something along these lines:
Additionally if we detect a non-c-style contiguous array (when doing np.reshape() without a call to copy()) the PhotoshopAPI will internally take care of this conversion and force the memory order as well as raising the following warning:
I will merge these changes to master with this PR #74 but it will likely only show up in a release in a while since i want to add a couple more features before pushing the next release. If you still want to grab the latest you can simply get it from this CI action (once done), scroll to the bottom and you will see an artifact called |
Beta Was this translation helpful? Give feedback.
Hi Florian,
Thanks for using the PhotoshopAPI and apologies for the late reply. The issue you are encountering is due to the ordering of the channels coming from cv2 which is (height, width, channels). PhotoshopAPI on the other hand requires (channels, height, width) which leads to the error you are encountering. I have updated your code to show how this can be done fairly trivially using numpy.