Select to view content in your preferred language

Reduce FGDB Attachment Size

613
3
11-17-2023 10:34 AM
Blevins_Mark
Frequent Contributor

Is there a method that will allow one to reduce the size (ie photo resolution) of fgdb record attachments, while they are still attached to the records inside the fgdb (as opposed to downloading the files and resizing outside of the fgdb)?

0 Kudos
3 Replies
MichaelVolz
Esteemed Contributor

If the attachments are stored in a BLOB field, which I think they would be, I do not think you can make changes to the attachments.  I think you would need to write the attachments out to a different format, make the resolution changes, and then save back in BLOB format.  Seems like a ton of work.

Blevins_Mark
Frequent Contributor

Thanks. I think you are right on all fronts, especially the ton of work part.

0 Kudos
Kepa
by Esri Contributor
Esri Contributor

Hi there,

I know it's an old thread but in case someone stumbles upon this issue I managed to compress them with this snippet

# Tested in ArcGIS PRO 3.5.x
import arcpy
import os
import tempfile
from PIL import Image
import io

# Attachment table
in_table = ""
quality = 70

with tempfile.TemporaryDirectory() as tmpdirname:
    with arcpy.da.UpdateCursor(in_table, ['DATA', 'ATT_NAME', 'DATA_SIZE']) as cursor:
        for row in cursor:
            attachment = row[0]
            filename = row[1]
            exported_img = os.path.join(tmpdirname, filename)
            open(exported_img, 'wb').write(attachment.tobytes())
            image = Image.open(exported_img)
            stream = io.BytesIO()
            image.save(stream, format='JPEG', quality=quality, exif=image.info.get("exif"))
            imagebytes = stream.getvalue()
            row[0] = imagebytes
            row[2] = len(imagebytes)
            cursor.updateRow(row)

It was tested on one of the two identical FileGDBs, which is actually compressing them based on the output:

Kepa_0-1760095626902.png

Funny thing, the FileGDB size itself does not seem to shrink by itself, it's like it maintains the allocated storage. But that's for another day...

Hope this helps!

 

0 Kudos