문제

나의 크기를 조정해야 jpg 이미지는 파이썬을 잃지 않고 원래 이미지의 EXIF 데이터(메타데이터에 대해 날짜를 촬영,카메라 모델 etc.).모든 google 검색에 대한 python 및 이미지점을 필 라이브러리는 현재 사용하지만,하지 않는 것을 유지할 수 있 메타데이터가 포함되어 있습니다.코드는 지금까지(사용필)이:

img = Image.open('foo.jpg')
width,height = 800,600
if img.size[0] < img.size[1]:
    width,height = height,width

resized_img = img.resize((width, height), Image.ANTIALIAS) # best down-sizing filter
resized_img.save('foo-resized.jpg')

어떤 아이디어가?또 다른 라이브러리를 사용할 수있?

도움이 되었습니까?

해결책

import jpeg
jpeg.setExif(jpeg.getExif('foo.jpg'), 'foo-resized.jpg') 

http://www.emilas.com/jpeg/

다른 팁

당신이 사용할 수 있습 pyexiv2 복사하 EXIF 데이터에서는 원본 이미지입니다.다음 예에서는 이미지 크기를 조절을 사용하여 필 라이브러리,EXIF 데이터 복사 pyexiv2 및 이미지 크기 EXIF 필드를 설정하는 새로운 크기입니다.

def resize_image(source_path, dest_path, size):
    # resize image
    image = Image.open(source_path)
    image.thumbnail(size, Image.ANTIALIAS)
    image.save(dest_path, "JPEG")

    # copy EXIF data
    source_image = pyexiv2.Image(source_path)
    source_image.readMetadata()
    dest_image = pyexiv2.Image(dest_path)
    dest_image.readMetadata()
    source_image.copyMetadataTo(dest_image)

    # set EXIF image size info to resized size
    dest_image["Exif.Photo.PixelXDimension"] = image.size[0]
    dest_image["Exif.Photo.PixelYDimension"] = image.size[1]
    dest_image.writeMetadata()

# resizing local file
resize_image("41965749.jpg", "resized.jpg", (600,400))

실제로 정말 간단한 방법으로의 복사 EXIF 데이터에서 사진과 다른만 PIL.하지만 그것을 허용하지 않을 수정할 수 exif 태그가 있습니다.

image = Image.open('test.jpg')
exif = image.info['exif']
# Your picture process here
image = image.rotate(90)
image.save('test_rotated.jpg', 'JPEG', exif=exif)

당신이 볼 수 있듯이,저장 기능을 수 있습 exif 논쟁을 허용하는 복사본을 원 exif 데이터에 새로운 이미지를 저장하는 경우.당신이 실제로 필요하지 않은 어떤 다른 lib 는 경우에는 당신이 원하는 모든 할 수 있다.나는 찾을 수 없는 것이 어떤 문서에 저장 옵션 및 내가 알지도 못하는 경우에는 특정 베개 또는 작업 PIL too.(만약 누군가가 어떤 종류의 링크 것을 즐길 수 있는 경우 그들은 이를 게시한 의견에)

왜 사용하지 않는 ImageMagick?
그것은 아주는 표준 도구(예를 들어,그것은 표준에 의해 사용되는 도구이 갤러리 2);나는 그것을 사용하지 마십시오,그러나 그것은 python 인터페이스 뿐만 아니라(또는,당신은 또한 단순히 생성 명령)와의 대부분은 모두를 유지할 수 있도록 합 EXIF 정보를 사로 모든 변환입니다.

에 대한 pyexiv2v0.3.2 API 설명서 참조하는 복사 방법을 통해 수행 EXIF 데이터에서 다른 하나의 이미지를.이 경우에는 것 EXIF 데이터는 원래의 이미지를 통해 크기가 조정된 이미지입니다.

가@Maksym Kozlenko 업데이트된 코드 복사하기 위한 EXIF 데이터이다:

    source_image = pyexiv2.ImageMetadata(source_path)
    source_image.read()

    dest_image = pyexiv2.ImageMetadata(dest_path)
    dest_image.read()

    source_image.copy(dest_image,exif=True)
    dest_image.write()

당신이 사용할 수 있습 pyexiv2 파일을 수정하여 저장한 후에는니다.

여기에 업데이트에 대답으로의 2018. piexif 순수한 python 라이브러리는 나를 위해 쉽게 설치를 통해 pip(pip 설치 piexif)고 아름답게 일(감사합니다,관리자!). https://pypi.org/project/piexif/

의 사용법은 매우 간단하여,하나의 라인 복제를 허용되는 대답과 모든 사본을 EXIF 태그에서 원본 이미지를 이미지 크기 조정:

import piexif
piexif.transplant("foo.jpg", "foo-resized.jpg")

나는 아직 시도하지 않지만,그것은 다음과 같할 수 있도 수행 modifcations 사용하여 쉽게,부하 덤프,그리고 삽입 기능을 설명서 링크된 문서입니다.

PIL 처리 EXIF 데이터,지 않나요?보 PIL.ExifTags.

from PIL import Image
img_path = "/tmp/img.jpg"
img = Image.open(img_path)
exif = img.info['exif']
img.save("output_"+img_path, exif=exif)

시험에서 베개 2.5.3

보@Depado 의 솔루션을 작동하지 않는 나를 위해서는 나의 시나리오 이미지지 않도 포함 exif 세그먼트입니다.

pyexiv2 하에 설치하는 Mac,대신 나를 사용하여 모듈 pexif https://github.com/bennoleslie/pexif/blob/master/pexif.py.추가"exif 세그먼트는"이미지에 포함되지 않은 exif 정보,추가 exif 정보에 포함된 이미지를 소유하고 exif 세그먼트

from pexif import JpegFile

#get exif segment from an image
jpeg = JpegFile.fromFile(path_with_exif)
jpeg_exif = jpeg.get_exif()

#import the exif segment above to the image file which does not contain exif segment
jpeg = JpegFile.fromFile(path_without_exif)
exif = jpeg.import_exif(jpeg_exif)
jpeg.writeFile(path_without_exif)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top