PIL paste image without blur - python

I want to paste an image into a single coloured background using PIL but some blures and noises appear around pasted photo like this:
(Zoom photo to see noises. I think it is due from Antialiasing) But I want to paste with sharp boundaries like here :
I am using this codes for paste:
my_image.convert('RGBA')
background = Image.new("RGBA", (background_size), background_color)
background.paste( my_image, (coordinates), my_image )
background.save("result.jpg")
What sholud i do for pasting with sharp boundaries?
Thanks.

jpg is a lossy format, so it may blur your image or add noise, in order to save memory. Use a lossless format like png instead:
background.save("result.png")

Related

Saving an image as jpg gives me plain black

I am implementing gabor kernels, when I display the kernels while running the code (before saving them) they give me a picture like this
But after saving the kernels as jpg images using cv2.imwrite, I get like that
Any explanations? and how to save the kernels as in the first image?
There could be different causes. So I have two suggestions:
If you display the first picture with plt.imshow(), export it with plt.savefig(). This should easily be working.
If you still want to export the image with cv2.imwrite() make sure that the picture is correctly rescaled first. (mind that if you have only one channel, you will get a grayscale picture).
If we call the original picture org_img:
img = org_img
min_val,max_val=img.min(),img.max()
img = 255.0*(img - min_val)/(max_val - min_val)
img = img.astype(np.uint8)
cv2.imwrite(img,"picture.png")

python: when reading and saving a image the color change

I tried loading and saving images with python using cv2,PIL, scipy , but the saved image has a bit different color compare to the original.
I am loading and saving tif format, so i expect no color change.
link to the image I am using:
https://data.csail.mit.edu/graphics/fivek/img/tiff16_c/a0486-jmac_MG_0791.tif
the difference between loaded image and saved image is:
can you help me understand what I am doing wrong? why the color change?
update:
the problem is because the image is prophoto rgb color.
does anyone knows how can i convert a batch of images from prophoto rgb to rgb?
thanks,
yoav
option 1:
img = imread(file_name)
imsave('imread.tif', img)
option 2:
img = cv2.imread(file_name)
cv2.imwrite('cv2.tif', img)
option 3:
img = Image.open(file_name)
img.save('pil.tif')
I think OpenCV is more interested in Computer Vision - i.e. detecting and measuring objects etc than printing or high quality image reproduction, editing and printing, so it pretty much ignores ICC profiles. If anyone knows better, I am happy to be corrected.
You can use ImageMagick to convert images from one format to another, and to do many, many other things, one of which is changing colour profiles. So, I think, if you go to this website and download an sRGB profile (I chose the first one with "preference" in its name) and save it as sRGB.icc, you can change one of your ProPhoto images to a normal sRGB image with the following command in Terminal:
convert input.tif -profile sRGB.icc output.tiff
Try that and see if it works. If so, make a copy of your images and on a copy, you can run mogrify to do the whole lot in one go - beware and make a copy like I suggest because it will very quickly alter all your images...
magick mogrify -profile sRGB.icc *tif
You can see the embedded profile and loads of other information about an image using ImageMagick's identify command:
magick identify -verbose OneOfYourImages.tiff

Removing highlighted areas when converting pdf to image?

I'm trying to convert pdfs to png which usually works great but I occasionally get this result.
There's two parts that are 'highlighted' which I'm not sure why since ImageMagick doesn't consistently do this.
Here's the code I'm working with:
with Image(filename=pdf, resolution=200) as src:
src.background_color = Color('white')
src.alpha_channel = 'remove'
images = src.sequence
Image(images[1]).save(filename='test.png')
I thought maybe there was a problem with transparency so the first two lines are related to this question.
How can I get this image to just show up normally like this
image which looks correct? Thanks!
The issue you have is that your input has an alpha channel. So just removing the alpha channel or flattening it on white leaves that area as gray, since it is in the underlying image.
The best way to fix that is using ImageMagick -lat function.
See http://www.imagemagick.org/script/command-line-options.php#lat
As I do not have your original, I can only process your resulting PNG file, which shows this behavior.
Input with transparency
Processing
convert input.png -background white -flatten -negate -lat 25x25+10% -negate result.png

SVG/PNG files open differently in the browser

I am working on a project where our database is on svg format. Now, the weird thing (I don't know much about image formats) is that the image looks totally different if I open it on a computer program like Image Viewer, ImageMagick or Pinta, to how the image looks when I open it on a browser (be it Mozilla or Chrome). I am attaching an image (converted from svg to png) for convenience and you can see that the difference is really big if you open it on a browser compared to a normal program (if you download it and open it on your computer).
Now, I opened it on Python to see what is going on, and apparently the image is on RBGA format. I thought to convert it to RGB, and did it using the following code:
img = Image.open(os.path.join(PNG_REPO, page)[:-4] + ".png")
arr = np.array(img)
rgbImage = cv2.cvtColor(arr, cv2.COLOR_RGBA2RGB)
img = Image.fromarray(rgbImage)
img.save("please.png")
and then, weird stuff happened. The image seems to have become completely black (attached below) and opening it on Python, a local program or a browser doesn't make any difference anymore.
Anyone has any idea what is going on? I think that I am losing it and I am completely stucked.
In fact, SVG and PNG are totally different.
SVG (Scallable vector graphics) is not a standard image format : the image is not stored as an raay of pixel (or derivative), but as a vectorized format. To display it, you would have to render the vectors into pixels, then display them. The big assets of this approach are the quality vs. size : for illustrations, you can have a fantastic quality for almost nothing in size, however, it requires more time to process, and the decoding is very different from other image formats, thus it is often not supported by image viewers, and the renders can depend a bit on the renderer.
PNG (Portable network graphics) on the other hand is still based on pixel, and is very widely supported.
As for your question, your image have an alpha (transparency) channel. When converting it to RGB, you are getting rid of this transparency, which opencv translates into a fixed color. Here, this color is (0,0,0), that is to say black.
You should try this :
img = Image.open(os.path.join(PNG_REPO, page)[:-4] + ".png")
img.load()
new_img = Image.new("RGB", img.size, (255, 255, 255))
new_img.paste(img, mask=img.split()[3]) # 3 is the alpha channel
new_img.save("should_be_good.png")

paste image without background PIL

I am trying to paste many small grayscale images into a bigger one. All images are jpegs. The small images had been previously rotated, so they have black background. What I wanted to do is to paste them without a background color, in other words, I need the background color to be transparent.
Thank you for your suggestions,
to my knowledge, jpg does not support transparency, you probably want your output to be a png, and you will need to set the alpha channel to be nothing
http://www.talkgraphics.com/showthread.php?22385-How-do-I-make-jpeg-image-background-transparent

Categories

Resources