Home/ Reference/ Bitmap

bitmap.Resample(Width, Height, Resolution)

Resamples, changing pixel count and resolution. Upscaling invents no detail, it only softens what's there. Leave the original alone if you can and scale the shape in

Signature

bitmap.Resample(Width, Height, Resolution)

Notes

Resamples, changing pixel count and resolution. Upscaling invents no detail, it only softens what's there. Leave the original alone if you can and scale the shape instead.

Python example (runnable)

Requires pywin32. The example assumes app / doc / page / layer are already connected. See Connection & Init
Python
shape = app.ActiveSelectionRange(1)

if shape.Type == 5:
    bmp = shape.Bitmap
    try:
        bmp.Resample(2000, 1500, 300)               # width, height, resolution
        print("Resampled: ", bmp.SizeWidth, bmp.SizeHeight, bmp.ResolutionX)
    except Exception as e:
        print("Resample failed; argument order may differ in your version: ", e)

It changes pixels, not the frame

Confusing Resample with SetSize is the most expensive mix-up in bitmap handling:

So "the image went blurry" has two completely different causes with opposite fixes. Work out which one you have before touching anything.

The three arguments

Resample does not preserve the aspect ratio for you. For proportional scaling, compute both dimensions from the original ratio, or the artwork comes out distorted. That is the source of nearly every "it looks squashed after resampling" complaint.

It is irreversible

Once you drop to a lower resolution, resampling back up returns an interpolated, blurry version. The original detail is gone for good. That is a property of bitmaps, not a flaw in the API. Which means:

Python
import os

# save a copy first, or do the work on a copy in the first place
doc.SaveAs("D:/output/source_untouched.cdr")

Keeping an untouched original before a batch resample is the only reason that line exists. Do not skip it.

Should you resample at all: compute the effective dpi first

Whether a bitmap is good enough to print is not decided by the dpi written in the file. It is decided by the effective dpi:

effective dpi = pixels / printed size in inches

Python
# a 2000px wide image going onto 210mm of paper
px = 2000
mm = 210
print(round(px / (mm / 25.4)))      # about 242 dpi - acceptable for most print

Below 300 and it is a stretch; well above 300 (say 1200) and you can downsample with confidence, saving both file size and downstream processing time. This one calculation replaces most of the "nudge the resolution until it looks right" guesswork.

Complete example: proportional scale to a target long edge

Python
def resample_long_edge(shape, target_px=2000, dpi=300):
    bmp = shape.Bitmap              # raises if the shape is not a bitmap
    w0, h0 = bmp.SizeWidth, bmp.SizeHeight     # pixels
    if max(w0, h0) <= target_px:
        return False                # already small enough, do not upscale

    scale = target_px / max(w0, h0)
    w1 = max(1, int(round(w0 * scale)))
    h1 = max(1, int(round(h0 * scale)))

    bmp.Resample(w1, h1, dpi)       # in place, irreversible
    return True

print(resample_long_edge(doc.ActiveSelectionRange(1), 2000, 300))

The early return False is deliberate. Resampling a small image upward produces something that takes more space and looks equally blurry. Upscaling never adds information, only bytes.

The dpi field versus the pixel count

Both describe the same thing, but trusting only one of them is how people get fooled. ResolutionX can say 300 while the pixel size is 600x400 — which prints at two inches square. Always judge print quality from pixels and finished size. The dpi field is a hint for layout software, not a promise.

When Resample is actually the right call

Conversely, "change 72 to 300 so the number looks right" is not a use case. That relabels the file; it does not improve the picture.

About this section: Bitmap

Bitmap — raster image handling.

Other entries in this section