PIL convert('L') to greyscale distorts some images - python-imaging-library

I'm converting some images to greyscale with the easy-thumbnails Django app. Most of them are fine, but a handful are getting partially or totally messed up. The same error is occurring on two different machines, so I don't think it's an issue of a PIL install being corrupt or something.
Here are a couple examples:
Original image http://66.228.39.122/uploads/companies/alerts_logo.png Disorted version http://66.228.39.122/uploads/companies/alerts_logo.png.198x150_q85_bw_upscale.jpg
Original image http://66.228.39.122/uploads/companies/HashableLogo_Color_RGB.png Disorted version http://66.228.39.122/uploads/companies/HashableLogo_Color_RGB.png.198x150_q85_bw_upscale.jpg
Any suggestions? Are the original images themselves corrupt in some subtle way? Should I recompile PIL with --no-random-distort or something? I've looked at the relevant part of the easy-thumbnails source and it's just calling image.convert('L'), so I think the problem must lie either inside PIL or with the images themselves. the way the exclamation mark is distorted in the Alerts.com logo makes me think that perhaps there's some kind of encoding issue, since the outline of the exclamation mark is angled and its color appears to be contributing to a larger grey splotch...but the Hashable logo doesn't show quite the same problem. Maybe they are two different problems. Thanks for any suggestions/thoughts/advice.
Update: Actually, if I remove the bw filter and add instead replace_alpha="red" I get similar (though not greyscaled) distortions, so it looks like it is probably PIL unhappy with how the images are encoded or something. Unfortunately I need non-technical people to be able to upload new images and have them work, so a programmatic solution is necessary, rather than just resaving the images manually.
Update 2: I actually experimented a bit and found that resaving the PNGs with GIMP solves the problem, if I resave once and check the box for saving color information for transparent pixels, and then resave a second time with that box unchecked. Just doing the first or the second of these doesn't solve the problem, and doing both and then the first again re-causes it. And it still doesn't handle partial transparency well. Since this is tedious and imperfect, a programmatic solution would still be great if there are any PIL or PNG encoding whizzes out there.

Here is some code that will convert an image to grayscale, without losing quality.
from PIL import Image, ImageOps
image = Image.open('filepath')
image = ImageOps.grayscale(image)
image.save('filepath', quality=95)

Related

Real cropping a PDF file

In ghostscript-crop-pdf-not-correctly,I got a cropped PDF,but it's only seemingly cropped.The remaining content still exists in fact.
In ghostscript-removes-content-outside-the-crop-box or
how-to-truly-crop-a-pdf-file or pdf-real-cropping or cropping-a-pdf-using-ghostscript-9-01 or itext-crop-out-a-part-of-pdf-file, no solution was found.May be a virtual PDF printer is the only way.
Use ghostscript or itext, Is there any way to clip a PDF file really.
A very straightforward (but perhaps not the most intelligent) way of solving your problem is to use pdfSweep.
pdfSweep is an iText7 addon that allows you to redact (remove) content.
It allows you to remove content by:
specifying a regular expression
specifying a rectangle (or rectangles)
In your case, you could calculate the rectangles you want removed, and then apply pdfSweep.
If you then crop the remaining page, the content would really be gone.
More information (including code samples) can be found here.
What leads you to believe that the content is still present ?
Any object which is not at least partially contained within the page clip will not be forwarded on to the pdfwrite device by Ghostscript, so I'm doubtful that content is preserved.
Your original question related to cropping away white space, so that makes your example file less than useful in this case. You should post an example of the problem file, and the Ghostscript command line you have used.
Note that if you are trying to crop out an image then no, this won't do what you want. If any part of the image lies on the media, then the entire image will be included in the file. The pdfwrite device isn't equipped to extract sub-areas from images. This is true for all the PDF editors that I'm aware of.

Unity sprites don't render properly

I recently came across a problem I can't solve which involves not being able to draw my sprites properly. I have tried a lot of different things and couldn't find any solution.
Here is how the image should look like in unity:
And here is how it actually looks like:
If someone could tell me how to fix this, I would be very grateful.
Presumably the top image is a screenshot of your image manipulation program, many of which use the chequerboard pattern to mean transparency. As such, the image you have exported is a gradient going from almost solid white at the bottom to transparent at the top. This is why the image appears as such in Unity.
Also, if you're wondering why the image appears as though it has bands of different colours, this is due to a problem called colour banding. This can be fixed by using a technique called Dither (which adds some noise to the image), but how you do so will depend on which image manipulation program you are using.

Ignore extra white space in Unity3D Texture

I have different textures for a player's helmet, shirt and pants in order to render custom uniforms. They have white space so it lays on the model correctly, but this is causing the App's file size to be huge once installed because the game has over a hundred items and each texture is 2.7 MB.
How can I tell Unity to ignore parts of the image or map the textures onto the player so that I do not need the white space? For example, cutting the whitespace out of the helmet image lowers the size to under a MB.
Thanks!
For the sake of others who read this:
The obvious answer is, cut out the empty spaces in an image editor. That will solve the problem in the way it really should be solved.
That being said, it's quite possible you are using poorly UV mapped models that need that space, and you are unable to fix this, as the person who asked this question is.
If you're in a position where it might cost a little time or money to get someone to fix it, you should, because no matter what, you're wasting space, and it will add up. No one wants a 100Mb download to get 50Mb worth of game. And if you payed someone for models and they came like this, consider taking it up with them, because this is a somewhat major flaw.
The "real" answer:
The first thing you should do is enable compression. From your picture it appears you are using the RGBA 16-bit format. This is a lower quality version of Truecolor, an uncompressed 32-bit format, but is not compressed in the "traditional" sense.
You should use the "Compressed" image import setting (To see it you must turn off Advanced settings). This will select one of several compression formats (depending on the platform), all of which are highly optimized. You can define a specific compression in the Advanced window, but it is rarely needed, as Unity is great at choosing the right one for a given situation, and can can take special cases (such as specific chipsets) into consideration.
Depending on the compression algorithm, that white space could easily end up taking next to no space, and depending on the image, the compression might end up virtually undetectable.
On average the "Compressed" setting can create several orders of magnitude of a reduction of image size.
From there, if your image is still to large you can experiment with import size. This creates a fairly linear change in space taken and quality image. You are importing at 1024x1024 right now. Importing at 512x512 will about half the amount of space taken, and half the resolution of your image, but depending on the art style, the change can often be negligible visually.
You can for more details on these changes in the documentation for the texture importer

How can I process an image to remove a watermark within my iPhone application?

I want to remove watermark from a picture within my iPhone / iPad application. Is there any kind of image processing I can perform within this application to do this?
Can't be done, sorry.
The watermarked image were originally two images (the base and the watermark), which were merged together to form the result. The problem here is that the most common image formats (such as JPG, PNG, or GIF) have no concept of layers - so that the base would be one layer, and the watermark another: the result is just one layer, onto which both were redrawn. This is somewhat similar to a physical painting: if you paint one image on a paper using watercolors, and then another over the same spot, their colors will mix and you won't be able to tell which parts belong to one or the other, as they'd become a single image.
This is similar with the computer image formats: there is only one "layer", which for every pixel encodes exactly one color that is there - only the current color exists, and the image doesn't keep track what was on that pixel before.
Now, the information is irreversibly lost from the result - in other words, it is not possible to recover the base knowing just the result (or the result and watermark) - BTW, that's exactly the point of watermarking.
I have borrowed the image sprites of StackOverflow for a demonstration; the actual images used are not unique, the technique would work just as well with any images. This was the watermark I used:
And this is the result image, after merging with the base:
Now, even though we have the exact watermark image used, there's no way to recover what was underneath that star in the original image. Through image processing operations, we could almost remove the star from the result, but there's not enough data to tell us what used to be underneath: - that information got erased in the merge at the beginning.
We could guess what used to be there, but then we're not doing recovery any more, we're interpreting the image and guessing what possibly could have been there - and that's pretty hard, even for a human; computers are really bad at that. This is the original image, before I watermarked it - I bet you were expecting something slightly different, no?
The watermark is almost certainly part of the image. (The only case in which it wouldn't be is something like PDF or SVG, where it could be a separate vector element.)
Watermarks are typically present on images for purposes of managing intellectual property; if one has licensed an image for a particular use, typically one will receive access to a version of the image without a watermark. Thus wanting to "remove watermarks" is also likely to be treated as highly suspicious.
Watermarks are part of the image, there isn't going to be a magic way to remove them and recover the missing pixels in any tool.
Take a look at the source! Most or the current watermarking is done in php as an automated script. In most cases you will see the base picture in source

iPhone transparent images rendering poorly

I'm developing an iPad application. I have been provided with a PNG image that contains some transparency - basically a drop shadow. The problem I'm having is that this is rendering poorly within the application, both on the device and in the sim.
I've made up some samples to illustrate. The first is how the image appears in the PSD (correctly that is). The second is how it appears on the device. You can see that the strip of shadow in the middle of the image is distinctly more yellow and poorly looking.
PDF http://www.aspyre.com.au/stackoverflow/photoshop.png On Device http://www.aspyre.com.au/stackoverflow/device.png
Any ideas what I'm doing wrong?
Edit: Links to files:
- PNG
- PSD
Edit 2: I've also tried pngcrush to remove the gamma, in case that was causing a problem, but no luck. Directions I followed were here: pngcrush
The reason you're getting a colour in your shadow is because the PSD's shadow layer is set to Multiply and has a colour in it. When you export it without a background, Photoshop is unable to multiply it to anything and just uses the layer as is. You need to grab the selection of the shadow layer, create a new layer, and fill that selection with black. Then set that new layer's opacity to something that mimics the old shadow.
Also I recommend you use Save-For-Web if you don't already. The colour-profile you use isn't much of an issue then as it will be stripped. However the point is valid that you want to be in sRGB when making iPhone/iPad graphics.
Link to your PSD adjusted: PSD
Common mistakes:
Your color space is non-standard. Either use no color space, or specify sRGB (strongly preferred).
You didn't save the PNG with gamma information included.
Without access to the actual files we're grasping at straws.. Maybe you have an 8-bit instead of 24-bit png?
For your shadow, use black instead of gray. Then adjust the transparency. That should fix it.
I had a similar issue that I resolved by disabling compression for an image. This is done in XCode by removing the .png extension. You can disable png compression for an entire project by editing the 'Compress PNG Files' project setting, but it is not recommended.
More details about Xcode PNG compression: http://iphonedevelopment.blogspot.com/2008/10/iphone-optimized-pngs.html