Esempio n. 1
0
def test_from_scratch(vector_mask):
    from pytoshop.user.nested_layers import Group, Image

    img1 = np.empty((100, 80), dtype=np.uint8)
    img2 = np.empty((100, 80), dtype=np.uint8)
    img3 = np.empty((100, 80), dtype=np.uint8)
    img4 = np.empty((2, 100, 80), dtype=np.uint8)

    layers = [
        Group(
            layers=[
                Image(channels={0: img1},
                      top=0, left=0, bottom=100, right=80),
                Image(channels=img2,
                      top=15, left=15),
                Image(channels=img4,
                      top=42, left=43),
                Image(channels=[img1, img1],
                      top=-5, left=49)
            ]),
        Image(channels={0: img3})
    ]

    psd = nested_layers.nested_layers_to_psd(
        layers, enums.ColorMode.grayscale,
        vector_mask=vector_mask)

    buff = io.BytesIO()
    psd.write(buff)

    buff.seek(0)

    pytoshop.read(buff)
Esempio n. 2
0
    def _add_layers_from_existing_psd(
            psd_file_obj) -> List[nested_layers.Layer]:
        """ Create List of Layers from opened PsdFile file object """
        existing_psd = pytoshop.read(psd_file_obj)
        existing_layers = nested_layers.psd_to_nested_layers(existing_psd)

        return existing_layers
Esempio n. 3
0
def parse_psd(fn):
    with open(fn, 'rb') as fd:
        im = pytoshop.read(fd)
        imdata = im.image_data
        layers = im.layer_and_mask_info.layer_info.layer_records
        bglayer = layers[0]
        bgimg = bglayer.channels[0].image
        fglayer = layers[1]
        print(fglayer._left, fglayer._top, fglayer._bottom, fglayer._right)

        fgroi = fglayer.channels[-1].image

        fgimg = np.zeros(shape=bgimg.shape)
        fgimg[fglayer._top:fglayer._bottom,
              fglayer._left:fglayer._right] = fgroi

        return bgimg, fgimg
Esempio n. 4
0
def test_proxy(compression):
    from pytoshop.user.nested_layers import Group, Image

    class ImageProxy(object):
        @property
        def shape(self):
            return (1000, 1000)

        @property
        def dtype(self):
            return np.uint8().dtype

        def __array__(self):
            return np.arange(1000000, dtype=np.uint8).reshape((1000, 1000))

    image_layers = []
    for i in range(256):
        image_layers.append(
            Image(
                channels={0: ImageProxy()},
                top=i, left=i
            )
        )

    layers = [
        Group(
            layers=image_layers
        )
    ]

    psd = nested_layers.nested_layers_to_psd(
        layers, enums.ColorMode.grayscale,
        compression=compression
    )

    buff = io.BytesIO()
    psd.write(buff)

    buff.seek(0)

    psd = pytoshop.read(buff)
    layers = nested_layers.psd_to_nested_layers(psd)
    # Extract all the data so it's tested and included in the timings
    for image in layers[0].layers:
        [x.image for x in image.channels.values()]
Esempio n. 5
0
def test_convert_to_version2(filename):
    if filename.endswith('rt.psd'):
        return

    with open(filename, 'rb') as fd:
        f = pytoshop.read(fd)
        f.version = 2

        # Disable compression just to make these tests faster
        # for layer in f.layer_and_mask_info.layer_info.layer_records:
        #     for channel in layer.channels.values():
        #         channel.compression = 0

        fd2 = io.BytesIO()
        f.write(fd2)

    fd2.seek(0)
    f = pytoshop.PsdFile.read(fd2)
Esempio n. 6
0
def test_files(filename):
    if filename.endswith('rt.psd'):
        return

    with open(filename, 'rb') as fd:
        f = pytoshop.read(fd)

        channels = f.image_data.channels
        assert channels is not None
        shape = f.image_data.shape
        assert len(shape) == 3
        info_map = f.layer_and_mask_info.additional_layer_info_map
        assert isinstance(info_map, dict)

        fd2 = io.BytesIO()
        f.write(fd2)

    fd2.seek(0)
    f = pytoshop.PsdFile.read(fd2)
Esempio n. 7
0
bg_path = './background'
bc_path = './bottom_color'
result_path = './result'

if not os.path.isdir(bg_path):
    os.makedirs(bg_path)
if not os.path.isdir(bc_path):
    os.makedirs(bc_path)
if not os.path.isdir(result_path):
    os.makedirs(result_path)

for file in os.listdir(bg_path):
    bg_group = None
    with open(os.path.join(bg_path, file), 'rb') as fd:
        bg_psd = pytoshop.read(fd)
        bg_nested = nested_layers.psd_to_nested_layers(bg_psd)
        for l in bg_nested:
            if l._name == 'bg':
                bg_group = l

        with open(os.path.join(bc_path, file), 'rb') as fd:
            bc_psd = pytoshop.read(fd)
            bc_nested = nested_layers.psd_to_nested_layers(bc_psd)
            if bg_group != None:
                bc_nested.insert(len(bc_nested) - 1, bg_group)

            with open(os.path.join(result_path, file), 'wb') as fd:
                bc_psd = nested_layers.nested_layers_to_psd(
                    bc_nested, bc_psd._color_mode)
                bc_psd.write(fd)
Esempio n. 8
0
def psdfile_handler(request):

    if request.method == 'GET':
        # Author, Work, Episode Dropdown menu
        # Not working yet
        psdfile_list = PSDFile.objects.all()
        serializer = PSDFileUploadSerializer(psdfile_list, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        psdfiles = request.FILES.getlist('psdfile')
        #
        #
        #
        #

        file_path = settings.MEDIA_ROOT + '/m1_test'
        for fd in psdfiles:
            psd = pytoshop.read(fd)
            # shape : (height, width)
            shape = psd.shape
            # extract layers from psd file
            # return : list of layers
            nestedLayers = nl.psd_to_nested_layers(psd)

            backcolor_index, visible_layer_list = m1.find_backcolor_and_visible(
                nestedLayers, shape)

            frame_index = []
            line_drawing_index = []
            left, right, top, bottom = [], [], [], []
            final_img = None

            # iterate all visible layers
            for visible_idx in visible_layer_list:
                layer_img = m1.layer_to_image(nestedLayers, visible_idx, shape)

                # left, right, top, bottom would be empty if the layer is not frame one
                is_frame, temp_left, temp_top, temp_right, temp_bottom = m1.check_frame_layer(
                    layer_img)
                left.extend(temp_left)
                right.extend(temp_right)
                top.extend(temp_top)
                bottom.extend(temp_bottom)

                if is_frame:
                    frame_index.append(visible_idx)

                else:
                    # the number of black pixels
                    black = np.all(layer_img == [0, 0, 0], axis=-1).sum()
                    # not black neither white pixels
                    not_black = np.any(layer_img != [0, 0, 0], axis=-1)
                    not_white = np.any(layer_img != [255, 255, 255], axis=-1)
                    # in short, the number of colorful pixels
                    color = (not_black == not_white).sum()

                    # Assume that line_drawing layer consists of white&black pixels
                    if black > color:
                        line_drawing_index.append(visible_idx)

            # create white layer
            if len(line_drawing_index) != 0:
                final_img = np.full((shape[0], shape[1], 3), 255)

            # prepare each line_drawing_layer
            # if a pixel in each layer image is not white, which is drawing, overwrite the pixel on previous overlapped layer image
            for ld_idx in line_drawing_index:
                # make each layer to image
                layer_img = m1.layer_to_image(nestedLayers, ld_idx, shape)
                # return boolean
                is_not_white = np.any(layer_img != [255, 255, 255], axis=-1)
                # get indices which indicates non-white pixels
                not_white_idx = np.argwhere(is_not_white)
                final_img[not_white_idx[:, 0],
                          not_white_idx[:, 1]] = layer_img[not_white_idx[:, 0],
                                                           not_white_idx[:, 1]]

            # crop image with frame corner points
            for i in range(len(left)):
                result = final_img[top[i]:bottom[i] + 1,
                                   left[i]:right[i] + 1, :]
                cv2.imwrite(file_path + '/{0}.png'.format(i),
                            result[:, :, ::-1])

        #
        #
        #
        for psdfile in psdfiles:
            serializer = PSDFileUploadSerializer(data=request.data)
            if serializer.is_valid():
                serializer.save(user=request.user,
                                datafile=psdfile,
                                author=request.data['author'],
                                work=request.data['work'],
                                episode=request.data['episode'])
        return Response({'status': 'success'})
Esempio n. 9
0
        elif layer.visible == True:
            visible_layer_list.append(layers.index(layer))

    return backcolor_index, visible_layer_list


if __name__ == '__main__':

    file_path = ''
    psd_name = ''
    save_path = file_path + ''
    os.mkdir(save_path)

    with open(file_path + psd_name, 'rb') as fd:
        # read binary file
        psd = pytoshop.read(fd)
        # shape : (height, width)
        shape = psd.shape
        # extract layers from psd file
        # return : list of layers
        nestedLayers = nl.psd_to_nested_layers(psd)

        backcolor_index, visible_layer_list = find_backcolor_and_visible(
            nestedLayers, shape)

        frame_index = []
        line_drawing_index = []
        left, right, top, bottom = [], [], [], []
        final_img = None

        # iterate all visible layers