Skip to content

Commit

Permalink
[doc] add docs about copy_xx()
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoHsiao1 committed Jan 27, 2024
1 parent 1322694 commit d199abd
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 13 deletions.
42 changes: 35 additions & 7 deletions docs/Tutorial-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ class Image:
def clear_icc (self)
def clear_thumbnail (self)

def copy_to_another_image(self, another_image,
exif=True, iptc=True, xmp=True,
comment=True, icc=True, thumbnail=True)


class ImageData(Image):
def __init__(self, data: bytes)
Expand All @@ -96,11 +100,11 @@ def convert_iptc_to_xmp(data: dict, encoding='utf-8') -> dict
def convert_xmp_to_exif(data: dict, encoding='utf-8') -> dict
def convert_xmp_to_iptc(data: dict, encoding='utf-8') -> dict

__version__ = '...'
__exiv2_version__ = '...'
__version__ = '2.11.0'
__exiv2_version__ = '0.28.1'
```

## Class Image
## class Image

-`Image` 用于根据文件路径打开图片。例如:
```py
Expand All @@ -118,7 +122,7 @@ __exiv2_version__ = '...'
```
- 另一个例子:中国地区的 Windows 电脑通常用 GBK 编码文件路径,因此它们不能被 utf-8 解码。

### close
### close()

- 当你处理完图片之后,请记得调用 `img.close()` ,以释放用于存储图片数据的内存。
- 不调用该方法会导致内存泄漏,但不会锁定文件描述符。
Expand All @@ -128,7 +132,7 @@ __exiv2_version__ = '...'
data = img.read_exif()
```

### read
### read_xx()

- 读取元数据的示例:
```py
Expand All @@ -144,7 +148,7 @@ __exiv2_version__ = '...'
- 调用 `Image.read_*()` 是安全的。这些方法永远不会影响图片文件(md5不变)。
- 读取 XMP 元数据时,空白字符 `\v` 和 `\f` 会被替换为空格 ` ` 。
### modify
### modify_xx()

- 修改元数据的示例:
```py
Expand Down Expand Up @@ -189,7 +193,7 @@ __exiv2_version__ = '...'
''
```

### clear
### clear_xx()

- 调用 `img.clear_exif()` 将删除图片的所有 EXIF 元数据。一旦清除元数据,pyexiv2 可能无法完全恢复它。
- `img.clear_iptc()``img.clear_xmp()` 的用法同理。
Expand Down Expand Up @@ -229,6 +233,30 @@ __exiv2_version__ = '...'
- EXIF 标准允许在 JPEG 图片中嵌入缩略图,通常存储在 APP1 标签(FFE1)中。
- Exiv2 支持读写图像中的 EXIF 缩略图。但是只能插入 JPEG 缩略图(不能插入TIFF)。

### copy_xx()

- 以下代码用于从一个图片拷贝元数据到另一个图片:
```py
with pyexiv2.Image(r'.\pyexiv2\tests\1.jpg') as img1:
with pyexiv2.Image(r'.\pyexiv2\tests\2.jpg') as img2:
img2.modify_exif(img1.read_exif())
img2.modify_iptc(img1.read_iptc())
img2.modify_xmp(img1.read_xmp())
```
但更推荐以下代码:
```py
with pyexiv2.Image(r'.\pyexiv2\tests\1.jpg') as img1:
with pyexiv2.Image(r'.\pyexiv2\tests\2.jpg') as img2:
# 如果不想保留第二张图像中已有的元数据,可以提前清除第二张图像
# img2.clear_exif()
# img2.clear_iptc()
# img2.clear_xmp()
img1.copy_to_another_image(img2, exif=True, iptc=True, xmp=True, comment=False, icc=False, thumbnail=False)
```
理由如下:
- 它的效率更高,因为不需要多次执行 modify_xx() 。
- 一张图片的元数据可能是错误的格式,无法被 pyexiv2 解析,但仍然可以复制到另一张图片上。

## class ImageData

-`ImageData` 继承于类 `Image`, 用于从字节数据中打开图片。
Expand Down
40 changes: 34 additions & 6 deletions docs/Tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ class Image:
def clear_icc (self)
def clear_thumbnail (self)

def copy_to_another_image(self, another_image,
exif=True, iptc=True, xmp=True,
comment=True, icc=True, thumbnail=True)


class ImageData(Image):
def __init__(self, data: bytes)
Expand All @@ -96,8 +100,8 @@ def convert_iptc_to_xmp(data: dict, encoding='utf-8') -> dict
def convert_xmp_to_exif(data: dict, encoding='utf-8') -> dict
def convert_xmp_to_iptc(data: dict, encoding='utf-8') -> dict

__version__ = '...'
__exiv2_version__ = '...'
__version__ = '2.11.0'
__exiv2_version__ = '0.28.1'
```

## class Image
Expand All @@ -118,7 +122,7 @@ __exiv2_version__ = '...'
```
- Another example: Windows computers in China usually encoded file paths by GBK, so they cannot be decoded by utf-8.

### close
### close()

- When you're done with the image, remember to call `img.close()` to free the memory for storing image data.
- Not calling this method causes a memory leak, but it doesn't lock the file descriptor.
Expand All @@ -128,7 +132,7 @@ __exiv2_version__ = '...'
data = img.read_exif()
```

### read
### read_xx()

- An example of reading metadata:
```py
Expand All @@ -144,7 +148,7 @@ __exiv2_version__ = '...'
- It is safe to call `Image.read_*()`. These methods never affect image files (md5 unchanged).
- When reading XMP metadata, the whitespace characters `\v` and `\f` are replaced with the space ` `.
### modify
### modify_xx()

- An example of modifing metadata:
```py
Expand Down Expand Up @@ -189,7 +193,7 @@ __exiv2_version__ = '...'
''
```

### clear
### clear_xx()

- Calling `img.clear_exif()` will delete all EXIF metadata of the image. Once cleared, pyexiv2 may not be able to recover it completely.
- Use `img.clear_iptc()` and `img.clear_xmp()` in the similar way.
Expand Down Expand Up @@ -229,6 +233,30 @@ __exiv2_version__ = '...'
- The EXIF standard allows embedding thumbnails in a JPEG image, which is typically stored in the APP1 tag (FFE1).
- Exiv2 supports reading and writing EXIF thumbnails in images. But only JPEG thumbnails can be inserted (not TIFF thumbnails).

### copy_xx()

- The following code is used to copy metadata from one image to another image:
```py
with pyexiv2.Image(r'.\pyexiv2\tests\1.jpg') as img1:
with pyexiv2.Image(r'.\pyexiv2\tests\2.jpg') as img2:
img2.modify_exif(img1.read_exif())
img2.modify_iptc(img1.read_iptc())
img2.modify_xmp(img1.read_xmp())
```
However, the following code is more recommended:
```py
with pyexiv2.Image(r'.\pyexiv2\tests\1.jpg') as img1:
with pyexiv2.Image(r'.\pyexiv2\tests\2.jpg') as img2:
# If you don't want to keep the metadata already in the second image, you can clear the second image in advance.
# img2.clear_exif()
# img2.clear_iptc()
# img2.clear_xmp()
img1.copy_to_another_image(img2, exif=True, iptc=True, xmp=True, comment=False, icc=False, thumbnail=False)
```
The reasons are as follows:
- It's more efficient because it doesn't require multiple executions of modify_xx() .
- The metadata of an image may be in the wrong format and cannot be parsed by pyexiv2, but it can still be copied to another image.

## class ImageData

- Class `ImageData`, inherited from class `Image`, is used to open an image from bytes data.
Expand Down

0 comments on commit d199abd

Please sign in to comment.