Resampling with antialiasing while using only 8 greyscales

Paul Furley <[email protected]> Sat, 23 Mar 2013 09:16:11 +0000
Newsgroups gmane.comp.python.image
Message-ID <CAKNAFAJ=gGaXf2Edmdj7JYEQX=mETN3ZiW-K9aYre-=vLDxdVQ@mail.gmail.com>
--===============0870935109==
Content-Type: multipart/alternative; boundary=bcaec54a37bc09e50904d89407ba

--bcaec54a37bc09e50904d89407ba
Content-Type: text/plain; charset=ISO-8859-1

Good evening,

F
irstly, thanks for creating and maintaining such a fantastic library, the
PIL is awesome!

I've got an interesting problem and I'd really appreciate if someone could
point me in the right direction. I'm a reasonable developer but I've become
out of my depth in image science :)

I've got greyscale images which I have dithered (Floyd-Steinberg) so that
they contain exactly 8 shades. The pixel values present in the images are 0,
36, 73, 109, 146, 182, 219, 255 - so 8 levels with pure-white and
pure-black being represented. Naturally, most of the 256 possible pixel
values in the histogram are empty.

I want to reduce the size of this image by 3x in both directions, ie (300,
300) => (100, 100) *with* anti-aliasing but *without* creating pixels
outside of those 8 shades. Of course, the Lanczos implementation in PIL
doesn't know to only use values 0, 36, 73 etc, so it uses the full 256
range, thereby undoing my dithering work.

Can anyone suggest a good approach to this? Would it be possible to modify
the resampling algorithm to work in 8 shades rather than 256? Or is there a
way I could put the pixel values back into the 8 "buckets" afterwards
without wrecking the colour accuracy? I have tried the latter and gradients
become steps, as you'd expect. It doesn't seem right to apply dithering
again somehow.

I have considered re-ordering my process from:
*High-res image -> Dithering -> Resampling*

to

*High-res image -> Resampling -> Dithering*

but I then think the dithering process wreaks havoc on the beautifully
antialiased edges in the image.

Any thoughts would be very gratefully received.

Kind regards,
Paul

--bcaec54a37bc09e50904d89407ba
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_default" style=3D"font-family:verdana,=
sans-serif">Good evening,<br><br></div>F<div class=3D"gmail_default" style=
=3D"font-family:verdana,sans-serif;display:inline">irstly, thanks for creat=
ing and maintaining such a fantastic library, the PIL is awesome!<br>


<br></div><div class=3D"gmail_default" style=3D"font-family:verdana,sans-se=
rif;display:inline">I&#39;ve got an interesting problem and I&#39;d really =
appreciate if someone could point me in the right direction. I&#39;m a reas=
onable developer but I&#39;ve become out of my depth in image science :)<br=
>


<br></div><div class=3D"gmail_default" style=3D"font-family:verdana,sans-se=
rif;display:inline">I&#39;ve got greyscale images which I have dithered (Fl=
oyd-Steinberg) so that they contain exactly 8 shades. The pixel values pres=
ent in the images are <span style=3D"vertical-align:baseline;font-variant:n=
ormal;font-style:normal;font-size:15px;background-color:transparent;text-de=
coration:none;font-family:Arial;font-weight:normal">0, 36, 73, 109, 146, 18=
2, 219, 255</span> - so 8 levels with pure-white and pure-black being repre=
sented. Naturally, most of the 256 possible pixel values in the histogram a=
re empty.<br>


<br></div><div class=3D"gmail_default" style=3D"font-family:verdana,sans-se=
rif;display:inline">I want to reduce the size of this image by 3x in both d=
irections, ie (300, 300) =3D&gt; (100, 100) <b>with</b> anti-aliasing but <=
b>without</b> creating pixels outside of those 8 shades. Of course, the Lan=
czos implementation in PIL doesn&#39;t know to only use values 0, 36, 73 et=
c, so it uses the full 256 range, thereby undoing my dithering work.<br>


<br></div><div class=3D"gmail_default" style=3D"font-family:verdana,sans-se=
rif;display:inline">Can anyone suggest a good approach to this? Would it be=
 possible to modify the resampling algorithm to work in 8 shades rather tha=
n 256? Or is there a way I could put the pixel values back into the 8 &quot=
;buckets&quot; afterwards without wrecking the colour accuracy? I have trie=
d the latter and gradients become steps, as you&#39;d expect. It doesn&#39;=
t seem right to apply dithering again somehow.<br>


</div><div class=3D"gmail_default" style=3D"font-family:verdana,sans-serif;=
display:inline"><br>I have considered re-ordering my process from:<br></div=
><div class=3D"gmail_default" style=3D"font-family:verdana,sans-serif;displ=
ay:inline">


<b>High-res image -&gt; Dithering -&gt; Resampling</b><br></div><div class=
=3D"gmail_default" style=3D"font-family:verdana,sans-serif;display:inline">=
<br></div><div class=3D"gmail_default" style=3D"font-family:verdana,sans-se=
rif;display:inline">


to <br><div class=3D"gmail_default" style=3D"font-family:verdana,sans-serif=
;display:inline"><br></div><b>High-res image -&gt; Resampling -&gt; Ditheri=
ng</b><br></div><div class=3D"gmail_default" style=3D"font-family:verdana,s=
ans-serif;display:inline">


<br></div><div class=3D"gmail_default" style=3D"font-family:verdana,sans-se=
rif;display:inline">but I then think the dithering process wreaks havoc on =
the beautifully antialiased edges in the image.<br><br></div><div class=3D"=
gmail_default" style=3D"font-family:verdana,sans-serif;display:inline">


Any thoughts would be very gratefully received.<br><br></div><div class=3D"=
gmail_default" style=3D"font-family:verdana,sans-serif;display:inline">Kind=
 regards,<br></div><div class=3D"gmail_default" style=3D"font-family:verdan=
a,sans-serif;display:inline">


Paul<br></div><div class=3D"gmail_default" style=3D"font-family:verdana,san=
s-serif;display:inline"></div></div>

--bcaec54a37bc09e50904d89407ba--

--===============0870935109==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Image-SIG maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/image-sig

--===============0870935109==--