1. York
  2. pyPicCrop

Overview

HTTPS SSH

PicCrop

PicCrop is an open source Python application to crop sub-pictures from parent pictures. This tool also allows format-converting from a corp-list file to a YAML layout file.

A crop-list file lists parent pictures and describes sub-pictures to crop.

A YAML layout file describes multiple picture layouts of screens. I invented the syntax under the standard YAML and used to assemble separated sub- pictures into whole-pictures.

Install

  1. Download a binary distribution of PicCrop (e.g., PicCrop-1.0-bin.zip) from Downloads page.
  2. Uncompress the binary distribution.

Getting Started

  1. Install PicCrop.
  2. Copy source (parent) pictures to the screen folder.
  3. Edit crop.lst
  4. Run rect.bat to draw rectangles listing in crop.lst.
  5. Run crop.bat to crop sub-pictures from source pictures.
  6. Run yaml.bat to convert the crop-list file (i.e., crop.lst) to the YAML file (i.e., layout_coord.yaml).

Note

  • Batch files rect.bat, crop.bat, and yaml.bat can be run in any order.
  • Result pictures of rect.bat are put into out folder.
  • Generated sub-pictures are put into out folder.
  • Run clean.bat to remove the generated files

Crop-list File

A crop.lst looks like as follows:

# section 1
parent1.png
 0,  0, 50, 50, crop1-1.png
50, 50, 12, 34, crop1-2.png

# section 2
parent2.png
 0,  1, 30, 40, crop2-1.png
90, 50, 33, 44, crop2-2.png
50, 50, 33, 22, crop2-3.png

# section 3
parent3.png
 0,  1, 30, 40, crop3-1.png
90, 50, 33, 44, crop3-2.png
50, 50, 33, 22, crop3-3.png
  • A crop-list file can list multiple original-subpicture sections.
  • Each section begins with a original picture filename (e.g., parent1.png), and describes multiple statements of sub-pictures (e.g., crop1-1.png).
  • Each sub-picture statement lists an area (i.e., x, y, w, h) and a target filename.
  • Symbol # is the comment prefix.

An Example

Let's say that I want to crop icons and texts from the iPod touch photograph named ipod-touch-5th-black.png:

ipod-touch-5th-black.png

First, I edited crop.lst and inputed 2 sections as below:

# source file to crop out 4 icons
#--------------------------------
ipod-touch-5th-black.png

# x,  y,  w,  h,  target file
#------------------------------------
117, 139, 62, 62, ico_FaceTime.png
190, 139, 62, 62, ico_Calendar.png
263, 139, 62, 62, ico_Photos.png
336, 139, 62, 62, ico_Camera.png


# source file to crop out 4 texts
#--------------------------------
ipod-touch-5th-black.png

# x,  y,  w,  h,  target file
#------------------------------------
112, 203, 70, 15, txt_FaceTime.png
185, 203, 70, 15, txt_Calendar.png
258, 203, 70, 15, txt_Photos.png
331, 203, 70, 15, txt_Camera.png

Second, I run rect.bat to generate the following 2 pictures:

ipod-touch-5th-black.png_1.png:

ipod-touch-5th-black.png_1.png

  • The above picture marks the icon areas with red rectangles.

ipod-touch-5th-black.png_2.png:

ipod-touch-5th-black.png_2.png

  • The above picture marks the text areas with red rectangles.

Then, I run crop.bat to get the following pictures:

icon pictures:

ico_FaceTime.png ico_Calendar.png ico_Photos.png ico_Camera.png

text pictures:

txt_FaceTime.png txt_Calendar.png txt_Photos.png txt_Camera.png

After running yaml.bat I got a YAML file (i.e., layout_coord.yaml):

# Generated by the Picture Crop v1.0
#    !author: Jiang Yu-Kuan <yukuan.jiang@gmail.com>
#    !trail: crop.py yaml -olayout_coord.yaml crop.lst
---
name: ipod-touch-5th-black
layout: {kind: coordinate}
align: left
parts:
  - {image: ico_FaceTime.png, coord: [117, 139]}
  - {image: ico_Calendar.png, coord: [190, 139]}
  - {image: ico_Photos.png, coord: [263, 139]}
  - {image: ico_Camera.png, coord: [336, 139]}
---
name: ipod-touch-5th-black
layout: {kind: coordinate}
align: left
parts:
  - {image: txt_FaceTime.png, coord: [112, 203]}
  - {image: txt_Calendar.png, coord: [185, 203]}
  - {image: txt_Photos.png, coord: [258, 203]}
  - {image: txt_Camera.png, coord: [331, 203]}
...

Command Line

Top level

usage: crop.exe [-h] [-v] {crop,yaml} ...

positional arguments:
  {crop,rect,yaml}  commands
    crop            Crop out pictures according to a crop-list file.
    rect            Mark out rectangles from a crop-list file.
    yaml            Generate a YAML file of screens with coordinate layouts.

optional arguments:
  -h, --help     show this help message and exit
  -v, --version  show program's version number and exit

crop command

usage: crop.exe crop [-h] [-s <directory>] [-o <directory>] crop-list-file

positional arguments:
  crop-list-file        The crop list file.

optional arguments:
  -h, --help            show this help message and exit
  -s <directory>, --screen_dir <directory>
                        assign a <directory> to read screen pictures. The
                        default directory is "screen".
  -o <directory>, --out_dir <directory>
                        assign a <directory> to store output cropped
                        pictures. The default directory is "out".

rect command

usage: crop.exe rect [-h] [-s <directory>] [-o <directory>] crop-list-file

positional arguments:
  crop-list-file        The crop list file.

optional arguments:
  -h, --help            show this help message and exit
  -s <directory>, --screen_dir <directory>
                        assign a <directory> to read screen pictures. The
                        default directory is "screen".
  -o <directory>, --out_dir <directory>
                        assign a <directory> to store output screen pictures.
                        The default directory is "out".

yaml command

usage: crop.exe yaml [-h] [-o <file>] crop-list-file

positional arguments:
  crop-list-file        The crop list file.

optional arguments:
  -h, --help            show this help message and exit
  -o <file>, --output <file>
                        place the output into <file>, the YAML file (default
                        "layout_coord.yaml").