Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tiff How to enhance UnsharpMask ? #7450

Closed
monkeycc opened this issue Oct 8, 2023 · 9 comments
Closed

tiff How to enhance UnsharpMask ? #7450

monkeycc opened this issue Oct 8, 2023 · 9 comments

Comments

@monkeycc
Copy link

monkeycc commented Oct 8, 2023

1.zip

16 bit gray chart tiff

Image.open

How to load ?

ImageEnhance.Sharpness
ImageEnhance.UnsharpMask

image enhancement
How to achieve the desired outcome ?

@homm
Copy link
Member

homm commented Oct 8, 2023

To enhance something you first need to say what wrong with existing thing.

@monkeycc
Copy link
Author

monkeycc commented Oct 8, 2023

To enhance something you first need to say what wrong with existing thing.

Sorry

Realizing the need for improvement in one's own expression

Completed

16 bit gray chart tiff

Image.open

How to load ?

ImageEnhance.Sharpness
ImageEnhance.UnsharpMask

image enhancement
How to achieve the desired outcome ?

@radarhere
Copy link
Member

  1. Pillow does have a load() method, to pull the data from the file into memory,
from PIL import Image
im = Image.open('1.tiff')
im.load()

but I expect there are many people who use Pillow without ever thinking about loading images, so I'd be surprised if that's what you meant. If you're interested in just opening the image,

from PIL import Image
im = Image.open('1.tiff')

should suffice. If that doesn't work for you, please let us know what version of Pillow you're using.

  1. Regarding ImageEnhance, you may have tried
from PIL import Image, ImageEnhance
im = Image.open('1.tiff')
enh = ImageEnhance.Sharpness(im)
enh.enhance(1.9).save('out.tiff')

but received an error - ValueError: image has wrong mode

That is the same as #7397. In that issue, the following workaround is suggested.

from PIL import Image, ImageEnhance
im = Image.open("1.tiff").point(lambda x: x / 256).convert("L")
enh = ImageEnhance.Sharpness(im)
enh.enhance(1.9).save("out.tiff")

Pillow doesn't have an ImageEnhance.UnsharpMask. You might be thinking of ImageFilter.UnsharpMask

from PIL import Image, ImageFilter
im = Image.open("1.tiff").point(lambda x: x / 256).convert("L")
im.filter(ImageFilter.UnsharpMask).save("out.tiff")

@radarhere
Copy link
Member

I suspect you primarily want a way to make 1.tiff look like the image on the right of your post though? Could you provide a description of how it was created?

@radarhere
Copy link
Member

If you're asking how to provide arguments to ImageFilter.UnsharpMask, here is an example.

from PIL import Image, ImageFilter
im = Image.open("1.tiff").point(lambda x: x / 256).convert("L")
im.filter(ImageFilter.UnsharpMask(3, 400, 0)).save("out.tiff")

@radarhere
Copy link
Member

from PIL import Image, ImageEnhance, ImageFilter
im = Image.open("1.tiff").point(lambda x: x / 256).convert("L")
im.filter(ImageFilter.UnsharpMask(7, 3600, 0)).save("out.jpg")

gives

Is that closer?

I recognise you may not want the background though, so the following code will get rid of it.

from PIL import Image, ImageEnhance, ImageFilter
im = Image.open("1.tiff").point(lambda x: x / 256).convert("L")
out = Image.new("L", im.size, 255)
mask = ImageEnhance.Contrast(im).enhance(5).point(lambda x: 255 if x < 50 else 0)
out.paste(im.filter(ImageFilter.UnsharpMask(7, 3600, 0)), mask=mask)
out.save("out.jpg")

@monkeycc
Copy link
Author

monkeycc commented Oct 9, 2023

Thank you for taking the time to debug

right
I tested it

Ripple pattern
微信截图_20231009104739
微信截图_20231009104844

Can't we handle this matter?

@radarhere
Copy link
Member

I imagine you're really after a different approach than Unsharp, but see what you think of this slight modification.

from PIL import Image, ImageEnhance, ImageFilter
im = Image.open("1.tiff").point(lambda x: x / 256).convert("L")
out = Image.new("L", im.size, 255)
out.paste(im, mask=ImageEnhance.Contrast(im).enhance(5).point(lambda x: 255 if x < 50 else 0))
out.paste(im.filter(ImageFilter.UnsharpMask(7, 3600, 0)), mask=im.point(lambda x: 255 if x < 59 else 0))
out.save("out.jpg")

gives

@monkeycc
Copy link
Author

monkeycc commented Oct 9, 2023

Thank you very much for your help

This method is not suitable
Due to surface defects and missing details

I will try to adjust myself

Because I want to see surface defects
Need to see surface details
See if there are any defects

Thank you again for your selfless help
It moved me very much
@radarhere

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants