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
Notes
Python example (runnable)
pywin32. The example assumes app / doc / page / layer are already connected.
See Connection & Initshape = 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:
shape.SetSize(w, h)changes the object's display size on the page. Not one pixel changes. The image is stretched or squashed and its effective dpi moves with it.shape.Bitmap.Resample(w, h, dpi)changes the pixels themselves, and the resolution is recomputed from them. Display size may change as a consequence, but because the pixel count changed, not because anything was stretched.
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
- Width / Height — target pixel counts, integers. Pixels, not document units.
- Resolution — target dpi, written into the bitmap metadata.
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:
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
# 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
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
- A client sends 6000px phone photos you will never use; drop them to 2500px so files move through the pipeline again.
- A 72dpi web image has to go on a business card. Work out the pixels needed from the finished size, then decide — if the gap is large, get a different image, because resampling cannot rescue it.
- Batch archiving: standardise the long edge and keep total size under control.
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.
- This section has 8 entries; this is one of them.
- See the full section index: Bitmap
Other entries in this section
- shape.BitmapEntry point to the bitmap sub-object. Check Type == 5 first or access
- bitmap.ResolutionX / ResolutionYHorizontal and vertical resolution in dpi. 300 is the floor for print;
- bitmap.SizeWidth / SizeHeightThe bitmap's size in pixels, not document size. This is how you tell w
- bitmap.ModeColor mode (CMYK, RGB, grayscale, bitmap and so on). The first thing t
- shape.ConvertToBitmap(Resolution)Turns a vector object into a bitmap (export and reimport, essentially)
- bitmap.SaveAs(FileName, Filter)Writes the bitmap out to its own file. Batch image extraction, sending
- shape.ApplyEffectInvert() / ApplyEffectGamma() / ApplyEffectPosterize()Applies color adjustments to a bitmap: invert, gamma, posterize. These